C++ and Win32 WC_DIALOG close event problem - c++

my English is bad. sorry
HWND DIALOG_0 = CreateWindowEx(0, WC_DIALOG, "Security Alert", DS_SETFONT | WS_OVERLAPPEDWINDOW | WS_VISIBLE, 600,300,300,200,Win.hwnd_0,NULL,NULL,NULL);
(WNDPROC)SetWindowLongPtr(DIALOG_0 , GWLP_WNDPROC, (INT_PTR)dede);
ShowWindow (DIALOG_0, SW_SHOW);
UpdateWindow(DIALOG_0);
How do I do the shut down event.
How do I make HWND click events
I use it, but it gets locked up.
(WNDPROC)SetWindowLongPtr(DIALOG_0 , GWLP_WNDPROC, (INT_PTR)dede);
WC_DIALOG create dede function
LONG_PTR __stdcall Win32::dede(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CTLCOLORDLG:
return (INT_PTR)GetStockObject(HOLLOW_BRUSH);
break;
default:
DefWindowProc(hDlg, message, wParam, lParam);
break;
}
return (INT_PTR)FALSE;
}

You usually create dialogs with DialogBox or CreateDialog, not with CreateWindow. If you use CreateWindow then you cannot use the DS_ styles.
GWLP_WNDPROC replaces the original window procedure and you normally should call CallWindowProc, not DefWindowProc. If you don't do this then you don't get the default IDCANCEL handling and all the other things a dialog usually provides.
Catch WM_CLOSE and WM_COMMAND to handle close and click events.
MSDN has a dialog guide here.

Related

Change WndProc of the window

I try to change standart WndProc function. I have this code:
HWND btn = CreateWindowEx(WS_EX_TRANSPARENT | WS_EX_CLIENTEDGE, L"BUTTON", L"Window title", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON
, 50, 50, 50, 50, (HWND)XApplicationMainWindow->window->_wnd, (HMENU)123,
(HINSTANCE)GetWindowLongPtr(XApplicationMainWindow->window->_wnd, GWLP_HINSTANCE), NULL);
SetWindowLongPtrW(btn, GWLP_WNDPROC, (LONG_PTR)SubclassWindowProc);
I can use L"BUTTON" class name, but when I change WndProc function I'll have a problem.
On this picture, you can see the blank square and normal button. If I try to create new WNDCLASS or WNDCLASSEX, I'll have nothing... Why?
How can I change the standart WndProc function, if I use L"BUTTON" class name?
It's my second WndProc:
LRESULT CALLBACK SubclassWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE:
break;
case WM_COMMAND:
//Event click
switch (LOWORD(wParam))
{
case 123:
OutputDebugStringA("Subclass click2");
break;
default:
break;
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
DefWindowProc() is the wrong window procedure for your SubclassWindowProc() to be calling.
You need to call the previous window procedure that you are replacing - the window procedure that handles all of the button's default behaviors (like drawing the button so it actually looks like a button, and responding to user input like a button, etc). SetWindowLongPtr() returns a pointer to that procedure to you, but you are currently ignoring it.
See Subclassing Controls on MSDN for more details.
Try this instead:
WNDPROC btnWndProc;
LRESULT CALLBACK SubclassWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_COMMAND:
//Event click
switch (LOWORD(wParam))
{
case 123:
OutputDebugStringA("Subclass click2");
break;
}
break;
}
return CallWindowProc(hWnd, btnWndProc, uMsg, wParam, lParam);
}
...
HWND btn = CreateWindowEx(...);
btnWndProc = (WNDPROC) SetWindowLongPtrW(btn, GWLP_WNDPROC, (LONG_PTR)SubclassWindowProc);
Alternatively, using SetWindowSubclass(), which is safer than using SetWindowsLongPtr(), eg:
LRESULT CALLBACK SubclassWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
switch (uMsg) {
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, SubclassWindowProc, uIdSubclass);
break;
case WM_COMMAND:
//Event click
switch (LOWORD(wParam))
{
case 123:
OutputDebugStringA("Subclass click2");
break;
}
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
...
HWND btn = CreateWindowEx(...);
SetWindowSubclass(btn, SubclassWindowProc, 1, 0);
Now, that being said, your subclass will never call OutputDebugStringA(), because it will never receive the WM_COMMAND message you are expecting. When a button is clicked, a WM_COMMAND message is not sent to the button itself. The button posts a WM_COMMAND message to the button's parent window instead (in this case, to XApplicationMainWindow->window->_wnd). So, you need to handle the WM_COMMAND message in the window procedure of the parent window, not in the window procedure of the button itself.
Otherwise, if you still want to subclass the button itself, you will have to handle the WM_LBUTTON(DOWN|UP) and WM_KEY(DOWN|UP)/WM_CHAR messages that the button receives and then subsequently translates into a WM_COMMAND message for its parent window.

How can I close a child window with WM_CLOSE?

I create the child hwnd as popup with close button and WndchildProc for handle message from child window
m_childHwnd = CreateWindowEx(
NULL,
TEXT("STATIC"), TEXT("childW"),
WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
m_parentHWnd, NULL,
GetModuleHandle(NULL), NULL
);
m_childhProc = (WNDPROC)SetWindowLongPtr(m_childHwnd , GWLP_WNDPROC, (LONG_PTR)WndChildProc);
static LRESULT CALLBACK WndChildProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
default:
return CallWindowProc(m_childhProc, hwnd, message, wParam, lParam);
}
I expected when I press close button of child window the message WM_CLOSE will trigger. And I can close child hwnd with this way.
But when close button of child hwnd pressed, nothing happen. If close button of parent-hwnd pressed, WM_DESTROY message trigger.
So, I can't close (just) the child window with its close button.
How can I close a child window with its close button?
Updated: missing text in copying, added: CallWindowProc for default case.
I found solution with event WM_NCLBUTTONUP trigger when close button clicked.
case WM_NCLBUTTONUP:
ShowWindow(hwnd, SW_HIDE);
return 0;

c++ win32 api notify mouse inside button area

I've a Button made using Win32Api that I want to be able to notify whenever the user put the mouse inside the button rectangle.
I noticed that when user does that the WM_NOTIFY is called but I don't know which flag to use for ensure that the user has the mouse inside it's area.
Here is my button:
HWND Button = CreateWindow("BUTTON", "Test",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_NOTIFY,
20, 240, 120, 20,
hwnd, (HMENU)101, NULL, NULL);
And my WndProc:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_NOTIFY:
{
//??? Here is where I want to do it
}
case WM_CREATE: //On Window Create
{
wHWND = hwnd;
if (onCreate != NULL)
onCreate(hwnd);
break;
}
case WM_COMMAND: //Command execution
{
//...
break;
}
case WM_DESTROY: //Form Destroyed
{
if (onDestroy != NULL)
onDestroy(hwnd);
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
I don't know if I'm in the right path using WM_NOTIFY or not, maybe there is an easier way to do that. Thank you!
Try catching the WM_SETFOCUS message instead if you want a generic way of tracking focus.
BN_SETFOCUS is sent as WM_COMMAND in the upper 16 bits of WPARAM (HIWORD):
The parent window of the button receives this notification code through the WM_COMMAND message
If you are not talking about focus but just the mouse position then you can use ChildWindowFromPointEx or RealChildWindowFromPoint and a timer.

Win32: C++: How do I re-focus on Parent Window after clicking in a child window?

In my Win32 CPP program, I have defined some Child Window to display various text strings using something like:
hnd_to_this_ch_window = CreateWindow(
L"EDIT",L"Some initial text", WS_VISIBLE | WS_CHILD | ES_LEFT,
position_of_this_window_X,
position_of_this_window_Y,
TEXTOUT_DEFAULT_WIDTH,
TEXTOUT_DEFAULT_HEIGHT,
handle_to_my_parent_window, NULL,
hinstance_variable_used_by_create_window,
NULL )
My problem is that if I click with my mouse to select the text in one of such child windows (to, say, copy it somewhere), the focus of the application goes to this child window and so any keypresses which used to be handled through my main windows CALLBACK (with case WM_KEYDOWN:) are now captured into the child window, where they appear as inputted characters. What magic function do I call to have the focus go back to the parent (so that my WM_KEYDOWN) can work again? I was hoping I could just click on the main Window's title bar and that would take it back to normal, but that isn't working (because, obviously, my program is lacking some extra logic).
Handle the WM_KILLFOCUS message in the window procedure of the window you want to focus, and restore the focus using the SetFocus function.
If you want to focus the window when it is clicked, handle the WM_LBUTTONDOWN message.
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
// Restore the focus when it was lost.
if (Msg == WM_KILLFOCUS) {
SetFocus(hWnd);
// Msg was handled, return zero.
return 0;
}
// Or when the window is clicked.
if (Msg == WM_LBUTTONDOWN) {
SetFocus(hWnd);
// Msg was handled, return zero.
return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
case WM_KEYDOWN:
SetFocus(Parent_Hwnd);
return SendMessage(Parent_Hwnd,WM_KEYDOWN,wParam,lParam);

Window handle doesn't save correctly

I don't really get how to use HWND in c++.
I want to press a button and it should start a thread with a code running.
But I never receive the command for button click in an other callback.
So I did some debugging and it seems like that _wndInstance->GetWndHWND() returns something not valid. The method returns a private field which has it stored.
If you look a case WM_CREATE, the window content added will not show up with _wndInstance->GetWndHWND(). But if I just use hwnd from the parameters, it does work. But how is that possible if my first test-check validates that they are the same??
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (_wndInstance->GetWndHWND() == hwnd)
cout << "same" << endl; // Code is getting here!
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
_wndInstance->CreateWndContent(_wndInstance->GetWndHWND()); // not working, but hwnd is!
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
EDIT:
_wndInstance is an instance of a mainwindow class I wrote.
Part of mainWindow header:
private:
HWND _wndHwnd;
public:
HWND GetWndHWND();
MainWindow cpp:
HWND MainWindow::GetWndHWND()
{
return _wndHwnd;
}
_wndHwnd is set in a private method which creates the window:
_wndHwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"\"Xbox controller on WINDOWS\" Manager",
WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, // no maximize box
CW_USEDEFAULT, CW_USEDEFAULT, 450, 370,
NULL, NULL, hinstance, NULL);
if (_wndHwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return;
}
ShowWindow(_wndHwnd, nCmdShow);
UpdateWindow(_wndHwnd);
WM_CREATE is sent before CreateWindowEx() returns, and thus before your assignment happens. You will need to restructure your code so that the HWND is only used after it is assigned to _wndHwnd.
If you don't need to do anything between WM_CREATE and the beginning of the message loop, you can just drop your WM_CREATE handler. That depends on how your code will work in the future.
But a safer approach would be to assign _wndHwnd in your WM_CREATE handler (or even WM_NCCREATE), since you have it available as the hwnd parameter to your window procedure, and it would handle other messages sent between the window creation and the variable assignment. You can even pass _wndInstance as the last parameter to CreateWindowEx() and access it from WM_CREATE; see this for details.