Ressourcen Menu erstellen

alixander

Mitglied
hi Leute ich wollte eigentlich nun mal ein Ressourcen-Menu erstellen und habe da zu auch schon eine Seite gefunden, aber unter Borland funktioniert das überhaupt nicht, und unter devc++ sieht man das Menü nicht. Hier der Code:
#include <windows.h>
#include "ressource.h"

LPCSTR MainClassName = "Ein Menü-Beispiel";

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,
WPARAM wParam, LPARAM lParam);

HMENU hmenu;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
WNDCLASSEX wc;
MSG wmsg;
HWND hWnd;

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(ID_ICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName = MainClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(ID_ICON),
IMAGE_ICON, 16, 16, 0);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Windows Registrations Fehler", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, MainClassName,
"Menü Beispiel",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
300,150,NULL,NULL,hInstance, NULL);

if(hWnd == NULL)
{
MessageBox(NULL, "Fehler beim Erstellen des Fensters!",
"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hmenu= GetMenu(hWnd);

while(GetMessage(&wmsg,NULL,0,0))
{
TranslateMessage(&wmsg);
DispatchMessage(&wmsg);
}
return wmsg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,
WPARAM wParam, LPARAM lParam)
{
char string[255];

switch (iMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_OPEN:
LoadString(GetModuleHandle(NULL),ID_STRING_OPEN,
string, sizeof(string));
MessageBox(hWnd,string,
"Öffnen",MB_ICONINFORMATION);
break;
case ID_FILE_SAVE:
LoadString(GetModuleHandle(NULL),ID_STRING_SAVE,
string,sizeof(string));
MessageBox(hWnd,string,
"Speichern",MB_ICONINFORMATION);
break;
case ID_OPTIONS_OPTIONS_OPTION1:
LoadString(GetModuleHandle(NULL),ID_STRING_OPTION1,
string,sizeof(string));
MessageBox(hWnd,string,
"Option 1",MB_ICONINFORMATION);
break;
case ID_OPTIONS_OPTIONS_OPTION2:
LoadString(GetModuleHandle(NULL),ID_STRING_OPTION2,
string,sizeof(string));
MessageBox(hWnd,string,"Option 2",
MB_ICONINFORMATION);
break;
case ID_ABOUT:
LoadString(GetModuleHandle(NULL),ID_STRING_ABOUT,
string,sizeof(string));
MessageBox(hWnd,string,"Über",MB_ICONINFORMATION);
break;
case ID_FILE_EXIT:
DestroyWindow(hWnd);
break;
}
break;
}
return DefWindowProc(hWnd,iMsg,wParam,lParam);
}
nun ressource.h:
#define ID_STRING_OPEN 1
#define ID_STRING_SAVE 2
#define ID_STRING_OPTION1 3
#define ID_STRING_OPTION2 4
#define ID_STRING_ABOUT 5

#define IDR_MENU1 101

#define ID_ICON 111

#define ID_FILE_OPEN 40001
#define ID_FILE_SAVE 40002
#define ID_FILE_EXIT 40003
#define ID_OPTIONS_OPTIONS_OPTION1 40004
#define ID_OPTIONS_OPTIONS_OPTION2 40005
#define ID_ABOUT 65535
und nun ressource.rc:
#include <windows.h>
#include "resource.h"

/////////////////////////////////////////////////////////////////
//
// Menü
//

IDR_MENU1 MENU
BEGIN
POPUP "Datei"
BEGIN
MENUITEM "Öffnen", ID_FILE_OPEN
MENUITEM "Speichern", ID_FILE_SAVE
MENUITEM "Ende", ID_FILE_EXIT
END
POPUP "Optionen"
BEGIN
POPUP "Optionen"
BEGIN
MENUITEM "Option&1", ID_OPTIONS_OPTIONS_OPTION1
MENUITEM "Option&2", ID_OPTIONS_OPTIONS_OPTION2
END
END
MENUITEM "Über", ID_ABOUT
END

/////////////////////////////////////////////////////////////////
//
//Icon
//
ID_ICON ICON "goofy.ico"

/////////////////////////////////////////////////////////////////
//
//Stringtabelle
//
STRINGTABLE
BEGIN
ID_STRING_OPEN, "Der Dialog Öffnen"
ID_STRING_SAVE, "Der Dialog Speichern"
ID_STRING_OPTION1, "Erste Option im Untermenü"
ID_STRING_OPTION2, "Zweite Option im Untermenü"
ID_STRING_ABOUT, "Ein Menü-Beispiel\nCoded by J.Wolf"
END
Könnt ihr mir vielleicht sagen , woran das liegt?
mfg alixander
 
Da Kachelator grad nicht da ist, Code Tags verwenden Bitte ! [ Code ] xycvsdg [ \ Code ]
Code:
#include <windows.h>
#include "ressource.h"

LPCSTR MainClassName = "Ein Menü-Beispiel";

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,
WPARAM wParam, LPARAM lParam);

HMENU hmenu;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
WNDCLASSEX wc;
MSG wmsg;
HWND hWnd;

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(ID_ICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName = MainClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(ID_ICON),
IMAGE_ICON, 16, 16, 0);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Windows Registrations Fehler", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, MainClassName,
"Menü Beispiel",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
300,150,NULL,NULL,hInstance, NULL);

if(hWnd == NULL)
{
MessageBox(NULL, "Fehler beim Erstellen des Fensters!",
"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hmenu= GetMenu(hWnd);

while(GetMessage(&wmsg,NULL,0,0))
{
TranslateMessage(&wmsg);
DispatchMessage(&wmsg);
}
return wmsg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,
WPARAM wParam, LPARAM lParam)
{
char string[255];

switch (iMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_OPEN:
LoadString(GetModuleHandle(NULL),ID_STRING_OPEN,
string, sizeof(string));
MessageBox(hWnd,string,
"Öffnen",MB_ICONINFORMATION);
break;
case ID_FILE_SAVE:
LoadString(GetModuleHandle(NULL),ID_STRING_SAVE,
string,sizeof(string));
MessageBox(hWnd,string,
"Speichern",MB_ICONINFORMATION);
break;
case ID_OPTIONS_OPTIONS_OPTION1:
LoadString(GetModuleHandle(NULL),ID_STRING_OPTION1,
string,sizeof(string));
MessageBox(hWnd,string,
"Option 1",MB_ICONINFORMATION);
break;
case ID_OPTIONS_OPTIONS_OPTION2:
LoadString(GetModuleHandle(NULL),ID_STRING_OPTION2,
string,sizeof(string));
MessageBox(hWnd,string,"Option 2",
MB_ICONINFORMATION);
break;
case ID_about :
LoadString(GetModuleHandle(NULL),ID_STRING_ABOUT,
string,sizeof(string));
MessageBox(hWnd,string,"Über",MB_ICONINFORMATION);
break;
case ID_FILE_EXIT:
DestroyWindow(hWnd);
break;
}
break;
}
return DefWindowProc(hWnd,iMsg,wParam,lParam);
}
nun ressource.h:
#define ID_STRING_OPEN 1
#define ID_STRING_SAVE 2
#define ID_STRING_OPTION1 3
#define ID_STRING_OPTION2 4
#define ID_STRING_ABOUT 5

#define IDR_MENU1 101

#define ID_ICON 111

#define ID_FILE_OPEN 40001
#define ID_FILE_SAVE 40002
#define ID_FILE_EXIT 40003
#define ID_OPTIONS_OPTIONS_OPTION1 40004
#define ID_OPTIONS_OPTIONS_OPTION2 40005
#define ID_ABOUT 65535
und nun ressource.rc:
#include <windows.h>
#include "resource.h"

/////////////////////////////////////////////////////////////////
//
// Menü
//

IDR_MENU1 MENU
BEGIN
POPUP "Datei"
BEGIN
MENUITEM "Öffnen", ID_FILE_OPEN
MENUITEM "Speichern", ID_FILE_SAVE
MENUITEM "Ende", ID_FILE_EXIT
END
POPUP "Optionen"
BEGIN
POPUP "Optionen"
BEGIN
MENUITEM "Option&1", ID_OPTIONS_OPTIONS_OPTION1
MENUITEM "Option&2", ID_OPTIONS_OPTIONS_OPTION2
END
END
MENUITEM "Über", ID_ABOUT
END

/////////////////////////////////////////////////////////////////
//
//Icon
//
ID_ICON ICON "goofy.ico"

/////////////////////////////////////////////////////////////////
//
//Stringtabelle
//
STRINGTABLE
BEGIN
ID_STRING_OPEN, "Der Dialog Öffnen"
ID_STRING_SAVE, "Der Dialog Speichern"
ID_STRING_OPTION1, "Erste Option im Untermenü"
ID_STRING_OPTION2, "Zweite Option im Untermenü"
ID_STRING_ABOUT, "Ein Menü-Beispiel\nCoded by J.Wolf"
END
 
Du musst das Menu schon erstellen !
Siehe unten und dann mit SetMenu im Fenster setzen --> Nichts ist umsonst im Leben ! ;)
Kannst übrigens schon bei CreateWindowEx das Handle auf das Menü setzen.
LoadMenu
The LoadMenu function loads the specified menu resource from the executable (.EXE) file associated with an application instance.

HMENU LoadMenu(
HINSTANCE hInstance, // handle to application instance
LPCTSTR lpMenuName // menu name string or menu-resource
// identifier
);

Parameters
hInstance
Handle to the instance of the module containing the menu resource to be loaded.
lpMenuName
Pointer to a null-terminated string that contains the name of the menu resource. Alternatively, this parameter can consist of the resource identifier in the low-order word and zero in the high-order word. To create this value, use the MAKEINTRESOURCE macro.
Return Values
If the function succeeds, the return value is the handle to the menu resource.

If the function fails, the return value is NULL. To get extended error information, callGetLastError.

Remarks
The DestroyMenu function is used, before an application closes, to destroy the menu and free memory that the loaded menu occupied.

Windows CE: Windows CE version 1.0 does not support cascading menus.

Windows CE versions 2.0 and later support cascading menus.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
 
Zurück