J
JigglyBit
Guest
Hi,
Rather long winded so if anyone has decent reading material they can refer me to on these topics that would be much appreciated as well!
I am having a bit of a conceptual issue trying to figure out how to identify a hWnd object passed to CALLBACK(). I have a series of buttons defined uniquely in InitInstance() in format HWND hwButton = CreateWindow(XXX), HWND hwButton2 = CreateWindow(YYY), etc. They work and populate parent window fine. However when I get in callback scope I am at a loss on how to identify which button object is passed. I have done a bit of reading on this and can't quite get to the root of this CALLBACK structure. Can someone please explain to me at the core what this function is doing? My interpretation is we pass a handle to this function containing window pointer. My first question is what is this pointer to? Is it the main parent window defined in the program? In my case hwnd = CreateWindow() defined after hInst? Or is it one of the button pointers in the subsequent definitions? I guess to summarize question #1 would be do we pass only the main project window instance to this function or do we pass arbitrary window creation instances to it as well?
Next question is kind of variable based on answer to first due to my lack of understanding. I am going to assume when I click a button it forces the button object (hwButton in my case) to be thrown at this function. I now have a pointer to that button object, a control message, and then two identifying parameters containing additional information. So we switch the control message and if its of type "command" then we switch the parameter containing the request identifier. If the identifier is of type button clicked we execute under that case header.
So if I am correct in my understanding, the act of clicking a button forces a callback of WndProcess(HWND pointer to button that was pressed memory location, UINT control message in this case associated with a command from the object, WPARAM containing message ID, LPARAM containing unknown to me).
Ultimately what I want to do is something like this (if (hWnd == hwButton)...obviously quite incorrect but just to illustrate point) and identify which button it was that was clicked so I can discern between actions. Right now any button clicked will lead to the execution of the same code snippet. My goal is to execute that PICWrite() statement on click of desired button.
//from InitInstance function definition
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700/*CW_USEDEFAULT*/, 550, nullptr, nullptr, hInstance, nullptr);
//HWND hwGraph = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 0/*CW_USEDEFAULT*/, 0, nullptr, nullptr, hInstance, nullptr);
HWND hwButton = CreateWindow(L"BUTTON", //pre-defined class type in unicode definition
L"Send A", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, //button attributes
10, //x position relative to parent frame
10, //y position relative to parent frame
110, //width
40, //height
hWnd, //handle to parent window
NULL, //window doesn't contain a menu
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
nullptr);
//skipping through the rest of that function to arrive at callback, there are more buttons defined in the same way
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
char c[20] = { 'c' };
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
int wmNotification = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case BN_CLICKED:
wParamValue = DWORD(lParam);
swprintf_s(wParamTextValueString, 10, L"%d", wParamValue);
//ReadFile();
if (hWnd == hwButton) {
PICWrite(c);
}
//MessageBox(NULL, _T("Test."), L"Hi.", NULL); //wParamTextValueString
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_RBUTTONDOWN:
{
MessageBox(NULL, _T("Right button clicked."), _T("Yep."), NULL);
break;
}
case WM_TIMER:
{
//skipping through rest of function
Continue reading...
Rather long winded so if anyone has decent reading material they can refer me to on these topics that would be much appreciated as well!
I am having a bit of a conceptual issue trying to figure out how to identify a hWnd object passed to CALLBACK(). I have a series of buttons defined uniquely in InitInstance() in format HWND hwButton = CreateWindow(XXX), HWND hwButton2 = CreateWindow(YYY), etc. They work and populate parent window fine. However when I get in callback scope I am at a loss on how to identify which button object is passed. I have done a bit of reading on this and can't quite get to the root of this CALLBACK structure. Can someone please explain to me at the core what this function is doing? My interpretation is we pass a handle to this function containing window pointer. My first question is what is this pointer to? Is it the main parent window defined in the program? In my case hwnd = CreateWindow() defined after hInst? Or is it one of the button pointers in the subsequent definitions? I guess to summarize question #1 would be do we pass only the main project window instance to this function or do we pass arbitrary window creation instances to it as well?
Next question is kind of variable based on answer to first due to my lack of understanding. I am going to assume when I click a button it forces the button object (hwButton in my case) to be thrown at this function. I now have a pointer to that button object, a control message, and then two identifying parameters containing additional information. So we switch the control message and if its of type "command" then we switch the parameter containing the request identifier. If the identifier is of type button clicked we execute under that case header.
So if I am correct in my understanding, the act of clicking a button forces a callback of WndProcess(HWND pointer to button that was pressed memory location, UINT control message in this case associated with a command from the object, WPARAM containing message ID, LPARAM containing unknown to me).
Ultimately what I want to do is something like this (if (hWnd == hwButton)...obviously quite incorrect but just to illustrate point) and identify which button it was that was clicked so I can discern between actions. Right now any button clicked will lead to the execution of the same code snippet. My goal is to execute that PICWrite() statement on click of desired button.
//from InitInstance function definition
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700/*CW_USEDEFAULT*/, 550, nullptr, nullptr, hInstance, nullptr);
//HWND hwGraph = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 0/*CW_USEDEFAULT*/, 0, nullptr, nullptr, hInstance, nullptr);
HWND hwButton = CreateWindow(L"BUTTON", //pre-defined class type in unicode definition
L"Send A", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, //button attributes
10, //x position relative to parent frame
10, //y position relative to parent frame
110, //width
40, //height
hWnd, //handle to parent window
NULL, //window doesn't contain a menu
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
nullptr);
//skipping through the rest of that function to arrive at callback, there are more buttons defined in the same way
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
char c[20] = { 'c' };
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
int wmNotification = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case BN_CLICKED:
wParamValue = DWORD(lParam);
swprintf_s(wParamTextValueString, 10, L"%d", wParamValue);
//ReadFile();
if (hWnd == hwButton) {
PICWrite(c);
}
//MessageBox(NULL, _T("Test."), L"Hi.", NULL); //wParamTextValueString
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_RBUTTONDOWN:
{
MessageBox(NULL, _T("Right button clicked."), _T("Yep."), NULL);
break;
}
case WM_TIMER:
{
//skipping through rest of function
Continue reading...