   Windows Program Problems
  Today, I started reading about Windows programming! Yeay! I haven''t mastered the advance stuff of C++ like Classes, Structures (Basiclly just like classes), Queues (Sounds to hard, didn''t even try yet.), and Stacks (What are stacks?...), but I''m gonna go learn the basics of Windows programming anyways.Well, I found out Windows programming is not that hard. At least not the basics, like calling callbacks and stuff.Anyways, here are the questions:1. When I build my windows, it builds. When I run it, it does not run. I don''t know if it''s my computer or my program. Here is my program:   #include <windows.h>#include <stdio.h>#define COLOR "BLACK_BRUSH";LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); //Window protypeint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){HWND hwnd;MSG msg;WNDCLASSEX wndclass;wndclass.cbSize =sizeof(wndclass);wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = WndProc;wndclass.cbWndExtra = 0;wndclass.hInstance = hInstance;wndclass.hIcon = LoadIcon (NULL, IDI_WINLOGO);wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);wndclass.lpszMenuName  = NULL;wndclass.lpszClassName = "Window Class 1";wndclass.hIconSm  = LoadIcon (NULL, IDI_WINLOGO);RegisterClassEx(&wndclass);hwnd = CreateWindow("WiseElbenWindow", "WisEElbEn''s SupEr DupEr Window!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,hInstance, NULL);HDC hdc = GetDC(hwnd);if(!hdc)return EXIT_FAILURE;ShowWindow(hwnd, iCmdShow);UpdateWindow (hwnd);RECT rect; //Makes the client COLOR;GetClientRect(hwnd,&rect);FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));while (GetMessage (&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}ReleaseDC(hwnd,hdc); //Free memory.UnregisterClass("WiseElbenWindow",hInstance);return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){    switch (iMsg){case WM_CREATE:break;case WM_DESTROY:case WM_CLOSE:PostQuitMessage(0);break;}return DefWindowProc (hwnd, iMsg, wParam, lParam);}2. Loading Icons.wndclass.hIcon = LoadIcon (NULL, IDI_WINLOGO);How do I load a different icon? Like an icon I made that is in my Recource folder in my Solution? Mabey wndclass.hIcon = LoadIcon (NULL, "icon.ico");?   E-mail:wiseelben@wiseelben.comAIM: WiseElbenICQ: 299127026[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042
  Are you 100% sure it''s not actually running an instance of itself in the background, but just not producing the window?  You can check for this by doing the following.Assuming your program compiles to winprog1.exe, if you check the processes you have running on your computer (in task manager), if winprog1.exe is listed as already running, then your program is running but just isn''t producing the desired window.Now, I''ve not done too much Windows programming yet, but there''s a few things I noticed in your code:1.)  You don''t check to ensure that the class was registered correctly.  Example: /* Try to register the window class and bail if it fails. */if (!RegisterClassEx(&wndclass))return 0;2.)  You''re using a WNDCLASSEX within your program, yet you''re trying to create the window with the function, CreateWindow() and not CreateWindowEx().  I honestly can''t say if this would cause any anomalies with the WNDCLASSEX or not, but I''d guess it may very well do so.Hopefully someone with a little more experience will be able to help you, but try what I''ve suggested and see what happens. --hellz ;  Ok. First off, stacks and queues don''t necessarily have anything to do with Windows. Those are just methods of organizing data. A stack is usually likened to a spring loaded dish dispenser in a cafeteria. The plate on top is the last plate pushed onto the stack (the last one in is the first one out). If you wanted to get at the plate on the bottom of the stack, you''d have to remove each plate from the stack until eventually getting to the plate at the bottom. If you could remove the plate at the bottom without having to do all that, then you would have a queue. (the first one in is the first one out). For example, like a deposit slip envelope dispenser at a ReadyTeller machine or a Pez candy dispenser. Ok. As for your program, you''ve registered a window class "Window Class 1", but you''re creating a window of class "WiseElbenWindow".  As for CreateWindow and WNDCLASSEX, CreateWindow is just a preprocessor macro that wraps CreateWindowEx. You can look it up in the include files to confirm. To use a different icon, first declare an identifier for it in the resource header, then declare your icon in the .rc file for your program // in resource.h#define IDI_MYICON 100// in myproject.rcIDI_MYICON ICON "icon.ico"Then you can load your icon like sowndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_MYICON)); don''t forget to include resource.h ;  GetMessage is not always returning zero when it receives a WM_QUIT. To be honest, on my machine it always returns -1 when receiving WM_QUIT. so you should check for (GetMessage() > 0) instead of ( GetMessage() ).The other stuff I found was already mentioned. ;  Sorry I didn't reply soon. I got 4 new games for my XBox and stopped programming for a while.quote:Ok. First off, stacks and queues don't necessarily have anything to do with Windows. I know that.quote:GetMessage is not always returning zero when it receives a WM_QUIT. To be honest, on my machine it always returns -1 when receiving WM_QUIT. so you should check for (GetMessage() > 0) instead of ( GetMessage() ).Ok. quote:To use a different icon, first declare an identifier for it in the resource header, then declare your icon in the .rc file for your programOk, Thanks.quote:/* Try to register the window class and bail if it fails. */if (!RegisterClassEx(&wndclass))return 0;Woops.------------------You see, I tried to combine GameTutorials.com's first Win32 tutorial with the HDC Win32 tutorial. I'll remember to check for missing items next time. Thanks.   E-mail:wiseelben@wiseelben.comAIM: WiseElbenICQ: 299127026[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042 [edited by - WiseElben on August 4, 2003 4:27:54 PM];  When I use CreateWindowEx() rather than CreateWindow(), an error appears saying CWX() doesn't take 11 paramaters. #include <windows.h>#include <stdio.h>#define COLOR "BLACK_BRUSH";LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); //Window protypeint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){HWND hwnd;MSG msg;WNDCLASSEX wndclass;wndclass.cbSize =sizeof(wndclass);wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = WndProc;wndclass.cbWndExtra = 0;wndclass.hInstance = hInstance;wndclass.hIcon = LoadIcon (NULL, IDI_WINLOGO);wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);wndclass.lpszMenuName  = NULL;wndclass.lpszClassName = "WiseElbenWindow";wndclass.hIconSm  = LoadIcon (NULL, IDI_WINLOGO);RegisterClassEx(&wndclass);//Try to register the window class and bail if it fails.if (!RegisterClassEx(&wndclass))return 0;hwnd = CreateWindow("WiseElbenWindow", "WisEElbEn's SupEr DupEr Window!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,hInstance, NULL);HDC hdc = GetDC(hwnd);if(!hdc)return EXIT_FAILURE;ShowWindow(hwnd, iCmdShow);UpdateWindow (hwnd);RECT rect; //Makes the client COLOR;GetClientRect(hwnd,&rect);FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}ReleaseDC(hwnd,hdc); //Free memory.UnregisterClass("WiseElbenWindow",hInstance);return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){    switch (iMsg){case WM_CREATE:break;case WM_DESTROY:case WM_CLOSE:PostQuitMessage(0);break;}return DefWindowProc (hwnd, iMsg, wParam, lParam);}[source]There are no errors. When I run it, I do not see a window. What I think is happening is that the window open then closes in a split second, for it is not running in the Processes Task Manager. A few builds ago, however, it was running in the Task Manager.If I can't get it to work, I'll just start over, and if I do, I'm gonna make sure I get everything right. :) EDIT: The GetMessage() > 0 thing didn't work. Error.EDIT: Wait a second... if it doesn't run that means it didn't register in the first place! Oh well, I'm gonna rewrite it all. , thanks for the tips.E-mail:wiseelben@wiseelben.comAIM: WiseElbenICQ: 299127026[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042 [edited by - WiseElben on August 4, 2003 4:28:52 PM] [edited by - WiseElben on August 4, 2003 4:34:00 PM];quote:Original post by LessBread  For example, like a deposit slip envelope dispenser at a ReadyTeller machine or a Pez candy dispenser. Very funny analogy, lol!Sorry, WiseElben, i don''t have any advice on the topic, though... but LessBread and hellz have good points on their posts ;  If anyone else have any tips for a new Win32 programmer, tell me!   E-mail:wiseelben@wiseelben.comAIM: WiseElbenICQ: 299127026[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042;  Code from original post: GetClientRect(hwnd,&rect);FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));while (GetMessage(&msg, NULL, 0, 0)){   TranslateMessage(&msg);   DispatchMessage(&msg);}ReleaseDC(hwnd,hdc); //Free memory.Unless you specify CS_OWNDC in your window class style, Windows will gladly steal your window''s device context if it needs the resources. If you don''t need/want a device context with global scope, you can call GetDC, make some GDI calls and release the DC (usually done in response to certain messages in the window procedure).Also, to get a black window background you can just do:wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);Hope that helps. ;  Tips:1. Make PSTR LPSTR. This is what I learned.2. Use WNDCLASSA under Visual Studio to acheive better results as to WNDCLASSEX. This method doesn''t require you to list the name of the class, the Class extra, or the Window Extra until you register the window.3. Name your window class windowClass. MSVC++ can help you out if you do.4. I see no need for stdio.5. You have no break statement under WM_DESTORY: in the switch statement. It will continue on to the next thing. Not good.6. Get a good book.7. Consider MFC.   Scott SimontisC++ Programmer 