just starting

i have just started windows programming and i am trying to make menus and it wont show up when i compile and run it? its in DEV C++. does this happen often if you need the code let me know. and the menu is on a ".rc" file and is run through a ".h" file.
Post the smallest complete program you can.
main windows file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

resource.h file
1
2
3
4
#define IDR_MYMENU 101
#define IDI_MYICON 201
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002 

the ".rc"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "resource.h"
IDR_MYMENU MENU
BEGIN
     POPUP "&File"
     BEGIN
          MENUITEM "E&xit", ID_FILE_EXIT
     END
     POPUP "&Stuff"
     BEGIN
          MENUITEM "&Go", ID_STUFF_GO
          MENUITEM "G&o somewhere else", 0, GRAYED
     END
END
IDI_MYICON ICON "crazy.ico"
That's not exactly a complete (runnable) program! But I filled in the missing bits, and the menu shows up for me. Here's my main.cpp:

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
#include <windows.h>
#include "resource.h"

const char* gClassName = "MenuTest";

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp ) {
   switch( msg ) {
      case WM_DESTROY: PostQuitMessage( 0 );  break;
   }
   return DefWindowProc( hwnd, msg, wp, lp );
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE unused,
                    LPSTR cmdLine, int cmdShow )
{
   WNDCLASSEX wc = { sizeof( WNDCLASSEX ) };
   wc.lpfnWndProc   = WndProc;
   wc.hInstance     = hInst;
   wc.hIcon         = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MYICON) );
   wc.hCursor       = LoadCursor( 0, IDC_ARROW );
   wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
   wc.lpszMenuName  = MAKEINTRESOURCE( IDR_MYMENU );
   wc.lpszClassName = gClassName;
   wc.hIconSm       = (HICON) LoadImage( hInst, MAKEINTRESOURCE( IDI_MYICON ),
                                         IMAGE_ICON, 16, 16, 0);
   RegisterClassEx( &wc );
   
   CreateWindowEx( 0, gClassName, gClassName,
      WS_OVERLAPPEDWINDOW | WS_VISIBLE,
      0, 0, 400, 300,
      0, 0, hInst, 0 );

   MSG msg;
   while( GetMessage( &msg, 0, 0, 0 ) ) {
      DispatchMessage( &msg );
   }
   return msg.wParam;
}

here's my code. i used yours hammurabi and i still dont get a menu! just a blank white 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
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
220, 200, 540, 320,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
It works fine for me too...

Are you sure you are linking with the compiled .rc file?
Try closing down the IDE (Dev-C++) and delete all of the extra files it has created. Delete all the .o files, the ones with _private in the name, and also the Makefile. Then open the project file (.dev) and hit F9.

Here's a rewrite of my original code. (Fixed an error (WM_DESTROY handler was also calling DefWindowProc), and hooked up the Exit command of the file menu.)

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
#include <windows.h>
#include "resource.h"

const char* gClassName = "MenuTest";

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp ) {
   switch( msg ) {
      case WM_COMMAND:
         switch( LOWORD( wp ) ) {
         case ID_FILE_EXIT:
            SendMessage( hwnd, WM_DESTROY, 0, 0 );
            break;
         }
         break;
      case WM_DESTROY:
         PostQuitMessage( 0 );
         break;
      default:
         return DefWindowProc( hwnd, msg, wp, lp );
   }
   return 0;
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE unused,
                    LPSTR cmdLine, int cmdShow )
{
   WNDCLASSEX wc = { sizeof( WNDCLASSEX ) };
   wc.lpfnWndProc   = WndProc;
   wc.hInstance     = hInst;
   wc.hIcon         = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MYICON) );
   wc.hCursor       = LoadCursor( 0, IDC_ARROW );
   wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
   wc.lpszMenuName  = MAKEINTRESOURCE( IDR_MYMENU );
   wc.lpszClassName = gClassName;
   wc.hIconSm       = (HICON) LoadImage( hInst, MAKEINTRESOURCE( IDI_MYICON ),
                                         IMAGE_ICON, 16, 16, 0);
   RegisterClassEx( &wc );
   
   CreateWindowEx( 0, gClassName, gClassName,
                   WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                   0, 0, 400, 300, 0, 0, hInst, 0 );

   MSG msg;
   while( GetMessage( &msg, 0, 0, 0 ) ) {
      TranslateMessage( &msg );
      DispatchMessage( &msg );
   }
   return msg.wParam;
}

Last edited on
i actually let it sit for a bit then recompiled and it worked. i dont know why it didnt before but whatever, lol.
If you still need help with Window Applications you should Go to: http://www.winprog.org/tutorial/
And download the PDF file, It helped me alot.
:)
that is the tutorial i am using!!!
Topic archived. No new replies allowed.