Skip to main content
gamedev.net
🔒 Locked

Error Creating Window?

Started by Colin Jeanne Jan 4, 2002 at 11:21 PM 12 replies 2.3k views
Original Post
Colin Jeanne
Colin Jeanne
I''m so lost at this. I''m trying to make a simple window exactly like I have many times before but this time CreateWindowEx() always fails. GetLastError() returns 0 and FormatMessage() tells me that the error''s string is "The operation was completed successfully". I looked over my code, I compared it with other apps I wrote that do work, I made a different project making sure that I created a Win32 Application and even copied the linked libs from a working project but it still died. Here is the first piece of code I tried:
  
/*First 2 lines in my WinMain function*/
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_NOTE), NULL, MainProc) == -1)
MessageBox(NULL, "Error Creating Window", "Note Book Error", MB_OK | MB_ICONEXCLAMATION);
  
DialogBox fails. I went the other way around with this code:
  
/*In my WinMain function*/
WNDCLASSEX wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInst;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = g_name;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN);
wc.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTEN));
wc.hIconSm = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTEN));
wc.hCursor = LoadCursor(hInst, IDC_ARROW);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wc.style =  CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;

if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error Registering Window Class", "Note Book Error", MB_OK | MB_ICONEXCLAMATION);
return GetLastError();
}

if (!(g_hwnd = CreateWindowEx(0,
g_name,
"Note Book v1.0.0",
WS_POPUP,
GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
320,
240,
NULL,
LoadMenu(hInst, MAKEINTRESOURCE(IDR_MAIN)),
hInst,
NULL)))
{
MessageBox(NULL, "Error Creating Window", "Note Book Error", MB_OK | MB_ICONEXCLAMATION);
unsigned long err = GetLastError();
LPVOID buf;

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buf,
0,
NULL);

MessageBox(NULL, (LPCTSTR)buf, "Note Book Error", MB_OK | MB_ICONEXCLAMATION);
return err;
}
  
Changing it so that no resources are loaded does nothing. CreateWindowEx always fails and I haven''t a clue why. Invader X Invader''s Realm
Kippesoep
Kippesoep
I see a couple of problems, although I can''t think why any of them would cause CreateWindowEx to fail.

  wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;  

should be
  wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;  


  LoadCursor(hInst, IDC_ARROW);  

should probably be
  LoadCursor(NULL, IDC_ARROW);  


It is concievable that the incorrect background brush causes your window to be created correctly, but drawn transparently (i.e. not at all).
Kippesoep
Codespike
Codespike
hi invader just letting you know your not the only one with this problem. I''m using MFC , almost the same thing and the stupid cow doesn''t wanna create dialogue boxes for me either. When I load other people''s program''s though it works, so go figure.
Live by the code, die by the code
ELS1
ELS1
this is what i use(d) but with a RGB with a for the background color.

  
static char szAppName[] = "_Shred32" ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;

wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = CreateSolidBrush (RGB (58,129,209)) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;

RegisterClassEx (&wndclass) ;

hwnd = CreateWindow (szAppName, "Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

return msg.wParam ;
}


Edited by - ELS1 on January 5, 2002 11:11:24 PM
Colin Jeanne
Colin Jeanne
    
long __stdcall WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_ABOUT:
MessageBox(hwnd, "Note Book v2.0 was made by Invader X\nVisit him at http://www.invadersrealm.com", "About Note Book v2.0", MB_OK | MB_ICONINFORMATION);

break;
};
break;

case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;

default:
return 0;
};
return DefWindowProc(hwnd, msg, wParam, lParam);
}



int __stdcall MainProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_NOTEN)));
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_NOTEN)));
break;

case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_ABOUT:
MessageBox(hwnd, "Note Book v2.0 was made by Invader X\nVisit him at http://www.invadersrealm.com", "About Note Book v2.0",

MB_OK | MB_ICONINFORMATION);
break;
};
break;

case WM_SIZE:
SetWindowPos(GetDlgItem(hwnd, IDC_TEXT), HWND_TOP, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_SHOWWINDOW);
break;

case WM_CLOSE:
EndDialog(hwnd, WM_CLOSE);
break;
default:
return 0;
};
return 1;
}



*EDIT To tell you that I've tried this with and without the resources and messageboxes commented out.

Invader X
Invader's Realm

Edited by - Invader X on January 6, 2002 2:53:46 AM
Colin Jeanne
Colin Jeanne
AP: Thanks. Reversing the return 0 and DefWindowProc() lines worked.

However, the DialogBox way still doesn't. I made sure to call InitCommonControls() in the beginning (IDC_TEXT is a RichEdit control).

*EDIT said was... mean way

Edited by - Invader X on January 7, 2002 2:04:48 AM
Brannon
Brannon
Unfortunately (this is incredibly retarded) InitCommonControls[Ex]() doesn''t initialize the RichEdit control. To initialize the RichEdit control put the following in your WinMain (or wherever is appropriate):

  
LoadLibrary("riched20.dll");
or
LoadLibrary("riched10.dll");


I had to do this in an old project to get it to work. It''s totally lame. I really can''t remember the name of the .dll to load exactly. I''m pretty sure you want "riched20.dll", but that could be wrong. If it is, let me know and I''ll dig up that old code to see which one I used.

I think that this "issue" is documented in a KB article somewhere (I found it out somehow).


-Brannon
-Brannon
Colin Jeanne
Colin Jeanne
I was afraid of that too...

About a week ago I spent a good amount of time looking around MSDN to find what was needed to load a richedit and it sounded like just InitCommonControls or InitCommonControlsEx().

Thanks for the LoadLibrary heads up. At first using "richedit20.dll" didn''t work, but "riched32.dll" did.

Thanks again!

Invader X
Invader''s Realm
Brannon
Brannon
Yeah, I knew I was remembering the name wrong!

Cool.


-Brannon
-Brannon

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.