// 4Windows_System_menu.cpp : 定义应用程序的入口点。//#include "stdafx.h"#include "4Windows_System_menu.h"#include <iostream>#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)HINSTANCE g_hInst = NULL; //窗口句柄HANDLE g_hStdout = NULL; //控制台句柄CHAR szText[256] = { 0 };void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){HMENU hSysMenu = GetSystemMenu(hWnd, FALSE);//删除菜单项RemoveMenu(hSysMenu, 0, MF_BYPOSITION); //挨个删除//RemoveMenu(hSysMenu, 0, MF_BYPOSITION); //这个执行就会移动不了了//RemoveMenu(hSysMenu, 0, MF_BYPOSITION);//RemoveMenu(hSysMenu, 0, MF_BYPOSITION);//RemoveMenu(hSysMenu, 0, MF_BYPOSITION);//增加菜单项InsertMenu(hSysMenu, 0, MF_BYPOSITION | MF_STRING, 1001, "测试1");InsertMenu(hSysMenu, 0, MF_BYPOSITION | MF_STRING, 1002, "测试2");}//OnSysCommandvoid OnSysCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){//LOWORD(wParam) 同样为系统菜单的IDswitch (LOWORD(wParam)){case 1001:PrintLog("测试1被触发\n");break;case 1002:PrintLog("测试2被触发\n");break;}sprintf_s(szText, 256, "OnSysCommand wParam:%08X, lParam:%08X\n", wParam, lParam);PrintLog(szText);}LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){switch (nMsg){case WM_CREATE:OnCreate(hWnd, nMsg, wParam, lParam);break;case WM_SYSCOMMAND:OnSysCommand(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_HREDRAW | CS_VREDRAW;ATOM atom = RegisterClassEx(&wce);if (atom == NULL){return FALSE;}else{return TRUE;}}HWND CreateWnd(LPSTR pszClassName){HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL, NULL, g_hInst, 0);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";WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);}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;}
原文:http://www.cnblogs.com/nfking/p/5573152.html