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

📄 controldrawdlg.cpp

📁 控件的使用源代码。打开后可实现控件的应用 。
💻 CPP
字号:
// ControlDrawDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CControlDrawDlg dialog

CControlDrawDlg::CControlDrawDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CControlDrawDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CControlDrawDlg)
	m_thickness = -0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CControlDrawDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CControlDrawDlg)
	DDX_Control(pDX, IDC_DRAW, m_DrawCanvas);
	DDX_Control(pDX, IDC_SLIDERG, m_sliderG);
	DDX_Control(pDX, IDC_SLIDERR, m_sliderR);
	DDX_Control(pDX, IDC_SLIDERB, m_sliderB);
	DDX_Control(pDX, IDC_LISTSTYLE, m_listStyle);
	DDX_Control(pDX, IDC_COMBOMETHOD, m_comboMethod);
	DDX_Control(pDX, IDC_COMBOLINESTYLE, m_comboLineStyle);
	DDX_Radio(pDX, IDC_THICKNESS, m_thickness);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CControlDrawDlg, CDialog)
	//{{AFX_MSG_MAP(CControlDrawDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_HSCROLL()
	ON_LBN_SELCHANGE(IDC_LISTSTYLE, OnSelchangeListstyle)
	ON_CBN_SELCHANGE(IDC_COMBOMETHOD, OnSelchangeCombomethod)
	ON_CBN_SELCHANGE(IDC_COMBOLINESTYLE, OnSelchangeCombolinestyle)
	ON_COMMAND(IDM_LINE, OnLine)
	ON_COMMAND(IDM_RECTANGLE, OnRectangle)
	ON_COMMAND(IDM_CIRCLE, OnCircle)
	ON_BN_CLICKED(IDC_THICKNESS, OnThickness)
	ON_BN_CLICKED(IDC_RADIO2, OnThickness)
	ON_BN_CLICKED(IDC_RADIO3, OnThickness)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CControlDrawDlg message handlers

BOOL CControlDrawDlg::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
	
	//初始化各类成员变量
	m_listStyle.AddString("水平线");
	m_listStyle.AddString("竖直线");
	m_listStyle.AddString("向下斜线");
	m_listStyle.AddString("向上斜线");
	m_listStyle.AddString("十字线");
	m_listStyle.AddString("交叉线");

	m_comboMethod.AddString("实心图形");
	m_comboMethod.AddString("阴影图形");
	m_comboMethod.SetWindowText("实心图形");

	m_comboLineStyle.AddString("虚线");
	m_comboLineStyle.AddString("点划线");
	m_comboLineStyle.AddString("双点划线");
	m_comboLineStyle.AddString("点线");
	m_comboLineStyle.AddString("边框线");
	m_comboLineStyle.AddString("无");
	m_comboLineStyle.AddString("实线");
	m_comboLineStyle.SetWindowText("虚线");

	m_RColor=m_GColor=m_BColor=0;
	m_LineStyle=0;
	m_Method=0;
	m_Style=0;
	m_Shape=0;

	m_sliderR.SetRange(0,255);
	m_sliderR.SetPos(m_RColor);

	m_sliderG.SetRange(0,255);
	m_sliderG.SetPos(m_GColor);
	
	m_sliderB.SetRange(0,255);
	m_sliderB.SetPos(m_BColor);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CControlDrawDlg::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 CControlDrawDlg::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
	{
		CDialog::OnPaint();
	}
	//重新绘制图形
	CWnd* pWnd=GetDlgItem(IDC_DRAW);
	pWnd->UpdateWindow();
	Draw();
}

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

//调整颜色
void CControlDrawDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	if(pScrollBar->GetDlgCtrlID()==IDC_SLIDERR)			//调整红色
	{
		m_RColor=m_sliderR.GetPos();
		Draw();
	}
	else if(pScrollBar->GetDlgCtrlID()==IDC_SLIDERG)	//调整绿色
	{
		m_GColor=m_sliderG.GetPos();
		Draw();
	}
	else if(pScrollBar->GetDlgCtrlID()==IDC_SLIDERB)	//调整蓝色
	{
		m_BColor=m_sliderB.GetPos();
		Draw();
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CControlDrawDlg::Draw()
{
	CWnd* pWnd=GetDlgItem(IDC_DRAW);
	CRect rect;
	pWnd->GetClientRect(&rect);

	CDC *pDC=pWnd->GetDC();	
	
	CPen drawPen;						//创建画笔
	drawPen.CreatePen(m_LineStyle,m_thickness+1,RGB(m_GColor,m_RColor,m_BColor));
	CPen *pOldPen=pDC->SelectObject(&drawPen);

	CBrush drawBrush;					//创建画刷
	if(m_Method==0)
	{
		drawBrush.CreateSolidBrush(RGB(m_RColor,m_GColor,m_BColor));
	}
	else
	{
		drawBrush.CreateHatchBrush(m_Style,RGB(m_RColor,m_GColor,m_BColor));
	}
	CBrush *pOldBrush=pDC->SelectObject(&drawBrush);
	
	//确定绘制的图形
	switch(m_Shape)
	{
	case 0:
		pDC->MoveTo(0,0);
		pDC->LineTo(rect.right,rect.bottom);
		break;
	case 1:
		pDC->Rectangle(rect);
		break;		
	case 2:
		pDC->Ellipse(rect);
		break;
	}
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
}

//改变填充样式
void CControlDrawDlg::OnSelchangeListstyle() 
{
	// TODO: Add your control notification handler code here
	m_Style=m_listStyle.GetCurSel();
	Draw();
}

//改变填充方法
void CControlDrawDlg::OnSelchangeCombomethod() 
{
	// TODO: Add your control notification handler code here
	m_Method=m_comboMethod.GetCurSel();
	Draw();
}

//改变线形
void CControlDrawDlg::OnSelchangeCombolinestyle() 
{
	// TODO: Add your control notification handler code here
	m_LineStyle=m_comboLineStyle.GetCurSel();
	Draw();
}

//绘制直线
void CControlDrawDlg::OnLine() 
{
	// TODO: Add your command handler code here
	m_Shape=0;
	Draw();
}

//绘制矩形
void CControlDrawDlg::OnRectangle() 
{
	// TODO: Add your command handler code here
	m_Shape=1;
	Draw();
}

//绘制圆
void CControlDrawDlg::OnCircle() 
{
	// TODO: Add your command handler code here
	m_Shape=2;
	Draw();
}

void CControlDrawDlg::OnThickness() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	Draw();
}

⌨️ 快捷键说明

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