您的位置:首页 > 其它

1.6.2 简单自绘控件——无边框对话框最小化按钮

2015-09-07 17:00 417 查看
1、准备PNG文件:



2、插入Static Text 控件,并将其设置在窗口右上角

CRect
rect;
GetClientRect(rect);
//获取对话框大小
CWnd
*pWnd;

pWnd
= GetDlgItem(IDC_STATIC_MIN);
pWnd
-> SetWindowPos(NULL,rect.Width()-39-28,
0,28,20,SWP_NOZORDER
);

3、设置Static Text 控件背景透明

该步骤将实现以下目的:显示透明PNG图片

类向导添加ON_WM_CTLCOLOR()消息处理函数,并在OnCtlColor()其中插入以下代码



if
( pWnd->
GetDlgCtrlID()
==
IDC_STATIC_MIN)
{
pDC->SetBkMode(TRANSPARENT);
//设置背景透明
pDC->SetTextColor(RGB(255,255,255));
return
HBRUSH(GetStockObject(HOLLOW_BRUSH));
}



4、图标动态化

该步骤将实现以下目的:鼠标移入Static_Close控件区域,更换较亮的图标;不在该空间区域,更换普通图标。

类向导添加ON_WM_MOUSEMOVE()消息处理函数,并在OnMouseMove ()其中插入以下代码:



CWnd
* pWndParent
= this->GetParent();

/*****CLOSE*****/
CRect
rect;
CClientDC
*pDC
=
new CClientDC(GetDlgItem(IDC_STATIC_CLOSE));
Graphics
graphics(pDC->m_hDC);
// Create a GDI+ graphics object
GetDlgItem(IDC_STATIC_CLOSE)->GetWindowRect(&rect);
ScreenToClient(&rect);
Image
image(L"res\\BT_CLOSE.png",FALSE);
// Construct animage

static
bool m_bcloseflag
= 0;//限制同样图片重绘次数
CRect
mrect(380-39-28,0,380,20);//将要刷新的区域放在一起
if
(point.x>rect.TopLeft().x
&&
point.x<rect.BottomRight().x
&&
point.y>rect.TopLeft().y
&&
point.y<rect.BottomRight().y)
{
if
(m_bcloseflag)
{
this->InvalidateRect(mrect);//刷新指定区域,放在绘图前面
UpdateWindow();//刷新指定区域
graphics.DrawImage(&image,
-38,
0,
156,
20);

m_bcloseflag
=
0;
}
}
else
{
if
(!m_bcloseflag)
{
this->InvalidateRect(&mrect);//刷新指定区域
UpdateWindow();//刷新指定区域
graphics.DrawImage(&image,
0,
0,
156,
20);
m_bcloseflag
=
1;
}

}

/*****MIN*****/
pDC
= new
CClientDC(GetDlgItem(IDC_STATIC_MIN));
Graphics
graphics1(pDC->m_hDC);
// Create a GDI+ graphics object

GetDlgItem(IDC_STATIC_MIN)->GetWindowRect(&rect);
ScreenToClient(&rect);
Image
image1(L"res\\BT_MIN.png",FALSE);
// Construct animage

static
bool m_bminflag
= 0;//限制同样图片重绘次数

if
(point.x>rect.TopLeft().x
&&
point.x<rect.BottomRight().x
&&
point.y>rect.TopLeft().y
&&
point.y<rect.BottomRight().y)
{
if
(m_bminflag)
{
this->InvalidateRect(mrect);//刷新指定区域
UpdateWindow();//刷新指定区域
graphics1.DrawImage(&image1,
-28,
0,
112,
20);

m_bminflag
=
0;
}

}
else
{
if
(!m_bminflag)
{
this->InvalidateRect(mrect);//刷新指定区域
UpdateWindow();//刷新指定区域
graphics1.DrawImage(&image1,
0,
0,
112,
20);

m_bminflag
=
1;
}

}

5、鼠标左键消息反应

该步骤将实现以下功能:在指定区域按下鼠标左键,将关闭对话框

类向导添加ON_WM_LBUTTONDOWN ()消息处理函数,并在OnLButtonDown ()其中插入以下代

//最小化实现
GetDlgItem(IDC_STATIC_MIN)->GetWindowRect(&rect);
ScreenToClient(&rect);
if
(point.x>rect.TopLeft().x
&&
point.x<rect.BottomRight().x
&&
point.y>rect.TopLeft().y
&&
point.y<rect.BottomRight().y)
{
ShowWindow(SW_MINIMIZE);
}

如此便可实现我们想要的效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: