Code:
#include <windows.h>
#include <stdio.h>
#define ID_OPEN 1000
#define ID_CLOSE 1001
#define ID_EXIT 1002
#define ID_CUT 1003
#define ID_COPY 1004
#define ID_PASTE 1005
#define ID_1 1006
#define ID_2 1007
#define ID_3 1008
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
CREATESTRUCT *cs;
HMENU hMenu, hSubMenu, hSubMenu2;
char szText[64];
// Menu-item identifiers
static char szMenuItem[6][10] = {"Open", "Close", "Exit", "Cut", "Copy", "Paste"};
switch(msg)
{
case WM_CREATE :
cs = (LPCREATESTRUCT)lParam;
hMenu = CreateMenu();
SetMenu(hwnd, hMenu);
hwndEdit = CreateWindow(TEXT("Edit"), "Alarm msg here", WS_CHILD | WS_VISIBLE | WS_BORDER,
90, 80, 110, 20, hwnd, (HMENU) 10,
NULL, NULL);
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_OPEN, "&Open");
AppendMenu(hSubMenu, MF_STRING, ID_CLOSE, "&Close");
AppendMenu(hSubMenu, MF_STRING, ID_EXIT, "E&xit");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, NULL);
InsertMenu(hMenu, 0, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu, "File");
hSubMenu2 = CreatePopupMenu();
AppendMenu(hSubMenu2, MF_STRING, ID_1, "File 1");
AppendMenu(hSubMenu2, MF_STRING, ID_2, "File 2");
AppendMenu(hSubMenu2, MF_STRING, ID_3, "File 3");
InsertMenu(hSubMenu, 5, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu2, "Recent &Files");
DestroyMenu(hSubMenu2);
DestroyMenu(hSubMenu);
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_CUT, "Cut");
AppendMenu(hSubMenu, MF_STRING, ID_COPY, "Copy");
AppendMenu(hSubMenu, MF_STRING, ID_PASTE, "Paste");
InsertMenu(hMenu, 1, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu, "Edit");
DestroyMenu(hSubMenu);
DestroyMenu(hMenu);
DrawMenuBar(hwnd);
break;
case WM_COMMAND :
switch(LOWORD(wParam))
{
case ID_OPEN :{
if(MessageBox(hwnd,"opening, (just a test)","Opening",MB_OK) == IDOK){
return 0;
}
else{
if(MessageBox(hwnd,"there has been a fatal error in your application,\nwould you like to continue using it?","error",MB_YESNO || MB_ICONWARNING) == IDYES){
return 0;
}
else{
WM_CLOSE;
}
}
}
case ID_CLOSE :
{
if(MessageBox(hwnd,"are you sure you want to close your appointments.","Close",MB_YESNO)== IDYES){
if(MessageBox(hwnd,"your appointments are now closed, would you like to exit the program","exit",MB_YESNO)== IDYES){
WM_CLOSE;
}
else {return 0;}
}
else {
}
}
case ID_EXIT :
case ID_CUT :
case ID_COPY :
case ID_PASTE :
/* printf(szText, "You chose %s", szMenuItem[LOWORD(wParam) - 1000]);
MessageBox(hwnd, szText, "MenuApp", MB_OK);*/
break;
}
case WM_CLOSE :
DestroyWindow(hwnd);
break;
case WM_DESTROY :
PostQuitMessage(0);
case WM_KEYDOWN :
if(wParam == 'O'){
if (MessageBox(hwnd,"Do you want to open your appointments to look at them.","Open",MB_YESNO) == IDYES){
MessageBox(hwnd,"your appointments have been opened.","Open",MB_OK);}
else {
MessageBox(hwnd,"your appointments will not be opened","Open",MB_OK);}
return 0;
}
else if (wParam == 'T')
MessageBox(hwnd, "T Pressed", "T", MB_OK);
else if (wParam == VK_ESCAPE)
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
case WM_KEYUP :
if (wParam == 'R')
MessageBox(hwnd, "R Pressed", "R", MB_OK);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
LPCTSTR className = "ClassName";
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error registering class",
"Error", MB_OK | MB_ICONERROR);
return 1;
}
HWND hwnd = CreateWindowEx(
0,
className,
"07 - Keyboard Input",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
300, 300,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd)
{
MessageBox(NULL, "Error creating window",
"Error", MB_OK | MB_ICONERROR);
return 1;
}
ShowWindow(hwnd, nShowCmd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}