-
Menus again...
I'm trying to display a menu, but it doesn't seem to work....I have had this problem before, but with a different compiler....I'm now using VC++ 6.
Here is the code:
Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#define WINDOW_CLASS_NAME " WINCLASS1"
HWND main_window_handle = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return 0;
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
} break;
case WM_DESTROY:
{
MessageBox(NULL, "Bye bye cruel world!", "Bye!", MB_OK | MB_ICONINFORMATION);
PostQuitMessage(0);
return 0;
} break;
default: break;
}
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hPrevInstance,
LPSTR lpstCmdLine, int ncmdshow)
{
WNDCLASS winclass;
HWND hwnd;
MSG msg;
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
if (!RegisterClass(&winclass))
return 0;
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, "Hello Carsten!",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
350,200,
320,200,
NULL,
NULL,
hinstance,
NULL)))
return 0;
main_window_handle = hwnd;
SetMenu(hwnd,LoadMenu(hinstance, "IDR_MENU1"));
while (1)
{
if (PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(msg.wParam);
}
-
Just curious, what is #define WIN32_LEAN_AND_MEAN?
-
It forces various options in the windows.h file for be taken....
You should always start every windows program like this:
Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
-
Hmm, never knew that. What kinds of options? Smaller .exe file, etc.?
-
Anyone know why the code above doesn't show the menu???
I have instructed the compiler where it can find the resources, and I have included the resource file in the main file.....:confused:
-
Could you upload the resource.h header and your resource file?
-
Here they are:
file 'res.rc':
Code:
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Danish resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_DAN)
#ifdef _WIN32
LANGUAGE LANG_DANISH, SUBLANG_DEFAULT
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
IDR_MENU1 MENU DISCARDABLE
BEGIN
POPUP "Test"
BEGIN
MENUITEM "Test", ID_TEST_TEST
END
END
#endif // Danish resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
file 'resource.h'
Code:
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by res.rc
//
#define IDR_MENU1 102
#define ID_TEST_TEST 40002
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40003
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
-
You need to set the Menu when you register the window class (WNDCLASSEX).
Code:
// Change this line
winclass.lpszMenuName = NULL;
// to this
winclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
-
I don't think you have to. May have a point about that MAKEINTRESOURCE() thing though, you could use that in the SetMenu function.... not sure that's right though, I'm guessing really.
-
Yep, that works. Change it to this:
Code:
SetMenu(hwnd,LoadMenu(hinstance, MAKEINTRESOURCE(IDR_MENU1)));
-
That solved the mystery! Thanks guys! :D
-
No, parkise, we never found the answer...Since I was using DevCpp at the time, I had to include the working dir in the compiler options...