Skip to main content
gamedev.net
🔒 Locked

Buttons don't work

Started by GuitarPlayer0912 Aug 7, 2006 at 11:58 AM 4 replies 550+ views
Original Post
GuitarPlayer0912
GuitarPlayer0912
Hey everyone, I'm using Code::Blocks c++ compiler, and I'm making a simple windows app with 2 buttons - OK and Cancel. The buttons show up and display their text, but when I go to the message handler (fnWndProc), the code I use to handle the message doesn't work for some reason. I don't know how to get my source code to show up in the window thingy, so I'll do the best I can... #include #include #define IDC_BTNOK 200 // The ok button #define IDC_BTNCANCEL 201 // The cancel button LRESULT CALLBACK fnWindowProcedure(HWND, UINT, LPARAM, WPARAM); char szAppName[] = "Experiment"; HINSTANCE g_hInstance; HWND g_hOk, g_hCancel = NULL; int WINAPI WinMain(HINSTANCE g_hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow){ WNDCLASSEX wcx; HWND g_hWnd; HMENU g_hMenu; MSG iMsg; wcx.cbSize = sizeof(wcx); wcx.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW; wcx.lpfnWndProc = (WNDPROC)fnWindowProcedure; wcx.cbClsExtra = 0; wcx.cbWndExtra = 0; wcx.hInstance = g_hInstance; wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wcx.hCursor = LoadCursor(NULL, IDI_APPLICATION); wcx.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); wcx.lpszMenuName = NULL; wcx.lpszClassName = szAppName; if(!RegisterClassEx(&wcx)){ MessageBox(g_hWnd, "Fatal Error! Could not register window!", "ERROR!", MB_OK); } g_hWnd = CreateWindow( szAppName, "Experimental Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, g_hInstance, NULL); g_hOk = CreateWindow( // Create the ok button "BUTTON", "OK", WS_VISIBLE | BS_PUSHBUTTON | WS_CHILD, 445, 395, 80, 25, g_hWnd, (HMENU)IDC_BTNOK, g_hInstance, NULL); g_hCancel = CreateWindow( // Create the cancel button "BUTTON", "Cancel", WS_VISIBLE | BS_PUSHBUTTON | WS_CHILD, 535, 395, 80, 25, g_hWnd, (HMENU)IDC_BTNCANCEL, g_hInstance, NULL); ShowWindow(g_hWnd, iCmdShow); UpdateWindow(g_hWnd); while(TRUE){ if(PeekMessage(&iMsg, NULL, 0, 0, PM_NOREMOVE)){ if(!GetMessage(&iMsg, NULL, 0, 0)) break; TranslateMessage(&iMsg); DispatchMessage(&iMsg); } } return(iMsg.wParam); } LRESULT CALLBACK fnWindowProcedure(HWND g_hWnd, UINT iMsg, LPARAM lParam, WPARAM wParam){ switch(iMsg){ case WM_COMMAND: switch(LOWORD(wParam)){ case IDC_BTNCANCEL: // where the cancel button messages go MessageBox(g_hWnd, "Cancel Button Clicked", "Cancel", MB_OK); break; } break; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CLOSE: DestroyWindow(g_hWnd); return 0; default: return DefWindowProc(g_hWnd, iMsg, lParam, wParam); } return 0; } A message box should show up when the cancel button is clicked, but nothing happens. I've tried everything I can think of to fix the problem, with no success. Any help would be appreciated. Thanks!
chxfryer
chxfryer
The "window" thing you are talking about can be used if you put your code between source tags [_source_] and [_/source_] (minus the _'s).

::fart::
GuitarPlayer0912
GuitarPlayer0912
Ok I'll try this again. Here's my WinMain.cpp

#include <windows.h>#include <stdio.h>#define IDC_BTNOK     200#define IDC_BTNCANCEL 201LRESULT CALLBACK fnWindowProcedure(HWND, UINT, LPARAM, WPARAM);char szAppName[] = "Experiment";HINSTANCE g_hInstance;HWND g_hOk, g_hCancel = NULL;int WINAPI WinMain(HINSTANCE g_hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine,  int iCmdShow){  WNDCLASSEX wcx;  HWND g_hWnd;  HMENU g_hMenu;  MSG iMsg;  wcx.cbSize = sizeof(wcx);  wcx.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;  wcx.lpfnWndProc = (WNDPROC)fnWindowProcedure;  wcx.cbClsExtra = 0;  wcx.cbWndExtra = 0;  wcx.hInstance = g_hInstance;  wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);  wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);  wcx.hCursor = LoadCursor(NULL, IDI_APPLICATION);  wcx.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);  wcx.lpszMenuName = NULL;  wcx.lpszClassName = szAppName;  if(!RegisterClassEx(&wcx)){    MessageBox(g_hWnd, "Fatal Error! Could not register window!", "ERROR!", MB_OK);  }  g_hWnd = CreateWindow(    szAppName,    "Experimental Application",    WS_OVERLAPPEDWINDOW,    CW_USEDEFAULT,    CW_USEDEFAULT,    640,    480,    NULL,    NULL,    g_hInstance,    NULL);  g_hOk = CreateWindow(    "BUTTON",    "OK",    WS_VISIBLE | BS_PUSHBUTTON | WS_CHILD,    445,    395,    80,    25,    g_hWnd,    (HMENU)IDC_BTNOK,    g_hInstance,    NULL);  g_hCancel = CreateWindow(    "BUTTON",    "Cancel",    WS_VISIBLE | BS_PUSHBUTTON | WS_CHILD,    535,    395,    80,    25,    g_hWnd,    (HMENU)IDC_BTNCANCEL,    g_hInstance,    NULL);  ShowWindow(g_hWnd, iCmdShow);  UpdateWindow(g_hWnd);  while(TRUE){    if(PeekMessage(&iMsg, NULL, 0, 0, PM_NOREMOVE)){      if(!GetMessage(&iMsg, NULL, 0, 0))        break;      TranslateMessage(&iMsg);      DispatchMessage(&iMsg);    }  }  return(iMsg.wParam);}LRESULT CALLBACK fnWindowProcedure(HWND g_hWnd, UINT iMsg, LPARAM lParam, WPARAM wParam){  switch(iMsg){    case WM_COMMAND:      switch(LOWORD(wParam)){        case IDC_BTNCANCEL:          MessageBox(g_hWnd, "Cancel Button Clicked", "Cancel", MB_OK);        break;      }    break;    case WM_DESTROY:      PostQuitMessage(0);      return 0;    case WM_CLOSE:      DestroyWindow(g_hWnd);      return 0;    default:      return DefWindowProc(g_hWnd, iMsg, lParam, wParam);  }  return 0;}


but I can't figure out why there aren't any line spaces... everything's crunched together.

Edit: Never mind, that just appeared in the preview
raz0r
raz0r
Your WindowProc's signature is wrong. It's (HWND, UINT, WPARAM, LPARAM), not the other way around.
Xiachunyi
Xiachunyi
Try using PM_REMOVE to remove the incoming messages from the queue after reading it since with PM_NOREMOVE, the initial message is stuck there and you seem to be only reading the initial.
GuitarPlayer0912
GuitarPlayer0912
Wow! Raz0r you're right. I can't believe I did that not 1, but 3 times in my code. I have never switched those two before in my life. I guess when I made the fnWndProc prototype, I switched them and stuck with it the entire time. Hmm, I guess that explains why I kept getting a conversion error on this line...
  wcx.lpfnWndProc = fnWndProc;


that had to be changed to

  wcx.lpfnWndProc = (WNDPROC)fnWndProc;


thanks for all your help guys, it works now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.