基于CDialog的对话框画图可以不在OnPaint函数中吗?
我有一个继承自CDialog的对话框,我想在上面画一个边距预览的图,通过画线来实现,不重写OnPaint函数可以吗?
我在初始化时执行画图,但是显示不出来。
代码如下:
BOOL DlgPreviewMargin::OnInitDialog()
{
CDialog::OnInitDialog();
...
// Initial preview area
mp_staticPreview = (CStatic *)GetDlgItem(IDC_STATIC_PREVIEW);
CRect rect, textRect;
int previewWidth = 100;
mp_staticPreview->GetClientRect(&rect);
rect.top += 5;
rect.bottom -= 2;
rect.left += (rect.Width() - previewWidth) / 2;
rect.right = rect.left + previewWidth;
textRect.left = rect.left + 10;
textRect.top = rect.top + 10;
textRect.right = rect.right - 10;
textRect.bottom = rect.bottom - 10;
drawPreview(rect, textRect);
...
}
/*
* Draw margin preview
*/
void DlgPreviewMargin::drawPreview(CRect borderRect, CRect textRect)
{
//CClientDC dc(this); // device context for painting
//RECT rect;
//GetClientRect(&rect);
CDC *pDC = mp_staticPreview->GetDC();
mp_staticPreview->Invalidate();
mp_staticPreview->UpdateWindow();
// set background to white
pDC->FillSolidRect(borderRect, (COLORREF)RGB(255, 255, 255));
CPen *pGreenPen=new CPen(PS_SOLID,2,RGB(0,0,0));
CGdiObject* pOldBrush=pDC->SelectObject(pGreenPen);
// draw border
pDC->MoveTo(borderRect.left,borderRect.top);
pDC->LineTo(borderRect.left,borderRect.bottom);
pDC->LineTo(borderRect.right,borderRect.bottom);
pDC->LineTo(borderRect.right,borderRect.top);
pDC->LineTo(borderRect.left,borderRect.top);
// draw lines
pDC->MoveTo(textRect.left,textRect.top);
pDC->LineTo(textRect.right,textRect.top);
}
[最优解释]
当然最好是写Onpaint方法,如果需要刷新界面
Invalidte(FALSE);
//如果要立即刷新加上 UpdateWindow();
[其他解释]
最好是把绘图的操作放到OnPaint函数,如果你放到其他的地方要考虑和处理的问题很多的比如窗口覆盖遮挡,移动,拉升,最大最小窗口等等
[其他解释]
需要移动,改变大小等才会触发重绘
[其他解释]
InvalidateRect
强制刷新,就会触发OnPaint了
[其他解释]
Invalidate(TRUE)就执行到了。
[其他解释]
我也试过重写OnPaint()函数,但是执行不到此函数,不知道为什么。
[其他解释]
最后是要放在OnPaint()里面,但现在是连显示都显示不了,OnPaint函数也执行不到。
我重新建立一个MFC多文档的测试工程,然后增加了一个Dialog,增加一个基于Dialog的类,重写OnInitDialog,OnPaint函数,只简单的画一条线,都画不出来。奇怪了!
在此工程的初始化中添加上起到此dialog的代码
MyTest mtest;
mtest.DoModal();
只光秃秃一个对话框,但就是没有线画出来。
BOOL MyTest::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: ここに初期化を追加してください
drawPreview();
return TRUE; // return TRUE unless you set the focus to a control
// 例外 : OCX プロパティ ペジは必ず FALSE を返します。
}
void MyTest::OnPaint()
{
CDialog::OnPaint();
drawPreview();
}
/*
* Draw margin preview
*/
void MyTest::drawPreview()
{
CClientDC dc(this); // device context for painting
CRect rect;
GetClientRect(&rect);
rect.left += 20;
rect.top += 20;
rect.right -= 20;
rect.bottom -= 20;
// draw lines
dc.MoveTo(rect.left,rect.top);
dc.LineTo(rect.right,rect.top);
// force to update screen
Invalidate(TRUE);
}
[其他解释]
是dc用的不对吗?
我试过
CPaintDC dc(this);
CClientDC dc(this);
都画不出来。
[其他解释]
测试程序的完整代码
#pragma once
// MyTest ダイアログ
class MyTest : public CDialog
{
DECLARE_DYNAMIC(MyTest)
public:
MyTest(CWnd* pParent = NULL); // コンストラクタ
virtual ~MyTest();
// ダイアログ デタ
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV サポト
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint(); // overide OnPaint()
public:
virtual BOOL OnInitDialog();
public:
void drawPreview();
};
// MyTest.cpp : 装ファイル
//
#include "stdafx.h"
#include "Test123.h"
#include "MyTest.h"
// MyTest ダイアログ
IMPLEMENT_DYNAMIC(MyTest, CDialog)
MyTest::MyTest(CWnd* pParent /*=NULL*/)
: CDialog(MyTest::IDD, pParent)
{
}
MyTest::~MyTest()
{
}
void MyTest::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(MyTest, CDialog)
END_MESSAGE_MAP()
// MyTest メッセジ ハンドラ
BOOL MyTest::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: ここに初期化を追加してください
drawPreview();
return TRUE; // return TRUE unless you set the focus to a control
// 例外 : OCX プロパティ ペジは必ず FALSE を返します。
}
void MyTest::OnPaint()
{
CDialog::OnPaint();
drawPreview();
}
/*
* Draw margin preview
*/
void MyTest::drawPreview()
{
CClientDC dc(this); // device context for painting
CRect rect;
GetClientRect(&rect);
rect.left += 20;
rect.top += 20;
rect.right -= 20;
rect.bottom -= 20;
// draw lines
dc.MoveTo(rect.left,rect.top);
dc.LineTo(rect.right,rect.top);
// force to update screen
Invalidate(TRUE);
}
[其他解释]
如果对话框有背景图片,在背景图片中绘制图形,也会遇到最大化和最小化还有覆盖的问题?