stop win32 window to be minimized programatically - winapi

i want to stop my win32 window to be minimized programatically , so I did below but its not working. please let me know any other solution or if i am doing something wrong here.
for testing I am minimizing my window after 2 sec& trying to catch that that and maximize my window again. but its not working as expected.
below is the code.
case WM_SYSCOMMAND:
if ((wParam & 0xFFF0) == SC_MINIMIZE)
{
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
UpdateWindow(hWnd);
}
else
DefWindowProc(hWnd, message, wParam, lParam);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//show & hide for every 2sec
VOID CALLBACK timerelapsed(HWND hWnd, UINT, UINT_PTR, DWORD)
{
ShowWindow(hWnd, SW_SHOWMINIMIZED);
}

Read the documentation for WM_WINDOWPOSCHANGING. In particular:
An application can prevent changes to the window by setting or clearing the appropriate bits in the flags member of WINDOWPOS.

Related

Is WM_CHAR message possible with a DialogBox() modal dialog?

I've been searching for the answer for hours, and a common solution is to respond to a WM_GETDLGCODE message, but I never get that message to respond to.
My example code is:
#include <windows.h>
#include "resource.h"
static LRESULT CALLBACK DlgProc(HWND hDlg, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (wParam) {
case IDOK:
case IDCANCEL:
EndDialog(hDlg, 0);
return TRUE;
}
break;
case WM_GETDLGCODE:
MessageBox(hDlg, "Got WM_GETDLGCODE", "DlgProc", MB_OK);
return DLGC_WANTCHARS;
case WM_CHAR:
MessageBox(hDlg, "Got WM_CHAR", "DlgProc", MB_OK);
break;
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szcmdline, int iCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),
NULL, DlgProc);
return 0;
}
The dialog is a single edit box (1st tab stop) with the Ok and Cancel buttons. The problem is, I get neither the WM_GETDLGCODE nor the WM_CHAR message. Before I continue down this path, I just want to know if this should work or not. Hooks is my next choice, but if WM_CHAR is available, it seemed like a less complicated solution if I want to keep the dialog modal.

Handling WinApi edit control with subclass of GLFW

I'm a newbie in winapi handling and opengl rendering. I try to use GLFW window to render my graphics and handling messages from WinApi windows. For handle messages from winapi i am create a subclass of GLFW window and replace wndproc function like this:
window = glfwCreateWindow(WIDTH, HEIGHT, "Window", nullptr, nullptr);
GLWINDOW = window;
HWND hWnd = glfwGetWin32Window(window);
SetParent(hWnd, mainWindow);
LONG nNewStyle = GetWindowLong(hWnd, GWL_STYLE) & ~WS_POPUP | WS_CHILDWINDOW;
SetWindowLong(hWnd, GWL_STYLE, nNewStyle);
ULONG_PTR cNewStyle = GetClassLongPtr(hWnd, GCL_STYLE) | CS_DBLCLKS;
SetClassLongPtr(hWnd, GCL_STYLE, cNewStyle);
SetWindowSubclass(hWnd, &msgProc, 0, 0);
glfwShowWindow(window);
Then i create my buttons and wndproc function. That work well, i can process messages from this buttons and click on it. But when i create "edit control", it doesn't react on my mouse clicks and key inputs. What do I need to do to make it work?
My wndproc:
LRESULT CALLBACK msgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
PAINTSTRUCT PS;
switch(message)
{
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
BeginPaint(hWnd, &PS);
EndPaint(hWnd, &PS);
break;
case WM_COMMAND:
if(LOWORD(wParam)==1)
glEnabled = true;
if(LOWORD(wParam)==2)
{
printf("system: application terminated \n");
terminate = true;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
The problem is your WM_PAINT routine. You don't let the WM_PAINT to be handled by the edit control.
You gave the edit control no chance to display anything. Subclassing means: Just do that what is neccesary. Let the rest be handled by the control. Also calling DefWindowProc is wrong for subclassing. You have to use
return CallWindowProc(wndprocOrig, hwnd, wm, wParam, lParam);
If this windowproc is used for the edit control it wonders me, that you handle WM_COMMAND message in it. WM_COMMAND messages are not sent to the control. They are sent to the parent window. Also there is a break missing.
Please read the MSDN for further details.
Advice for the future: As you are new to SO, please offer as much information and code as possible when you ask a question.

Using KBDLLHOOKSTRUCT to determine first key press

I'm using a low-level keyboard hook on windows. It works like a charme, despite of the fact that I'm currently unable to tell whether the key was initially pressed or pressed again. The documentation (+here) says, that bit 7 holds the transition state. But this seems only to be true when the key is being released. Bit 7 is sadly not set when I firstly press the key.
Is there any way to tell whether the key is pressed initially?
I happen to run into this problem recently. I can't find any good solutions, but I ended up using a flag and a GetAsyncKeyState before SetWindowHookEx.
BOOL wasDown;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
LPKBDLLHOOKSTRUCT key = (LPKBDLLHOOKSTRUCT) lParam;
if (key->vkCode == VK_SOMETHING) {
switch (wParam) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (!wasDown) {
// Processing on first key down
wasDown = true;
}
break;
case WM_KEYUP:
case WM_SYSKEYUP:
// Processing on key up
wasDown = FALSE;
break;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
wasDown = GetAsyncKeyState(VK_SOMETHING) < 0;
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0);
Of course, this code only does it for one key. You can use an array of flags to do multiple keys. Depending on your application you could also unconditionally set the flag to false if you want the first press after your hook is set.

C++ Win32 PostQuitMessage(0) not working? CPU # 100%

OK so I have my main window procedure (simplified):
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
if(!loadFiles())
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
loadFiles() is a function to read some text files. If the files are missing, it posts an error message and then returns false. I handle this by sending the WM_CLOSE message, which sends WM_DESTROY? The program calls PostQuitMessage(0) and.. nothing. The process is still sitting in background, now using 100% CPU on core 1. Here is my message loop :
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
Very simple, I've looked around and seen people using all kinds of different loops, most with PeekMessage() function.. its worth noting I have several child windows containing different controls, I show and hide them when the user selects different tabs, but I don't think it matters because PostQuitMessage(0) is supposed to post WM_QUIT to the message queue, which destroys the child windows aswell right? Any help? Thanks
According to MSDN WM_CREATE is sent during CreateWindow. So you are destroying the window right during creation. I am not sure that this is supposed to work. Also, why so complicated? The documentation says
If an application processes this message, it should return zero to
continue creation of the window. If the application returns –1, the
window is destroyed and the CreateWindowEx or CreateWindow function
returns a NULL handle.
So just return -1 in case loadFiles() fails and handle CreateWindow returning NULL accordingly.

Execute code after a window is displayed

I'm working on a windows application where I'm implementing the whole event loop and everything like that myself (there's a reason for that). In one place, I need to execute some code AFTER a window is shown. Normally, when the window is created, I do some initialisation when WM_CREATE message is received. WM_SHOWWINDOW is sent just BEFORE the window is displayed. However I need to get some code executed right AFTER the window is displayed for the first time. I can't seem to find a notification message sent AFTER the window is shown. Could it be that there isn't one?
Of course, I can keep a boolean - FirstRun - indicating whether or not I had performed my logic, and then execute the code when WM_ACTIVATE is received, provided the boolean is TRUE, then set FirstRun to FALSE so that the code is not execute the next time I am receive WM_ACTIVATE, but this seems somewhat unnatural to me.
It's been ages since I did win32 programming on this level, so can't remember much of it. What is the best approach here?
There is no special notification, but in many cases you can use this trick:
LRESULT CALLBACK MainWndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam) // second message parameter
{
switch (uMsg)
{
case WM_USER + 100:
//window is just displayed, do some actions
return DefWindowProc(hwnd, uMsg, wParam, lParam);
case WM_CREATE:
PostMessage(hwnd, WM_USER + 100, 0, 0);
return DefWindowProc(hwnd, uMsg, wParam, lParam);
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
There isn't one because that's what WM_SHOWWINDOW is for. Once that mesage is passed to the default message handling procedure, the window will be shown. The best thing you could do is poll with IsWindowVisible via some sort of timer.
Your program design seems flawed though to have to rely on something like this. What are you trying to do?
As long as you are implementing the whole event loop and everything like that yourself, you can handle this directly in WinMain() like this:
HWND hWnd = CreateWindow(...);
if (!hWnd) return 0;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
OnWindowJustDisplayed(); // here
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

Resources