如何在MFC VC ++中的oninitdialog上绘制rect? [英] How can I draw rect on oninitdialog in MFC VC++?

查看:116
本文介绍了如何在MFC VC ++中的oninitdialog上绘制rect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我点击按钮点击我的应用程序中的矩形。

但我想在应用程序启动时绘制它。因为我正在使用OninitDialog函数,它不会在它上面绘制任何内容。



所以如何在启动应用程序时绘制它?



我尝试过:



void XYZ :: OnBnClickedOk()

{

CRect rc;

m_grpDisp.GetClientRect(& rc);

ScreenToClient(& rc) ;

int width = rc.Width();

int height = rc.Height();



//获取设备上下文

CClientDC dc(GetDlgItem(IDC_GDISP));

//创建新笔

CPen lDot(PS_DASH ,2,RGB(255,255,255));

CPen lPoint(PS_SOLID,4,RGB(0,0,255));

CPen lLine(PS_SOLID ,8,RGB(255,0,0));



CBrush灰色(RGB(240,240,240));

dc.SelectObject(& grey);

dc.Rectangle((width / 2)-445,8,(width / 2)+ 445,height-3);

}

Hello people.

I am drawing rect in my application on button click.
but i want draw it on start of application. as i am drawing on OninitDialog function it will not drawing anything on it.

so how can i draw it on starting application?

What I have tried:

void XYZ::OnBnClickedOk()
{
CRect rc;
m_grpDisp.GetClientRect(&rc);
ScreenToClient(&rc);
int width = rc.Width();
int height = rc.Height();

// Get the Device Context
CClientDC dc(GetDlgItem(IDC_GDISP));
// Create a new pen
CPen lDot(PS_DASH, 2, RGB(255, 255, 255));
CPen lPoint(PS_SOLID, 4, RGB(0, 0, 255));
CPen lLine(PS_SOLID, 8, RGB(255, 0, 0));

CBrush grey(RGB(240, 240, 240));
dc.SelectObject(&grey);
dc.Rectangle((width/2)-445,8,(width/2)+445,height-3);
}

推荐答案

如果你在f之外绘制对象响应WM_PAINT消息的unction,然后它将不起作用。一旦显示对话框,它将删除背景并重绘资源中的所有控件。您应该将矩形添加到资源设置中。
If you draw objects outside of the function that responds to the WM_PAINT message, then it will not work. As soon as the dialog is shown it will erase the background and redraw all controls in the resource. You should add the rectangle into your resource settings.


另一种解决方案是使用PostMessage获取WM_PAINT消息,该消息触发控件的绘制。最好是之前使用 InvalidateRect 作为控件。



提示:只创建一次笔,作为类成员或静态对象以提高性能。
Another solution is to use PostMessage for a WM_PAINT message which triggers the painting of your control. Best is to use InvalidateRect for the control before.

Tip: Create the pens only once, as class members or as static objects to improve performance.


这篇关于如何在MFC VC ++中的oninitdialog上绘制rect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆