// 3Windows_menu.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "3Windows_menu.h"
#include <iostream>
HINSTANCE g_hInst;
HANDLE g_hStdout = NULL;
BOOL g_bCheckCut = FALSE; //是看Cut项有没有被选择上
//宏定义输入函数
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL);
void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
PrintLog("OnCreate!\n");
HMENU hMainMenu = CreateMenu();
//增加菜单项,用AppendMenu
//下面这三句如果不注释掉会多产生一块母菜单
//AppendMenu(hMainMenu, MF_STRING, 2001, "文件");
//AppendMenu(hMainMenu, MF_STRING, 2002, "编辑");
//AppendMenu(hMainMenu, MF_STRING, 2003, "帮助");
//增加弹出菜单项抽象
HMENU hFileMenu = CreatePopupMenu();
HMENU hEditMenu = CreatePopupMenu();
HMENU hHelpMenu = CreatePopupMenu();
//增加弹出菜单项实体
//新建菜单
AppendMenu(hFileMenu, MF_STRING|MF_CHECKED, 3001, "新建(&N)"); //&N 加快捷键N
AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
AppendMenu(hFileMenu, MF_STRING, 3002, "退出(&X)"); //Disabled无法被选择
//编辑菜单
AppendMenu(hEditMenu, MF_STRING, 4001, "剪切(&T)");
AppendMenu(hEditMenu, MF_STRING, 4002, "复制(&C)");
AppendMenu(hEditMenu, MF_STRING, 4003, "粘贴(&P)");
//帮助
AppendMenu(hHelpMenu, MF_STRING, 5001, "关于(&A)");
AppendMenu(hHelpMenu, MF_STRING | MF_DISABLED, 5002, "注册(&R)");
AppendMenu(hHelpMenu, MF_STRING, 5003, "帮助(&H)");
//增加弹出方法
AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT)hFileMenu, "文件(&F)");//文件弹出
AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT)hEditMenu, "编辑(&E)"); //编辑弹出
AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT)hHelpMenu, "帮助(&H)"); //帮助弹出
//显示菜单
SetMenu(hWnd, hMainMenu);
//SetMenu(hWnd, hFileMenu); //err
}
//命令处理
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
CHAR szText[256] = { 0 };
sprintf_s(szText, 256, "OnCommand ID:%d\n", LOWORD(wParam));
PrintLog(szText);
switch (LOWORD(wParam))
{
case 3002:
PostQuitMessage(0);
break;
case 3001:
PrintLog("新建菜单!\n");
break;
case 4001: //取返一下,来改变剪切前的勾
g_bCheckCut = !g_bCheckCut;
break;
}
}
void OnInitMenuPopup(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
CHAR szText[256] = { 0 };
sprintf_s(szText, 256, "OnInitMenuPopup: WPARAM=%08X, LPARAM=%08X\n", wParam, lParam);//打成十六进制
PrintLog(szText);
//给剪切前加对勾
if (TRUE == g_bCheckCut)
{
HMENU hMenu = (HMENU)wParam; //传过来的参数就是HMENU句柄
CheckMenuItem(hMenu, 4001, MF_CHECKED | MF_BYCOMMAND);
}
else
{
HMENU hMenu = (HMENU)wParam; //传过来的参数就是HMENU句柄
CheckMenuItem(hMenu, 4001, MF_UNCHECKED | MF_BYCOMMAND);
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_CREATE:
OnCreate(hWnd, nMsg, wParam, lParam);
break;
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_INITMENUPOPUP:
OnInitMenuPopup(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_VREDRAW | CS_HREDRAW;
ATOM aTom = RegisterClassEx(&wce);
if (aTom == 0)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
HWND hWnd = CreateWindowEx(0, pszClassName, "ttttt", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, g_hInst, NULL);
return hWnd;
}
void showWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//控制台
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
PrintLog(szText);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
ConsoleWnd();
RegisterWnd("OOOO");
HWND hWnd = CreateWnd("OOOO");
showWnd(hWnd);
msg();
return 0;
}
时间: 2024-10-27 06:56:05