Ê×Ò³ Ê«´Ê ×Öµä °å±¨ ¾ä×Ó ÃûÑÔ ÓÑ´ð ÀøÖ¾ ѧУ ÍøÕ¾µØÍ¼
µ±Ç°Î»ÖÃ: Ê×Ò³ > ½Ì³ÌƵµÀ > ¿ª·¢ÓïÑÔ > VC/MFC >

»ùÓÚCDialogµÄ¶Ô»°¿ò»­Í¼¿ÉÒÔ²»ÔÚOnPaintº¯ÊýÖÐÂ𣿽â¾ö˼·

2012-12-14 
»ùÓÚCDialogµÄ¶Ô»°¿ò»­Í¼¿ÉÒÔ²»ÔÚOnPaintº¯ÊýÖÐÂð£¿ÎÒÓÐÒ»¸ö¼Ì³Ð×ÔCDialogµÄ¶Ô»°¿ò£¬ÎÒÏëÔÚÉÏÃæ»­Ò»¸ö±ß¾àÔ¤

»ùÓÚ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º¯Êý£¬Èç¹ûÄã·Åµ½ÆäËûµÄµØ·½Òª¿¼ÂǺʹ¦ÀíµÄÎÊÌâºÜ¶àµÄ±ÈÈç´°¿Ú¸²¸ÇÕÚµ²£¬Òƶ¯£¬À­Éý£¬×î´ó×îС´°¿ÚµÈµÈ
[ÆäËû½âÊÍ]
ÒýÓÃ:
ÎÒÒ²ÊÔ¹ýÖØÐ´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ÓõIJ»¶ÔÂð£¿
ÎÒÊÔ¹ý
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 : Œg×°¥Õ¥¡¥¤¥ë
//

#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);

}


[ÆäËû½âÊÍ]
Èç¹û¶Ô»°¿òÓб³¾°Í¼Æ¬£¬ÔÚ±³¾°Í¼Æ¬ÖлæÖÆÍ¼ÐΣ¬Ò²»áÓöµ½×î´ó»¯ºÍ×îС»¯»¹Óи²¸ÇµÄÎÊÌ⣿

ÈȵãÅÅÐÐ