»ùÓÚ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);
}
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);
}
#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);
}