5Windows_system_right_menu右键菜单
Posted 养老保险年审
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5Windows_system_right_menu右键菜单相关的知识,希望对你有一定的参考价值。
5Windows_system_right_menu右键菜单
// 5Windows_system_right_menu右键菜单.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "5Windows_system_right_menu右键菜单.h"
// 4Windows_System_menu.cpp : 定义应用程序的入口点。
//
#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 OnRButtonUp(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//获取菜单句柄(建立菜单)
HMENU hPopMenu = CreatePopupMenu();
//增加菜单
AppendMenu(hPopMenu, MF_STRING, 1001, "复制");
AppendMenu(hPopMenu, MF_STRING, 1002, "粘贴");
AppendMenu(hPopMenu, MF_STRING, 1003, "剪切");
AppendMenu(hPopMenu, MF_SEPARATOR, 1004, NULL);
AppendMenu(hPopMenu, MF_STRING, 1005, "退出");
//显示菜单
POINT point = { 0 }; //转换时需要POINT数据结构
point.x = LOWORD(lParam); //客户区坐标
point.y = HIWORD(lParam);
//转换成屏幕坐标
ClientToScreen(hWnd, &point);
TrackPopupMenu(hPopMenu, TPM_LEFTALIGN, point.x, point.y, 0, hWnd, NULL);
}
//OnCommand
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int CMDId = LOWORD(wParam);
switch (CMDId)
{
case 1001:
//TD//...
PrintLog("复制已完成\n");
break;
case 1002:
//TD//...
PrintLog("粘贴已完成\n");
break;
case 1003:
//TD//...
PrintLog("剪切已完成\n");
break;
case 1005:
//TD//...
PostQuitMessage(0);
break;
}
//打印日志
sprintf_s(szText, 256, "OnCommand ID:%d\n", CMDId);
PrintLog(szText);
}
//OnConTextMenu,右键菜单专用函数
void OnConTextMenu(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//获取菜单句柄(建立菜单)
HMENU hPopMenu = CreatePopupMenu();
//增加菜单
AppendMenu(hPopMenu, MF_STRING, 1006, "一式");
AppendMenu(hPopMenu, MF_STRING, 1007, "二式");
AppendMenu(hPopMenu, MF_STRING, 1008, "三式");
AppendMenu(hPopMenu, MF_SEPARATOR, 1009, NULL);
AppendMenu(hPopMenu, MF_STRING, 1010, "极招");
//显示菜单
//POINT point = { 0 }; //转换时需要POINT数据结构
//point.x = LOWORD(lParam); //客户区坐标
//point.y = HIWORD(lParam);
////转换成屏幕坐标
//ClientToScreen(hWnd, &point);
int nX = LOWORD(lParam);
int nY = HIWORD(lParam);
TrackPopupMenu(hPopMenu, TPM_LEFTALIGN, nX, nY, 0, hWnd, NULL);
//删除菜单
DestroyMenu(hPopMenu);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_RBUTTONUP:
//OnRButtonUp(hWnd, nMsg, wParam, lParam);
break;
case WM_CONTEXTMENU:
OnConTextMenu(hWnd, nMsg, wParam, lParam);
break;
case WM_COMMAND:
OnCommand(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;
}
以上是关于5Windows_system_right_menu右键菜单的主要内容,如果未能解决你的问题,请参考以下文章