Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Windows API

Tags:

c++

winapi

I've been trying to get a basic program going with the windows API (WinMain and WndProc) and have completed 4 tutorials now, all of which say the same thing. I create the two vital functions previously mentioned but when I compile and run no window is shown.

I get no errors or crashes, the program runs fine it's just the window that should but doesn't appear.

Any help would be great, I've tried using a Win32 Console project setup, a Win32 project setup and an Empty Project setup in VS2010.

Thanks.

EDIT: Apologies, here is the code I am using to set up and show the window:

WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(WNDCLASS));

wcex.cbSize = sizeof(WNDCLASS);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = "MyWindowClass";
wcex.hIconSm = 0;

RegisterClassEx(&wcex);

HWND hWnd = CreateWindowEx(NULL, "MyWindowClass",       // Name of window class
                         "Window Name",         // Title of window
                         WS_OVERLAPPEDWINDOW,   // Window style
                         300, 500,              // x,y position of window
                         800, 600,              // w,h of window
                         NULL,                  // Parent window
                         NULL,                  // Menus
                         hInstance,             // Application handle
                         NULL);                 // Multiple windows

ShowWindow(hWnd, nCmdShow);
like image 512
Simon McArdle Avatar asked Apr 08 '26 00:04

Simon McArdle


2 Answers

 wcex.lpfnWndProc = (WNDPROC)WndProc;

That's a very fishy cast, it should never be necessary. This problem is otherwise explained by a borked window procedure. You didn't post it. Start by deleting the cast and solve any compile error you get. Make sure that it always calls DefWindowProc() for messages you don't process yourself.

Consider using the boilerplate code you get from selecting the Win32 Project project template to get these details right.

like image 110
Hans Passant Avatar answered Apr 11 '26 02:04

Hans Passant


ZeroMemory(&wcex, sizeof(WNDCLASS));

wcex.cbSize = sizeof(WNDCLASS)

The above parameters should have been WNDCLASSEX to match the WNDCLASSEX structure defined just above them.

like image 43
Simon McArdle Avatar answered Apr 11 '26 02:04

Simon McArdle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!