Sign in to follow this  

Guys what am i doing wrong? (C++)

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i wrote this code, according to a book i got, it compiles alright with no problem but when i double click it to run it loads up since i see it in the task manager's processes but the acctual window does not show up
#include <windows.h>
LRESULT CALLBACK WndProc (HWND hWindow, UINT iMessage,
                           WPARAM wParam, LPARAM lPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst,
                       LPSTR lpCmdLine, int nShowCMD)
{
 WNDCLASS kWndClass;

kWndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
kWndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
kWndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

kWndClass.hInstance = hInstance;
kWndClass.lpfnWndProc = WndProc;
kWndClass.lpszClassName = "Hello World";

kWndClass.lpszMenuName = NULL;

kWndClass.cbClsExtra = NULL;
kWndClass.cbWndExtra = NULL;
kWndClass.style = NULL;

if (!RegisterClass (&kWndClass))
{
 return -1;
}

HWND hWindow;
hWindow = CreateWindow ("Hello World!", "A Test Window",
                         WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
                         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                         NULL, NULL, hInstance, NULL);
MSG kMessage;
while (GetMessage (&kMessage, hWindow, 0, 0))
{
 TranslateMessage (&kMessage);
 DispatchMessage (&kMessage);
}

return 0;
}
LRESULT CALLBACK WndProc (HWND hWindow, UINT iMessage,
                           WPARAM wParam, LPARAM lParam)
{
switch (iMessage)
{
 case WM_CLOSE:
      PostQuitMessage (0);
      break;

      default:
      return DefWindowProc (hWindow, iMessage, wParam, lParam);
      }
      return 0;
 }

[Edited by - alex9025 on August 12, 2007 4:02:12 PM]

Share this post


Link to post
Share on other sites
The name of your window class is "Hello World", but the parameter you pass to CreateWindow() is "Hello World!" (with a !). To avoid this kind of mistake, just pass kWndClass.lpszClassName.

Share this post


Link to post
Share on other sites
Try this:


hWindow = CreateWindow ( kWndClass.lpszClassName, "A Test Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL );

If you also needed to process the WM_PAINT message to properly display a window then you'll need to add a call to UpdateWindow( hWindow ); after ShowWindow.

Share this post


Link to post
Share on other sites

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this