Code:
#include "resource.h"
#include <windows.h>
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_DO 9003
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int do_stuff();
int cr8_window(int nCmdShow)
{
const char g_szClassName[] = "myWindowClass";
WNDCLASSEX wc;// Data structure for the windowclass
HWND hwnd;//window handle
MSG Msg;//used for translate msg
HINSTANCE hInstance = GetModuleHandle(NULL);
//Step 1: Registering the WindowClass
wc.cbSize = sizeof(WNDCLASSEX);// The size of the structure
wc.style = 0;// Class Styles (CS_*), not to be confused with Window Styles (WS_*) This can usually be set to 0.
wc.lpfnWndProc = WndProc;// Pointer to the window procedure for this window class.
wc.cbClsExtra = 0;// Amount of extra data allocated for this class in memory. Usually 0.
wc.cbWndExtra = 0;// Amount of extra data allocated in memory per window of this type. Usually 0.
wc.hInstance = hInstance;// Handle to application instance (that we got in the first parameter of WinMain()).
wc.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_MSKIN));// Large (usually 32x32) icon shown when the user presses Alt+Tab.
wc.hCursor = LoadCursor(NULL, IDC_ARROW);// Cursor that will be displayed over our window.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);// Background Brush to set the color of our window.
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);// Name of a menu resource to use for the windows with this class.
wc.lpszClassName = g_szClassName;// Name to identify the class with.
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_MSKIN), IMAGE_ICON, 16, 16, 0);// icon to show in the taskbar and in the top left corner of the window.
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
cr8_window(nCmdShow);
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HICON hIcon, hIconSm;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "EXIT");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Main");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&dostuff");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
SetMenu(hwnd, hMenu);
hIcon = LoadImage(NULL, "Skin.bmp", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
else
MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);
hIconSm = LoadImage(NULL, "Skin.bmp", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
else
MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
DestroyWindow(hwnd);
break;
case ID_STUFF_GO:
do_stuff();
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int do_stuff()
{
for(;;) {
MessageBox(NULL,"COOL","COOL",MB_OK);
}
}