ex16dlg.cpp

来自「一些VC++的经典实例」· C++ 代码 · 共 271 行

CPP
271
字号
// ex16Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ex16.h"
#include "ex16Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEx16Dlg dialog

CEx16Dlg::CEx16Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEx16Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEx16Dlg)
		// NOTE: the ClassWizard will add member initialization here
	type=2;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CEx16Dlg, CDialog)
	//{{AFX_MSG_MAP(CEx16Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_CLEAR, OnClear)
	ON_BN_CLICKED(ID_DYN, OnDyn)
	ON_BN_CLICKED(ID_PT, OnPt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx16Dlg message handlers

BOOL CEx16Dlg::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 CEx16Dlg::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 CEx16Dlg::OnPaint() 
{
	int i,j;
	
	CWnd * pWnd=GetDlgItem(IDC_STATIC);
	CDC * pEditDC=pWnd->GetDC();
	CRect rect;
	CBrush WhiteBrush;
	CBrush GreenBrush;
	CPen bigPen;
	CPen dashPen;
	CBitmap m_BMP1;
	CPoint m_point[4];
	CSize m_Size;
	WhiteBrush.CreateSolidBrush(RGB(255,255,255));
	GreenBrush.CreateSolidBrush(RGB(0,255,0));
	bigPen.CreatePen(PS_DASH,10,RGB(0,255,255));
	dashPen.CreatePen(PS_DASH,1,RGB(255,0,255));
	m_BMP1.LoadBitmap(IDB_BITMAP1);
	m_Size.cx=m_Size.cy=32;
	
	pWnd->Invalidate();
	pWnd->UpdateWindow();
	pWnd->GetClientRect(&rect);
	m_point[0].x=rect.left+100;
	m_point[0].y=rect.top+100;
	m_point[2].x=rect.left+50;
	m_point[2].y=rect.top+10;
	m_point[3].x=rect.left+50;
	m_point[3].y=rect.top+150;
	pEditDC->SelectObject(&WhiteBrush);
	pEditDC->Rectangle(&rect);
	
	switch(type)
	{
		case 0:
			for(i=0;i<rect.right-rect.left;i++)
			{
				for(j=0;j<rect.bottom-rect.top;j++)
				{
					m_point[1].x=rect.left+i;
					m_point[1].y=rect.top+j;
					pEditDC->SetPixel(m_point[1],RGB(0,0,255));
				}
				Sleep(10);
			}
			break;
			
		case 1:
			pEditDC->SelectObject(&GreenBrush);
			pEditDC->Rectangle(&rect);
			pEditDC->Ellipse(&rect);
			pEditDC->DrawState(m_point[0],m_Size,m_BMP1,DST_BITMAP);
			pEditDC->MoveTo(m_point[2]);
			pEditDC->LineTo(m_point[3]);
			pEditDC->SelectObject(&bigPen);
			pEditDC->MoveTo(m_point[2].x+30,m_point[2].y);
			pEditDC->LineTo(m_point[3].x+30,m_point[3].y);
			pEditDC->SelectObject(&dashPen);
			pEditDC->MoveTo(m_point[2].x-30,m_point[2].y);
			pEditDC->LineTo(m_point[3].x-30,m_point[3].y);
			break;
		case 2:
			pEditDC->SelectObject(&WhiteBrush);
			pEditDC->Rectangle(&rect);
			break;
		default:
			break;

	}

	WhiteBrush.DeleteObject();
	GreenBrush.DeleteObject();
	bigPen.DeleteObject();
	dashPen.DeleteObject();
	m_BMP1.DeleteObject();
	
	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
	{
		CDialog::OnPaint();
	}
}

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

void CEx16Dlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	type=2;
	Invalidate();
}

void CEx16Dlg::OnDyn() 
{
	// TODO: Add your control notification handler code here
	type=0;
	Invalidate();
}

void CEx16Dlg::OnPt() 
{
	// TODO: Add your control notification handler code here
	type=1;
	Invalidate();
}

⌨️ 快捷键说明

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