Win32 Buttons, which is which? [solved]
-
Hello everyone, I've been experimenting with C++ and how to build a form with it (without MFC). I've created two buttons at runtime with the following code: (I used a bit of copying and pasting)
if (button1 == NULL)
{
button1 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button1", WS_CHILD ,50,50,50,50,parent,NULL,::GetModuleHandle(NULL),NULL);
SendMessage(button1, WM_SETFONT, (WPARAM)defFont, MAKELPARAM(TRUE, 0));
::ShowWindow(button1,SW_SHOW);
}
if (button2 == NULL)
{
button2 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button2", WS_CHILD ,125,50,50,50,parent,NULL,::GetModuleHandle(NULL),NULL);
SendMessage(button2, WM_SETFONT, (WPARAM)defFont, MAKELPARAM(TRUE, 0));
::ShowWindow(button2,SW_SHOW);}
There's no problem with building the buttons, this is my WndProc:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case SW_SHOW:
break;
case WM_COMMAND:
if (wParam == BN_CLICKED)
{
Triggered(L"1"); //Shows a messagebox.
}
break;
case WM_SHOWWINDOW:
Init(hwnd); //Builds the buttons here.
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;}
How can I tell which one sent the message? -- Nevermind, got it. lParam equals with the (LPARAM)handle of the button that sent it.
modified on Tuesday, June 7, 2011 3:05 PM
-
Hello everyone, I've been experimenting with C++ and how to build a form with it (without MFC). I've created two buttons at runtime with the following code: (I used a bit of copying and pasting)
if (button1 == NULL)
{
button1 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button1", WS_CHILD ,50,50,50,50,parent,NULL,::GetModuleHandle(NULL),NULL);
SendMessage(button1, WM_SETFONT, (WPARAM)defFont, MAKELPARAM(TRUE, 0));
::ShowWindow(button1,SW_SHOW);
}
if (button2 == NULL)
{
button2 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button2", WS_CHILD ,125,50,50,50,parent,NULL,::GetModuleHandle(NULL),NULL);
SendMessage(button2, WM_SETFONT, (WPARAM)defFont, MAKELPARAM(TRUE, 0));
::ShowWindow(button2,SW_SHOW);}
There's no problem with building the buttons, this is my WndProc:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case SW_SHOW:
break;
case WM_COMMAND:
if (wParam == BN_CLICKED)
{
Triggered(L"1"); //Shows a messagebox.
}
break;
case WM_SHOWWINDOW:
Init(hwnd); //Builds the buttons here.
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;}
How can I tell which one sent the message? -- Nevermind, got it. lParam equals with the (LPARAM)handle of the button that sent it.
modified on Tuesday, June 7, 2011 3:05 PM
When you call CreateWindowEx, the return is a handle to the button, that same handle can be used within the WPARAM or LPARAM to distinguish, or alternatively, if you have two button and you must distinguish, then they should probably be sending different messages.
-
Hello everyone, I've been experimenting with C++ and how to build a form with it (without MFC). I've created two buttons at runtime with the following code: (I used a bit of copying and pasting)
if (button1 == NULL)
{
button1 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button1", WS_CHILD ,50,50,50,50,parent,NULL,::GetModuleHandle(NULL),NULL);
SendMessage(button1, WM_SETFONT, (WPARAM)defFont, MAKELPARAM(TRUE, 0));
::ShowWindow(button1,SW_SHOW);
}
if (button2 == NULL)
{
button2 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button2", WS_CHILD ,125,50,50,50,parent,NULL,::GetModuleHandle(NULL),NULL);
SendMessage(button2, WM_SETFONT, (WPARAM)defFont, MAKELPARAM(TRUE, 0));
::ShowWindow(button2,SW_SHOW);}
There's no problem with building the buttons, this is my WndProc:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case SW_SHOW:
break;
case WM_COMMAND:
if (wParam == BN_CLICKED)
{
Triggered(L"1"); //Shows a messagebox.
}
break;
case WM_SHOWWINDOW:
Init(hwnd); //Builds the buttons here.
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;}
How can I tell which one sent the message? -- Nevermind, got it. lParam equals with the (LPARAM)handle of the button that sent it.
modified on Tuesday, June 7, 2011 3:05 PM
Another way is to create button window with id. button1 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button1", WS_CHILD ,50,50,50,50,parent,(HMENU)100,::GetModuleHandle(NULL),NULL); button2 = ::CreateWindowEx(WS_EX_LEFT,L"button",L"Button2", WS_CHILD ,125,50,50,50,parent,(HMENU)200,::GetModuleHandle(NULL),NULL); Now you can distinguish which is which. case WM_COMMAND: if ((UINT)HIWORD(wParam) == BN_CLICKED) { switch (int)LOWORD(wParam): { case 100: Triggered(L"1"); //Shows a messagebox. break; case 200: Triggered(L"2"); //Shows a messagebox. break; } } break;