|
-
May 28th, 2001, 11:03 PM
#1
Thread Starter
Hyperactive Member
Menus with API
I would like to use the API to create my menus, instead of a resource file. Could anyone please tell me how to, or where i could find a tutorial? thank you very much!
Amon Ra
The Power of Learning.
-
May 29th, 2001, 10:37 AM
#2
Thread Starter
Hyperactive Member
-
May 29th, 2001, 11:30 AM
#3
Thread Starter
Hyperactive Member
Weird...
How come my MSVC++ always gives me this error?
Fatal Error:
"The compiler is out of heap space..."
Here is all the code:
#include <windows.h>
static char g_szClassName[] = "My Window Class";
static HINSTANCE g_hInst = NULL;
HMENU MyMenu;
HMENU SubMenu;
HWND hButton;
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam,
LPARAM lParam)
{
switch(Message) {
case WM_CREATE:
{
MENUITEMINFO MenuInfo ;
MenuInfo.cbSize=sizeof(MenuInfo);
MenuInfo.fMask=MIIM_DATA|MIIM_ID|MIIM_TYPE|MIIM_STATE ;
MenuInfo.fType= MFT_STRING|MFT_MENUBREAK ;
MenuInfo.fState=MFS_DISABLED ;
MenuInfo.wID=152;
MenuInfo.hSubMenu=NULL;
MenuInfo.hbmpChecked=NULL;
MenuInfo.hbmpUnchecked=NULL;
MenuInfo.dwItemData=153;
MenuInfo.dwTypeData="My Menu Item";
MenuInfo.cch=sizeof("My Menu Item");
SubMenu=CreateMenu();
InsertMenuItem(SubMenu,2,true,&MenuInfo);
MenuInfo.cbSize=sizeof(MenuInfo);
MenuInfo.fMask=MIIM_DATA|MIIM_ID|MIIM_TYPE|MIIM_STATE|MIIM_SUBMENU ;
MenuInfo.fType= MFT_STRING ;
MenuInfo.fState=MFS_ENABLED ;
MenuInfo.wID=150;
MenuInfo.hSubMenu=SubMenu;
MenuInfo.hbmpChecked=NULL;
MenuInfo.hbmpUnchecked=NULL;
MenuInfo.dwItemData=151;
MenuInfo.dwTypeData="My Menu";
MenuInfo.cch=sizeof("My Menu");
MyMenu=CreateMenu();
InsertMenuItem(MyMenu,1,true,&MenuInfo);
SetMenu(hwnd,MyMenu);
}
break;
case WM_LBUTTONDOWN: //handles the message when clicked
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "Left Button Clicked: Program Name and Path: ",
MB_ICONINFORMATION | MB_OK);
SetWindowText(hwnd, "hello cruel world!");
}
break;
case WM_RBUTTONDOWN:
PostMessage(hwnd, WM_CLOSE,0,0); //Post a message in the message queue
break;
case WM_CLOSE: //destroy the window
DestroyMenu(SubMenu);
DestroyMenu(MyMenu);
DestroyWindow(hwnd);
break;
case WM_DESTROY: //terminates the program
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,Message,wParam,lParam);
}
return 0;
}
//main function of the program(like main() in console)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX WndClass;
HWND hwnd;
MSG msg;
//set class characteristics
g_hInst = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//check for window creation: success?
if(!RegisterClassEx(&WndClass)) {
MessageBox(0, "Window Registration Failed!","Error",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
//finally create the window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
g_szClassName,
"This is my first window in C++!!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 300,
NULL, NULL, g_hInst, NULL);
//created successfully?
if(hwnd == NULL) {
MessageBox(0, "Window Creation Failed", "Error",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
//show it
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
hButton = CreateWindowEx(NULL,"Button1","C++ Button",
WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, NULL,
g_hInst);
//message loop: message --> WindowProc
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Thanks for helping If you dont mind, you can also help on the previous post Thanks
Alex
Amon Ra
The Power of Learning.
-
May 29th, 2001, 11:35 AM
#4
Frenzied Member
Guys, just one note: Please put the code in the [code./code] tags.
-
May 29th, 2001, 11:59 AM
#5
Frenzied Member
I dont know why you are running out of heap space, but I did find a problem with your program. You need to fix your CreateWindowEX, your button class has to be called "Button", plus you need a lParm, at the end of your call.
So it would look like this:
PHP Code:
hButton = CreateWindowEx(NULL,"Button","C++ Button",
WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, NULL,
g_hInst,0);
Other than that your program worked fine for me.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
May 29th, 2001, 08:49 PM
#6
Addicted Member
I think MenuInfo.wID is the item number you should intercept in th WM_COMMAND but i'm not shure so try out all the different item number's and see which one the the menu item ID.
-
May 30th, 2001, 10:27 AM
#7
Thread Starter
Hyperactive Member
Menus...
How do I get the message when a menu item is clicked? Meaning, how do i respond to a menu click the way i respond to a button click?
Amon Ra
The Power of Learning.
-
May 30th, 2001, 10:36 AM
#8
Frenzied Member
Just change the case to your wID #ers:
PHP Code:
case WM_COMMAND:
{
switch(wparam)
{
case ID_QUIT:
{
MessageBox(NULL, "The application is now quiting!",
"Quiting application", MB_OK | MB_ICONINFORMATION);
PostQuitMessage(0);
return 0;
} break;
}
} break;
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
May 30th, 2001, 07:24 PM
#9
Thread Starter
Hyperactive Member
Cool...
Thanks a lot
Amon Ra
The Power of Learning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|