做了一个能用的例子: // DrawDlg.h : header file //#if !defined(AFX_DRAWDLG_H__DBD98072_5313_4D48_8485_C102C4DDAA90__INCLUDED_) #define AFX_DRAWDLG_H__DBD98072_5313_4D48_8485_C102C4DDAA90__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000///////////////////////////////////////////////////////////////////////////// // CDrawDlg dialogclass CDrawDlg : public CDialog { // Construction public: CBitmap* m_bitmap; CDrawDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data //{{AFX_DATA(CDrawDlg) enum { IDD = IDD_DRAW_DIALOG }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CDrawDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CDrawDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnDestroy(); //}}AFX_MSG DECLARE_MESSAGE_MAP() };//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_DRAWDLG_H__DBD98072_5313_4D48_8485_C102C4DDAA90__INCLUDED_)// DrawDlg.cpp : implementation file //#include "stdafx.h" #include "Draw.h" #include "DrawDlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog { public: CAboutDlg();// Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT }void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CDrawDlg dialogCDrawDlg::CDrawDlg(CWnd* pParent /*=NULL*/) : CDialog(CDrawDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDrawDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_bitmap=NULL; }void CDrawDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDrawDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CDrawDlg, CDialog) //{{AFX_MSG_MAP(CDrawDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_LBUTTONDOWN() ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CDrawDlg message handlersBOOL CDrawDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here CRect rc; GetClientRect(&rc); CPaintDC dc(this); m_bitmap=new CBitmap(); m_bitmap->CreateCompatibleBitmap(&dc,rc.Width(),rc.Height()); CDC myDC; myDC.CreateCompatibleDC(&dc); CBitmap *pOldBitmap = myDC.SelectObject(m_bitmap); CBrush mybrush(RGB(255,255,255)); myDC.FillRect(&rc,&mybrush); myDC.SelectObject(pOldBitmap); myDC.DeleteDC(); return TRUE; // return TRUE unless you set the focus to a control }void CDrawDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } }// 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.void CDrawDlg::OnPaint() { 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); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CPaintDC dc(this); if(!m_bitmap) return; BITMAP bmp; m_bitmap->GetBitmap(&bmp); CDC cdc; cdc.CreateCompatibleDC(&dc); CBitmap* pOldbm; pOldbm=cdc.SelectObject(m_bitmap); dc.BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&cdc,0,0,SRCCOPY); cdc.SelectObject(pOldbm); CDialog::OnPaint(); } }// The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDrawDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; }void CDrawDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CPaintDC dc(this); CDC myDC; myDC.CreateCompatibleDC(&dc); CBitmap *pOldBitmap = myDC.SelectObject(m_bitmap); //_____________________画图 CBrush brush; brush.CreateSolidBrush(RGB(255,0,0)); myDC.SelectObject(&brush); CRect rcShape (point.x - 4, point.y - 4, point.x + 4, point.y + 4); myDC.Ellipse(rcShape); //_____________________ myDC.SelectObject(pOldBitmap); myDC.DeleteDC(); Invalidate(FALSE); CDialog::OnLButtonDown(nFlags, point); }void CDrawDlg::OnDestroy() { CDialog::OnDestroy();
// TODO: Add your message handler code here delete m_bitmap; }
OnLButtonDown时把圆画进内存Bitmap
OnPaint时把内存Bitmap BitBlt出来或者
建立一个类CCircle,由CCircle自己负责画圆
OnLButtonDown时new一个CCircle,放进一个全局的CPtrArray
OnPaint时把CPtrArray里所有的CCircle都取出来画
{
// TODO: Add your message handler code here and/or call default
mypoint=point;
flag=1;
CTest29Dlg::OnPaint();
CDialog::OnLButtonDown(nFlags, point);
}
void CTest29Dlg::OnPaint()
{
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);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{ if(flag==1)
{
CPaintDC dc(this);
CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));
dc.SelectObject(&brush);
CRect rcShape (mypoint.x - 4, mypoint.y - 4, mypoint.x + 4, mypoint.y + 4);
dc.Ellipse(rcShape);
Invalidate();
}
CDialog::OnPaint();
}
}
{
// TODO: Add your message handler code here and/or call default
mypoint=point;
flag=1;
Invalidate();
CDialog::OnLButtonDown(nFlags, point);
}
{
if (IsIconic())
{
............
}
else
{
if(flag==1)
{
CPaintDC dc(this);
CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));
dc.SelectObject(&brush);
CRect rcShape (mypoint.x - 4, mypoint.y - 4, mypoint.x + 4, mypoint.y + 4);
dc.Ellipse(rcShape);
//Invalidate(); ***************去掉这一句,这会造成死循环**********
}
CDialog::OnPaint();
}
}
// DrawDlg.h : header file
//#if !defined(AFX_DRAWDLG_H__DBD98072_5313_4D48_8485_C102C4DDAA90__INCLUDED_)
#define AFX_DRAWDLG_H__DBD98072_5313_4D48_8485_C102C4DDAA90__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
// CDrawDlg dialogclass CDrawDlg : public CDialog
{
// Construction
public:
CBitmap* m_bitmap;
CDrawDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
//{{AFX_DATA(CDrawDlg)
enum { IDD = IDD_DRAW_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDrawDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; // Generated message map functions
//{{AFX_MSG(CDrawDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_DRAWDLG_H__DBD98072_5313_4D48_8485_C102C4DDAA90__INCLUDED_)// DrawDlg.cpp : implementation file
//#include "stdafx.h"
#include "Draw.h"
#include "DrawDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:
CAboutDlg();// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CDrawDlg dialogCDrawDlg::CDrawDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDrawDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDrawDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_bitmap=NULL;
}void CDrawDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDrawDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CDrawDlg, CDialog)
//{{AFX_MSG_MAP(CDrawDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CDrawDlg message handlersBOOL CDrawDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CRect rc;
GetClientRect(&rc); CPaintDC dc(this);
m_bitmap=new CBitmap();
m_bitmap->CreateCompatibleBitmap(&dc,rc.Width(),rc.Height()); CDC myDC;
myDC.CreateCompatibleDC(&dc);
CBitmap *pOldBitmap = myDC.SelectObject(m_bitmap); CBrush mybrush(RGB(255,255,255));
myDC.FillRect(&rc,&mybrush);
myDC.SelectObject(pOldBitmap);
myDC.DeleteDC();
return TRUE; // return TRUE unless you set the focus to a control
}void CDrawDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}// 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.void CDrawDlg::OnPaint()
{
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);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
if(!m_bitmap)
return;
BITMAP bmp;
m_bitmap->GetBitmap(&bmp);
CDC cdc;
cdc.CreateCompatibleDC(&dc);
CBitmap* pOldbm;
pOldbm=cdc.SelectObject(m_bitmap);
dc.BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&cdc,0,0,SRCCOPY);
cdc.SelectObject(pOldbm);
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDrawDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CDrawDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPaintDC dc(this);
CDC myDC;
myDC.CreateCompatibleDC(&dc);
CBitmap *pOldBitmap = myDC.SelectObject(m_bitmap); //_____________________画图
CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));
myDC.SelectObject(&brush);
CRect rcShape (point.x - 4, point.y - 4, point.x + 4, point.y + 4);
myDC.Ellipse(rcShape); //_____________________
myDC.SelectObject(pOldBitmap);
myDC.DeleteDC();
Invalidate(FALSE);
CDialog::OnLButtonDown(nFlags, point);
}void CDrawDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
delete m_bitmap;
}