一、建立菜单
struct GameMenu
{
int id;//按钮ID
TCHAR szName[10];//按钮名称
int xPos;//距离客户区左边的位置
int yPos;//距离客户区顶部的位置
}menu[] = {
{ 1, TEXT(“开始游戏”), 345, 250 },
{ 2, TEXT(“查看帮助”), 345, 300 },
{ 3, TEXT(“作者博客”), 345, 350 },
{ 4, TEXT(“退出游戏”), 345, 400 }
};
二、绘制菜单
HDC hdc = GetDC(hwnd);//建立设备DC
HPEN hPen;//创建画笔
HFONT hFont;//创建字体
HBRUSH hBrush;//创建画刷
LOGFONT lf = { 0 };
wcscpy(lf.lfFaceName, L"隶书");
lf.lfWidth = 12;
lf.lfHeight = 25;
lf.lfWeight = FW_NORMAL;
lf.lfCharSet = GB2312_CHARSET;
hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 255));
hFont = CreateFontIndirect(&lf);
hBrush = CreateSolidBrush(RGB(0, 0, 0));
//选择对象到hdc
SelectObject(hdc, hPen);
SelectObject(hdc, hFont);
SelectObject(hdc, hBrush);
SetBkMode(hdc, TRANSPARENT);
SetBkColor(hdc, RGB(0, 0, 0));
SetTextCharacterExtra(hdc, 3);
SetTextColor(hdc, RGB(255, 255, 255));
int i = 0;
for (i; i < 4; i++)
TextOut(hdc, menu[i].xPos, menu[i].yPos, menu[i].szName, lstrlen(menu[i].szName));
DeleteObject(hPen);
DeleteObject(hFont);
DeleteObject(hBrush);
三、菜单的处理
虽然我们已经建立的菜单,但这样的菜单是没什么用的,还需要添加相应的处理函数。处理的原理简单说来就是判断鼠标位置-》响应不同的处理消息进行处理。
1.判断鼠标消息
首先我们建立一个计时器,出发WM_TIMER小时,用来持续检测鼠标位置。
SetTimer(hwnd, 1, 100, NULL);
2.位置判断
//返回位置1,2,3,4,分别代表4个按钮。
int DealMouseMove(HWND hwnd)
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
if (pt.x < 340) return 0;
if (pt.x > 450) return 0;
if (pt.y < 245) return 0;
if (pt.y > 435) return 0;
if (pt.y > 250 && pt.y < 285)
return 1;
if (pt.y > 300 && pt.y < 335)
return 2;
if (pt.y > 350 && pt.y < 385)
return 3;
if (pt.y > 400 && pt.y < 435)
return 4;
return 0;
}
3.在 WM_TIMER消息中添加处理函数
//menuID,oldMenuID 为全局变量,用以保存
menuID = DealMouseMove(hwnd);//得到ID
//处理鼠标悬停
if (menuID != oldMenuID && menuID > 0)
{
DrawSelectedMenu(hwnd, menuID-1);
oldMenuID = menuID;
}
处理悬停代码如下:
void creatmenu2(HWND hwnd)
{
HDC hdc = GetDC(hwnd);
HPEN hPen;
HFONT hFont;
HBRUSH hBrush;
LOGFONT lf = { 0 };
wcscpy(lf.lfFaceName, L"隶书");
lf.lfWidth = 12;
lf.lfHeight = 25;
lf.lfWeight = FW_NORMAL;
lf.lfCharSet = GB2312_CHARSET;
hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 255));
hFont = CreateFontIndirect(&lf);
hBrush = CreateSolidBrush(RGB(0, 0, 0));
SelectObject(hdc, hPen);
SelectObject(hdc, hFont);
SelectObject(hdc, hBrush);
SetBkMode(hdc, TRANSPARENT);
SetBkColor(hdc, RGB(0, 0, 0));
SetTextCharacterExtra(hdc, 3);
SetTextColor(hdc, RGB(255, 255, 255));
int i = 0;
for (i; i < 4; i++)
TextOut(hdc, menu[i].xPos, menu[i].yPos, menu[i].szName, lstrlen(menu[i].szName));
DeleteObject(hPen);
DeleteObject(hFont);
DeleteObject(hBrush);
}
void DrawBlackText(HWND hwnd,int i)
{
HDC hdc = GetDC(hwnd);
HPEN hPen;
HFONT hFont;
HBRUSH hBrush;
LOGFONT lf = { 0 };
wcscpy(lf.lfFaceName, L"隶书");
lf.lfWidth = 12;
lf.lfHeight = 25;
lf.lfWeight = FW_NORMAL;
lf.lfCharSet = GB2312_CHARSET;
hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 255));
hFont = CreateFontIndirect(&lf);
hBrush = CreateSolidBrush(RGB(0, 0, 0));
SelectObject(hdc, hPen);
SelectObject(hdc, hFont);
SelectObject(hdc, hBrush);
SetBkMode(hdc, TRANSPARENT);
SetBkColor(hdc, RGB(0, 0, 0));
SetTextCharacterExtra(hdc, 3);
SetTextColor(hdc, RGB(0, 0, 0));
TextOut(hdc, menu[i].xPos, menu[i].yPos, menu[i].szName, lstrlen(menu[i].szName));
DeleteObject(hPen);
DeleteObject(hFont);
DeleteObject(hBrush);
}
void DrawSelectedMenu(HWND hwnd,int i)
{
creatmenu2(hwnd);
DrawBlackText(hwnd, i);
}
void DrawSelectedMenu(HWND hwnd,int i)
{
creatmenu2(hwnd);
DrawBlackText(hwnd, i);
}
最终效果如图:
四。处理鼠标点击时间,这一部分主要是利用menuID这个全局变量,在WM_LBUTTONDOWN消息中添加处理函数
如:
switch (menuID)
{
case 1:// 点击开始,打开百度
ShellExecute(NULL, L"open", L"http://www.baidu.com", NULL, NULL, SW_SHOW);
break;
case 4:// 点击退出,退出程序
PostQuitMessage(0);
break;