⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawdemodlg.cpp

📁 从学案例入手
💻 CPP
字号:
// DrawDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DrawDemo.h"
#include "DrawDemoDlg.h"

#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class 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()

/////////////////////////////////////////////////////////////////////////////
// CDrawDemoDlg dialog

CDrawDemoDlg::CDrawDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDrawDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDrawDemoDlg)
		// 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);
}

void CDrawDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDrawDemoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDrawDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDrawDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawDemoDlg message handlers

BOOL CDrawDemoDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDrawDemoDlg::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 CDrawDemoDlg::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);
		CPen pen1(PS_SOLID, 1, RGB(192, 192, 192)), pen2(PS_SOLID, 1, RGB(0, 0, 255)), *pOldPen;
		// 更改设备上下文所使用的当前字体,使之更适合于文本输出
		LOGFONT lf;
		dc.GetCurrentFont()->GetLogFont(&lf);
		lf.lfHeight=-12;
		lf.lfWidth=0;
		strcpy(lf.lfFaceName, "宋体");
		CFont font, *pOldFont;
		font.CreateFontIndirect(&lf);
		pOldFont=dc.SelectObject(&font);
		// 使用函数 Arc 和 ArcTo 输入弧形
		{
			pOldPen=dc.SelectObject(&pen1);
			dc.Rectangle(10, 10, 160, 110);
			dc.MoveTo(85, 60);
			dc.LineTo(160, 60);
			dc.MoveTo(85, 60);
			dc.LineTo(10, 10);
			dc.SelectObject(&pen2);
			dc.MoveTo(10,10);
			dc.ArcTo(10, 10, 160, 110, 160, 60, 10, 10);
			dc.Arc(10, 10, 160, 110, 10, 30, 160, 110);
			dc.TextOut(10, 115, "Arc & ArcTo");
		}
		// 使用函数 PolyPolyline 输出多段折线
		{
			dc.SelectObject(&pen1);
			dc.Rectangle(180, 10, 330, 110);
			CPoint pts[]={CPoint(190, 20), CPoint(200, 60), CPoint(270, 40), CPoint(210, 80), 
			CPoint(250, 100), CPoint(300, 30), CPoint(310, 80), CPoint(320, 50)};
			DWORD pps[]={5, 3};
			dc.SelectObject(&pen2);
			dc.PolyPolyline(pts, pps, 2);
			CRect rc(200, 30, 310, 90);
			dc.TextOut(180, 115, "PolyPolyline");
		}
		// 使用函数 DrawFocusRect 和 Draw3dRect 输出特殊样式的矩形
		{
			dc.SelectObject(&pen1);
			dc.Rectangle(350, 10, 500, 110);
			dc.SelectObject(&pen2);
			dc.DrawFocusRect(CRect(370, 25, 480, 95));
			dc.Draw3dRect(CRect(390, 40, 460, 80), RGB(192, 192, 192), RGB(64, 64, 64));
			dc.SetBkColor(RGB(255, 255, 255));
			dc.TextOut(350, 115, "Draw3dRect & DrawFocusRect");
		}
		// 使用 Pie 和 Chord 输出弓形和扇形
		{
			dc.SelectObject(&pen1);
			dc.Rectangle(10, 140, 160, 240);
			dc.SelectObject(&pen2);
			dc.Ellipse(10, 140, 160, 240);
			dc.Pie(20, 150, 150, 230, 160, 160, 10, 160);
			dc.Chord(20, 150, 150, 230, 10, 220, 160, 220);
			dc.TextOut(10, 245, "Ellipse, Pie & Chord");
		}
		// 使用 PolyDraw 输出贝塞尔曲线
		{
			dc.SelectObject(&pen1);
			dc.Rectangle(180, 140, 330, 240);
			dc.MoveTo(180, 140);
			dc.LineTo(330, 160);
			dc.MoveTo(330, 240);
			dc.LineTo(180, 220);
			dc.SelectObject(&pen2);
			CPoint pts[]={CPoint(330, 160), CPoint(180, 220), CPoint(330, 240)};
			BYTE typs[]={PT_BEZIERTO, PT_BEZIERTO, PT_BEZIERTO|PT_CLOSEFIGURE};
			dc.MoveTo(180, 140);
			dc.PolyDraw(pts, typs, 3);
			dc.TextOut(180, 245, "PolyDraw");
		}
		// 使用 Polygon 输出多边形
		{
			dc.SelectObject(&pen1);
			dc.Rectangle(350, 140, 500, 240);
			dc.Ellipse(375, 140, 475, 240);
			dc.SelectObject(&pen2);
			CPoint pts1[5];
			for (int i=0; i<=4; i++)
			{
				pts1[i].x=425+int(50.*cos(3.14159/2.5*i+3.14159/10.));
				pts1[i].y=190-int(50.*sin(3.14159/2.5*i+3.14159/10.));
			}
			CPoint pts2[]={pts1[0], pts1[2], pts1[4], pts1[1], pts1[3]};
			dc.Polygon(pts2, 5);
			dc.TextOut(350, 245, "Polygon");
		}
		// 恢复设备上下文原有的 GDI 绘图对象
		dc.SelectObject(pOldPen);
		dc.SelectObject(pOldFont);
		font.DeleteObject();
		pen1.DeleteObject();
		pen2.DeleteObject();
		
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDrawDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -