win32API 绘制文字型按钮

一、建立菜单
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;

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值