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);
}
如此便可实现我们想要的效果
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);
}
如此便可实现我们想要的效果
相关文章推荐
- LeetCode58----Length of Last Word
- C# 面向对象的三大特性
- sqlite or svn 错误 The database disk image is malformed 可解决
- 第一周 项目0 宣告“主权”
- 第一次博文
- 宣告主权
- Mac 上配置Python开发环境 Pydev + Eclipse
- js打印对象的方法
- 第2周SHH数据结构—【项目1 - C/C++语言中函数参数传递的三种方式】
- hibernate中一对多关系的映射
- 1232 -- 找模式串
- 1231 -- 金子上的友情
- apns 官方文档
- Java中枚举的7种用法
- 《IOS_OC》继承、多态、初始化方法、便利构造器
- IOS_UI_UITableView 应用
- 蓝桥杯:翻硬币
- python中的高阶函数
- Sql数据库批量清理日志
- mysql中insert和replace的区别