Handmade Network»Forums
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Issues with PeekMessage --- Win32 programming
I have an issue with the busy icon cursor (the spinning circle)showing up when launching app . But when i minimize or maximize the app (not resize) the busy icon goes away.Anyways how to fix it.

Here's the WinMain code,


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
int CALLBACK WinMain(HINSTANCE Instance,
	HINSTANCE hPrevInstance,
	LPSTR     lpCmdLine,
	int       nCmdShow) {

	WNDCLASS wc;
	ATOM atom;
	RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
	DWORD style = WS_OVERLAPPEDWINDOW  ;
	DWORD exstyle = WS_EX_APPWINDOW  | WS_EX_LAYERED;
	HWND window;
	HDC dc;

	/* Win32 */
	memset(&wc, 0, sizeof(wc));
	wc.lpfnWndProc = Win32MainWindowCallback;
	wc.hInstance = Instance;
	wc.hCursor = LoadCursor(NULL,MAKEINTRESOURCE(IDC_POINTER));
	wc.lpszClassName = "WindowClass";
	wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
	wc.hIcon = LoadIcon(Instance, MAKEINTRESOURCE(IDI_ICON1)); // Make sure to pass instance of app
	atom = RegisterClass(&wc);
	AdjustWindowRectEx(&rect, style, FALSE, exstyle);
	window = CreateWindowEx(exstyle, wc.lpszClassName, "Win32 programming",
		style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
		rect.right - rect.left, rect.bottom - rect.top,
		NULL, NULL, wc.hInstance, NULL);
	
	int aElements[2] = { COLOR_WINDOW, COLOR_ACTIVECAPTION };
	DWORD aOldColors[2];
	DWORD aNewColors[2];

	aOldColors[0] = GetSysColor(aElements[0]);
	aOldColors[1] = GetSysColor(aElements[1]);
	aNewColors[0] = RGB(0x80, 0x80, 0x80);  // light gray 
	aNewColors[1] = RGB(0x80, 0x00, 0x80);  // dark purple 

	SetSysColors(2, aElements, aNewColors);
	dc = GetDC(window);
	if (window) {
		ShowWindow(window, SW_SHOWDEFAULT);
		Win32ResizeDIBSection(&GlobalBackBuffer, 1920, 1080);
		memset(GlobalBackBuffer.Memory, 60, GlobalBackBuffer.BytesPerPixel*GlobalBackBuffer.Height*GlobalBackBuffer.Width);
		while (GlobalRunning)
		{
			MSG Message;
			while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) {
				if (Message.message == WM_QUIT) {
					GlobalRunning = false;
				}

				switch (Message.message) {
				case WM_SYSKEYDOWN:
				case WM_SYSKEYUP:
				case WM_KEYDOWN:
				case WM_KEYUP: {
					uint32 VKCode = (uint32)Message.wParam;

					// NOTE: Since we are comparing WasDown to IsDown,
					// we MUST use == or != to convert these bit tests to actual
					// 0 or 1 values.
					bool WasDown = ((Message.lParam & (1 << 30)) != 0);
					bool IsDown = ((Message.lParam & (1 << 31)) == 0);

					if (WasDown != IsDown) {
						if (VKCode == 'W') {

						}
						else if (VKCode == 'A') {

						}
						else if (VKCode == 'S') {

						}
						else if (VKCode == 'D') {

						}
						else if (VKCode == 'Q') {

						}
						else if (VKCode == 'E') {

						}
						else if (VKCode == VK_UP) {

						}
						else if (VKCode == VK_LEFT) {

						}
						else if (VKCode == VK_RIGHT) {

						}
						else if (VKCode == VK_DOWN) {

						}
						else if (VKCode == VK_ESCAPE) {

							GlobalRunning = false;
						}
						else if (VKCode == VK_SPACE) {

						}

						if (IsDown) {
							bool32 AltKeyWasDown = (Message.lParam & (1 << 29));
							if ((VKCode == VK_F4) && AltKeyWasDown) {
								GlobalRunning = false;
							}

							if ((VKCode == VK_RETURN) && AltKeyWasDown) {
								if (Message.hwnd) {
									ToggleFullscreen(Message.hwnd);
								}
							}
						}
					}
				} break;
				default: {
					TranslateMessage(&Message);
					DispatchMessage(&Message);
				}

				}

			}

		}

		SetSysColors(2, aElements, aOldColors);
		ReleaseDC(window, dc);
	}
	UnregisterClass(wc.lpszClassName, wc.hInstance);
	return 0;

}
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Issues with PeekMessage --- Win32 programming
Fixed i guess it was a typo(handling WM_SETCURSOR) sorry about that.

The current issue is how to use win32 dialog boxes while realtime rendering (uisng peekmessage).
Mārtiņš Možeiko
2562 posts / 2 projects
Issues with PeekMessage --- Win32 programming
If you talk about your own dialog boxes, then you can do that with CreateDialog function. It simple creates separate window which still depends on your own message loop. Don't use DialogBox which implements its own message loop and does not return until closed.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Issues with PeekMessage --- Win32 programming
It works. But I can't tab between buttons. And the button is outlined in blue.
Do I have to do something in the message queue.
511 posts
Issues with PeekMessage --- Win32 programming
msmshazan
It works. But I can't tab between buttons. And the button is outlined in blue.
Do I have to do something in the message queue.


you are intercepting the keyboard events so it never reaches the code for the dialog box, if the hwnd doesn't match the window you created just translate and dispatch normally.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Issues with PeekMessage --- Win32 programming
@ratchefreak I can't understand what are you telling.

Here's the code to app.

Tell what to fix.
Mārtiņš Možeiko
2562 posts / 2 projects
Issues with PeekMessage --- Win32 programming
Edited by Mārtiņš Možeiko on
Have you seen documentation for CreateDialog? https://msdn.microsoft.com/en-us/...ary/windows/desktop/ms645434.aspx
It has link to example code (Creating a Modeless Dialog Box).

There it shows and explains how to write message loop that uses dialog boxes. In pseudocode:
1
2
3
4
5
6
7
while (GetNextMessage())
{
  if (IsDialogMessage())
  {
    ProcessMessage();
  }
}


But your code looks like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
while (GetNextMessage())
{
  if (IsMessageFromKeyboardEvent())
  {
     // process keyboard message
  } 
  else
  {
    if (IsDialogMessage())
    {
      ProcessMessage();
    }
  }
}


Do you see how keyboard messages get processed with your code and not passed to IsDialogMessage function? That's why they don't get to dialog. This is what ratchetfreak was saying.

See the documentation for IsDialogMessage function, which says:
When IsDialogMessage processes a message, it checks for keyboard messages and converts them into selections for the corresponding dialog box. For example, the TAB key, when pressed, selects the next control or group of controls, and the DOWN ARROW key, when pressed, selects the next control in a group.


Also, btw your function signature for AboutDlgProc function is wrong. See DialogProc in MSDN what it should be.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Issues with PeekMessage --- Win32 programming
So how to handle dialog box keyboard inputs manually because i need handle key inputs to get key press info.
Mārtiņš Možeiko
2562 posts / 2 projects
Issues with PeekMessage --- Win32 programming
First get whatever information you need from key messages in your code. And then pass it to IsDialogMessage, don't skip call to it.
Of course that means that pressing TAB key will do something in your game/application and also in dialog box at the same time. If that's what you want.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Issues with PeekMessage --- Win32 programming
Thanks mmozeiko, it worked i changed my code from this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
while (GetNextMessage())
{
  if (IsMessageFromKeyboardEvent())
  {
     // process keyboard message
  } 
  else
  {
    if (IsDialogMessage())
    {
      ProcessMessage();
    }
  }
}


to this
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
while (GetNextMessage())
{
if (IsDialogMessage())
    {
  if (IsMessageFromKeyboardEvent())
  {
     // process keyboard message
  } 
  else
  {   
      ProcessMessage();
  }
}
}