win32 window doesn't resize

Hello.

I'm having a bit of a problem with creating a win32 window: the window shows up fine, but can not be resized. Also, clicking on the minimize, maximize or close buttons does not do anything.

This is how I create the window:
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
m_hWnd = nullptr;
    m_hInstance = nullptr;

    // config
    m_bytes = 8; // color bit depth


    //GLuint		PixelFormat;						// Holds The Results After Searching For A Match
	WNDCLASS	wc;							// Windows Class Structure
	DWORD		dwExStyle;						// Window Extended Style
	DWORD		dwStyle;						// Window Style
	RECT WindowRect;							// Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left=(long)0;						// Set Left Value To 0
	WindowRect.right=(long)getWindowWidth();						// Set Right Value To Requested Width
	WindowRect.top=(long)0;							// Set Top Value To 0
	WindowRect.bottom=(long)getWindowHeight();						// Set Bottom Value To Requested Height

	m_hInstance		= GetModuleHandle(nullptr);			// Grab An Instance For Our Window
	wc.style		= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;		// Redraw On Move, And Own DC For Window
	wc.lpfnWndProc		= (WNDPROC) &WindowsWindow::WndProc;				// WndProc Handles Messages
	wc.cbClsExtra		= 0;						// No Extra Class Data
	wc.cbWndExtra		= 0;						// No Extra Window Data
	wc.hInstance		= m_hInstance;					// Set The Instance
	wc.hIcon		= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
	wc.hbrBackground	= nullptr;						// No Background Required For GL
	wc.lpszMenuName		= nullptr;						// We Don't Want A Menu
	wc.lpszClassName	= "CWWindowsWindow";					// Set The Class Name

    if (!RegisterClass(&wc))						// Attempt To Register The Window Class
	{
		MessageBox(nullptr,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		fail();
		return;
		//return FALSE;							// Exit And Return FALSE
	}

	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;	// Window Extended Style
    dwStyle=WS_OVERLAPPEDWINDOW;					// Windows Style

    AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size

    if (!(m_hWnd = CreateWindowEx(	dwExStyle,				// Extended Style For The Window
					"CWWindowsWindow\0",				// Class Name
					"CW\0",					// Window Title
					WS_CLIPSIBLINGS |			// Required Window Style
					WS_CLIPCHILDREN |			// Required Window Style
					dwStyle,				// Selected Window Style
					0, 0,					// Window Position
					WindowRect.right-WindowRect.left,	// Calculate Adjusted Window Width
					WindowRect.bottom-WindowRect.top,	// Calculate Adjusted Window Height
					nullptr,					// No Parent Window
					nullptr,					// No Menu
					m_hInstance,				// Instance
					this)))					// Pass self to WM_CREATE
	{
		
		MessageBox(nullptr,"Window Creation Error.\0","ERROR\0",MB_OK|MB_ICONEXCLAMATION);
		fail();
		return;
	}

	ShowWindow(m_hWnd,SW_SHOW);
	SetForegroundWindow(m_hWnd);
	SetFocus(m_hWnd);
	UpdateWindow(m_hWnd);

My message pump looks like this:
1
2
3
4
5
6
7
//message handling
    while(PeekMessage(&m_msg,nullptr,0,0,PM_REMOVE))
    {
        TranslateMessage(&m_msg);
        DispatchMessage(&m_msg);
    }

How can I get my window to resize?
Last edited on
In my wndproc, I handle WM_SIZE messages like this:

1
2
3
4
5
6
7
case WM_SIZE:							// Resize The OpenGL Window
		{
            // LoWord=Width, HiWord=Height
            ctxptr->getEventHandler()->queueEvent(new WindowResizeTriggeredEvent(LOWORD(lParam), HIWORD(lParam)));

			return DefWindowProc(p_hWnd,uMsg,wParam,lParam);						// Jump Back
		}


I still can not resize the window.
Last edited on
I can't seem to work this with a console application. The closet i could get to this was using a custom interface called: cInterface.windowSetting.cpp
This is an absolutely perfect example of the point I was trying to make 10 months ago here...

https://www.cplusplus.com/forum/windows/275786/

...with these verbose, convoluted, complicated 'beginners' templates that attempt to create a window, take hundreds of lines of convoluted code to do it, and in the end fail at creating a usable window, and leave the student/learner dis-satisfied and ready to try one of the Windows Application Development frameworks.

At the above link, in the 5th post, I present a minimal Windows SDK template comprising only 38 lines of correct code, and the created window WILL resize correctly.

This is a double edged ongoing problem.
if you open VS 2019 (I have not moved along yet) and start a new windows program in C++, you will get a 'start from here' program with global variables, incorrect constants (#defines), and tons of unnecessary gibberish (it takes up the better part of a page to generate an empty about box, which should be 1 line!)
so the bot generated code is of exceedingly low quality, and ...
nearly every example in the documentation is a trainwreck.
between the two, I salute the people that made big programs under the tools.
Last edited on

if you open VS 2019 (I have not moved along yet) and start a new windows program in C++, you will get a 'start from here' program with global variables, incorrect constants (#defines), and tons of unnecessary gibberish (it takes up the better part of a page to generate an empty about box, which should be 1 line!)
so the bot generated code is of exceedingly low quality, and ...
nearly every example in the documentation is a trainwreck.


Familiar with that. When I first saw it I thought I'd redo it right, just for kicks and giggles. Never got around to it. I thought it was just my cranky old nature as the reason I was so offended by it. Glad to see I'm not alone!
luckily you can create a template of your project with vs 2019 and newer so you can use it over and over again.
It is indeed retarded what ms comes up with, as if you would use that in professional programs.
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

#include <windows.h>

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WindowProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam);

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window";
    WNDCLASSEX wcex = { };
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WindowProc;
    wcex.cbClsExtra = NULL;
    wcex.cbWndExtra = NULL;
    wcex.hInstance = hInstance;
    wcex.hCursor = reinterpret_cast<HCURSOR>(LoadImage(0, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED));
    wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = CLASS_NAME;
    wcex.hIcon = reinterpret_cast<HICON>(LoadImage(0, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED));
    wcex.hIconSm = reinterpret_cast<HICON>(LoadImage(0, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED));
    //if (RegisterClassEx(&wcex) == NULL) { return 0; }
    if (!RegisterClassEx(&wcex)) return 0;

    // Create the window.
    HWND hwnd_Main = CreateWindowEx(
        WS_EX_CONTROLPARENT | WS_EX_WINDOWEDGE,                   // Optional window styles.
        CLASS_NAME,                                               // Window class
        L"C++ Window with a button",                              // Window text
        WS_VISIBLE | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,  // Window style
        200, 100, 1000, 640,                                      // Size and position
        NULL,                                                     // Parent window    
        NULL,                                                     // Menu
        hInstance,                                                // Instance handle
        NULL);                                                    // Additional application data

    // Dispatch Windows messages
    MSG msg = { };
    while (GetMessage(&msg, 0, 0, 0))
    {
        if (IsDialogMessage(hwnd_Main, &msg) == 0) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return 0;
}

LRESULT CALLBACK WindowProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{

    switch (uMsg)
    {
    case WM_CREATE:
        // create button                                                                                   
        ::CreateWindowEx(NULL, L"button", L"&Close", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 100, 100, 100, 100, hwnd, (HMENU)IDCANCEL, NULL, &lParam);
        return 0;

    case WM_COMMAND:

        switch (LOWORD(wParam))
        {
        case IDCANCEL:
            ::SendMessage(hwnd, WM_CLOSE, 0, 0);
            return 0;  //break;
        }

   case WM_SIZE:
    {
        int width = LOWORD(lParam);
        int height = HIWORD(lParam);
        ::MoveWindow(GetDlgItem(hwnd, IDCANCEL), width - 200, height - 80, 175, 50, TRUE);
    }
    return 0; 

    case WM_DESTROY:
        
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


And now the question arises for a beginner, how do I get this working in ms Visual studio?
Last edited on

LoadIcon and LoadCursor have been deprecated, use LoadImage instead.

OK I changed that in the code from the previous post

I don't program much with window gui anymore.
I was surprised that the code below still works
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

#include <windows.h>

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    // Create window
    HWND hWnd = CreateWindowEx(WS_EX_CONTROLPARENT | WS_EX_WINDOWEDGE, L"#32770", L"Hello", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0);

   // Dispatch Windows messages
    MSG uMsg = { };
while (GetMessage(&uMsg, 0, 0, 0))
{
   if (IsDialogMessage(hWnd, &uMsg) == 0) {
        TranslateMessage(&uMsg);
        DispatchMessage(&uMsg);
   }else {

       switch(uMsg.message)
        {

       case WM_COMMAND:

           switch (LOWORD(uMsg.wParam))
           {

           case WM_DESTROY:
               PostQuitMessage(0);
               return 0;
           }
       }
     }
    }
    return 0;
}
Topic archived. No new replies allowed.