15,979
社区成员
发帖
与我相关
我的任务
分享
/******************************************************************************************
* @name OnPaint
* @brief 窗体绘制处理程序,以下是关于窗体重绘的简短说明
If you add a minimize button to your dialog, you will need the code below
to draw the icon. For MFC applications using the document/view model,
this is automatically done for you by the framework.
当调用InvalidWindow,UpdateWindow和RedrawWindow等函数时
也就是发送了WM_PAINT消息时该事件会响应,另外还有类似相关的一些函数如下:
1.OnPaint: WM_PAINT
消息函数,接受到WM_PAINT消息时,首先调用的是OnPaint(),OnPaint()再调用OnDraw()
OnPaint()--OnDraw();
2.OnDraw:虚函数,需要重载
3.OnDrawItem: WM_DRAWITEM
子控件有自画属性且控件需重画时,父窗口会调用该函数
在具有Owner Draw属性的控件需要重画的时候,就会激发OnDrawItem
当自画子按钮控件、组合框控件、列表框控件或菜单的可视部分需要被画出时调用这个函数
OnDrawItem()--DrawItem();
4.DrawItem:虚函数,需要重载
如果使用DrawItem来自画控件,需要给控件加上自画样式,然后重载该控件类的自画函数(DrawItem)函数,如果该控件的父窗口提供了ON_WM_DRAWITEM消息映射宏,
并重载了OnDrawItem函数,则重画消息会由父窗口处理,父窗口调用基类的OnDrawItem来调用派生的子控件的DrawItem函数.
* @param 无
* @retval 无
******************************************************************************************/
void HID5000PSoundCardDlg::OnPaint()
{
//添加主界面背景图片
CRect InterfaceRect;
CPaintDC Interfacedc(this);
CBitmap Interfacebitmap;
CBrush Interfacebrush;
CBrush* pOldInterfaceBrush;
Interfacebitmap.LoadBitmap(IDB_BITMAP_BACKGROUND); //这个IDB_BITMAP1要自己添加,文件被定义在resource.h文件中,可以通过Easy USB 51 Programer Test1.rc这个文件来进行资源管理
Interfacebrush.CreatePatternBrush(&Interfacebitmap);//用于绘制背景图的画笔,使用上面的背景图片
pOldInterfaceBrush=Interfacedc.SelectObject(&Interfacebrush);
GetWindowRect(&InterfaceRect);//得到要绘图的区域,如果需要获得窗体在屏幕上的位置,使用GetWindowRect
//InterfaceRect.Width();//宽度
//InterfaceRect.Height();//高度
Interfacedc.Rectangle(0,0,InterfaceRect.Width(),InterfaceRect.Height()); // 这些参数可以调整图片添加位置和大小
//Interfacedc.Rectangle(50,50,InterfaceRect.Width(),InterfaceRect.Height()); // 这些参数可以调整图片添加位置和大小
Interfacedc.SelectObject(pOldInterfaceBrush);
if (IsIconic())//窗口是否处于最小化状态
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);//获取屏幕的像素大小,参数是图标
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);// 得到的是在客户区坐标系下的RECT(即以所在窗口左上角为原点,去掉了标题栏计算,仅仅是个大小,返回值的左上角永远为0,0)
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else//否则直接执行OnPaint函数,这个是父类CDialog的静态方法
{
CDialog::OnPaint();
}
}
Interfacebitmap.DeleteObject();
Interfacebrush.DeleteObject();