本文是,截图:Windows编程基础 第六章 系统菜单 上下文相关菜单 图标 光标 字符串 菜单 加速键
本文的下载地址:http://yunpan.cn/cd59RHKB5FBwd 访问密码 6af8
// 演示右键菜单和子菜单
//
#include "stdafx.h"
#include "windows08.h"
#include "stdio.h"
#include "windows.h"
HINSTANCE g_hInst=NULL;
HANDLE g_hConsole=NULL;
VOID OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HMENU hSysMenu=GetSystemMenu(hWnd,FALSE);
//这里的参数是FALSE,如果是TRUE的话,那么看到的将是没有任何修改的菜单项,如果使用FALSE那么看到的就是修改后的菜单项。
//另外这个参数改写成为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);
RemoveMenu(hSysMenu,0,MF_BYPOSITION);
InsertMenu(hSysMenu,0,MF_BYPOSITION,1001,TEXT("菜单项1"));
InsertMenu(hSysMenu,1,MF_BYPOSITION,1002,TEXT("菜单项2"));
}
VOID OnRButtonUp(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HMENU hContextMenu=CreatePopupMenu();
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING,1003,TEXT("右键菜单项1"));
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING,1004,TEXT("右键菜单项2"));
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING,1005,TEXT("右键菜单项3"));
POINT pt={0};
pt.x=LOWORD(lParam);
pt.y=HIWORD(lParam);
ClientToScreen(hWnd,&pt);
TrackPopupMenu(hContextMenu,TPM_RIGHTALIGN,pt.x,pt.y,0,hWnd,NULL);
//因为要实现的效果是鼠标在哪里点击就要在哪里显示右键菜单,所以这里的最后的一个参数,也就是矩形这个参数,应该设置为NULL
}
VOID OnContextMenu(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HMENU hContextMenu=CreatePopupMenu();
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING,1003,TEXT("右键菜单项1"));
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING,1004,TEXT("右键菜单项2"));
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING,1005,TEXT("右键菜单项3"));
HMENU hContextMenuPop=CreatePopupMenu();
AppendMenu(hContextMenuPop,MF_BYCOMMAND|MF_STRING,1006,TEXT("子菜单项0"));
AppendMenu(hContextMenuPop,MF_BYCOMMAND|MF_STRING,1007,TEXT("子菜单项1"));
AppendMenu(hContextMenuPop,MF_BYCOMMAND|MF_STRING,1008,TEXT("子菜单项2"));
AppendMenu(hContextMenu,MF_BYCOMMAND|MF_STRING|MF_POPUP,(UINT)hContextMenuPop,TEXT("子菜单项"));
POINT pt={0};
pt.x=LOWORD(lParam);
pt.y=HIWORD(lParam);
TrackPopupMenu(hContextMenu,TPM_RIGHTALIGN,pt.x,pt.y,0,hWnd,NULL);
DestroyMenu(hContextMenu);
}
VOID OnSysCommand(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(LOWORD(wParam))
{
case 1001:
MessageBox(NULL,TEXT("菜单1"),TEXT("系统菜单:"),MB_OK);
break;
case 1002:
MessageBox(NULL,TEXT("菜单2"),TEXT("系统菜单:"),MB_OK);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
case WM_SYSCOMMAND:
OnSysCommand(hWnd,uMsg,wParam,lParam);
break;
// case WM_RBUTTONUP:
OnRButtonUp(hWnd,uMsg,wParam,lParam);
break;
case WM_CONTEXTMENU:
OnContextMenu( hWnd, uMsg, wParam, lParam);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL RegisterWnd(TCHAR * pszClassName )
{
WNDCLASSEX wce={0};
wce.cbClsExtra=0;
wce.cbSize=sizeof(wce);
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|CS_DBLCLKS;
ATOM result=RegisterClassEx(&wce);
if (result==0)
{
MessageBox(NULL,TEXT("窗口类注册失败!"),TEXT("窗口类注册失败!"),MB_OK);
return FALSE;
}
return TRUE;
}
VOID DisplayWindow(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
VOID Message()
{
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND CreateMyWindow(TCHAR * pszClassName)
{
HWND hWndNew=CreateWindowEx(0,pszClassName,TEXT("系统菜单和右键菜单!"),WS_OVERLAPPEDWINDOW|WS_THICKFRAME,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInst,NULL);
return hWndNew;
}
VOID NewConsole()
{
AllocConsole();
g_hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
}
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
NewConsole();
RegisterWnd(TEXT("MyWnd"));
HWND hWnd=CreateMyWindow(TEXT("MyWnd"));
DisplayWindow(hWnd);
Message();
return TRUE;
}
//用了演示资源脚本中的各种资源除了对话框和位图
//
#include "stdafx.h"
#include "resource.h"
#include "windows08.h"
#include "stdio.h"
#include "windows.h"
HINSTANCE g_hInst=NULL;
HANDLE g_hConsole=NULL;
/*
* WM_NCHITTEST and MOUSEHOOKSTRUCT Mouse Position Codes
#define HTERROR (-2)
#define HTTRANSPARENT (-1)
#define HTNOWHERE 0
#define HTCLIENT 1
#define HTCAPTION 2
#define HTSYSMENU 3
#define HTGROWBOX 4
#define HTSIZE HTGROWBOX
#define HTMENU 5
#define HTHSCROLL 6
#define HTVSCROLL 7
#define HTMINBUTTON 8
#define HTMAXBUTTON 9
#define HTLEFT 10
#define HTRIGHT 11
#define HTTOP 12
#define HTTOPLEFT 13
#define HTTOPRIGHT 14
#define HTBOTTOM 15
#define HTBOTTOMLEFT 16
#define HTBOTTOMRIGHT 17
#define HTBORDER 18
#define HTREDUCE HTMINBUTTON
#define HTZOOM HTMAXBUTTON
#define HTSIZEFIRST HTLEFT
#define HTSIZELAST HTBOTTOMRIGHT
#if(WINVER >= 0x0400)
#define HTOBJECT 19
#define HTCLOSE 20
#define HTHELP 21
#endif WINVER >= 0x0400 */
//这个函数实现:当鼠标位于客户区的不同的位置的时候,鼠标的样式会发生变化。
BOOL OnSetCursor(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HCURSOR hCursor=NULL;
WORD wPosition=LOWORD(lParam);
if (wPosition!=HTCLIENT)//如果鼠标没有在客户区
{
return FALSE;
}
RECT reClient={0};
POINT pt={0};
GetClientRect(hWnd,&reClient);//取得窗口客户区大小
GetCursorPos(&pt);//取得光标位置,是屏幕坐标
ScreenToClient(hWnd,&pt);
if (pt.x<reClient.right/2)//设置鼠标在客户区的四个矩形区域内的不同的光标模式
{
if (pt.y<reClient.bottom/2)
{
hCursor=LoadCursor(g_hInst,IDC_ARROW);
}
else
{
hCursor=LoadCursor(g_hInst,IDC_CROSS);
}
}
else
{
if (pt.y>reClient.bottom/2)
{
hCursor=LoadCursor(g_hInst,IDC_WAIT);
}
else
{
hCursor=LoadCursor(g_hInst,IDC_SIZEALL);
}
}
SetCursor(hCursor);
return TRUE;
}
VOID OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
MessageBox(NULL,TEXT("WM_CREATE"),TEXT("OnCreate()函数执行!"),MB_OK);
}
VOID OnCommand(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDM_EXIT:
PostQuitMessage(0);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SETCURSOR://用来更改客户区不同区域的光标的消息
if (TRUE==OnSetCursor(hWnd,uMsg,wParam,lParam))
{
return 0;
}
break;
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
case WM_COMMAND://用来处理菜单消息
OnCommand(hWnd,uMsg,wParam,lParam);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL RegisterWnd(TCHAR * pszClassName )
{
WNDCLASSEX wce={0};
wce.cbClsExtra=0;
wce.cbSize=sizeof(wce);
wce.cbWndExtra=0;
wce.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
wce.hCursor=NULL;
wce.hIcon=LoadIcon(g_hInst,MAKEINTRESOURCE(IDI_ICON1));
//wce.hIcon=(HICON)LoadImage(NULL, L"warcraft1.ico", IMAGE_ICON, 48, 48, LR_LOADFROMFILE);//网友说不要用LoadIcon()这样的老函数了,使用LoadImage()这样的新的函数。
wce.hIconSm=LoadIcon(g_hInst,MAKEINTRESOURCE(IDI_ICON1));;
wce.hInstance=g_hInst;
wce.lpfnWndProc=WndProc;
wce.lpszClassName=pszClassName;
wce.lpszMenuName=NULL;
wce.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
ATOM result=RegisterClassEx(&wce);
if (result==0)
{
MessageBox(NULL,TEXT("窗口类注册失败!"),TEXT("窗口类注册失败!"),MB_OK);
return FALSE;
}
return TRUE;
}
VOID DisplayWindow(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
VOID Message(HWND hWnd)
{
HACCEL hAccel=LoadAccelerators(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS08));
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateAccelerator(hWnd,hAccel,&msg);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND CreateMyWindow(TCHAR * pszClassName)
{
//加载字符串资源
TCHAR szText[100]=TEXT("");
LoadString(g_hInst,IDS_STRINGMain,szText,100);
//加载菜单资源
HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS08));
HWND hWndNew=CreateWindowEx(0,pszClassName,szText,WS_OVERLAPPEDWINDOW|WS_THICKFRAME,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
return hWndNew;
}
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
RegisterWnd(TEXT("MyWnd"));
HWND hWnd=CreateMyWindow(TEXT("MyWnd"));
DisplayWindow(hWnd);
Message(hWnd);
return TRUE;
}