MFC画图
#include "stdafx.h"
#include "huaquxian.h"
#include "huaquxianDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// ChuaquxianDlg 对话框
ChuaquxianDlg::ChuaquxianDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_HUAQUXIAN_DIALOG, pParent)
, m_coeff1(_T(""))
, m_coeff2(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void ChuaquxianDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_coeff1);
DDX_Text(pDX, IDC_EDIT2, m_coeff2);
}
BEGIN_MESSAGE_MAP(ChuaquxianDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &ChuaquxianDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &ChuaquxianDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// ChuaquxianDlg 消息处理程序
BOOL ChuaquxianDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void ChuaquxianDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void ChuaquxianDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
CDC *pDC = GetDC();
CPen pen1, pen2;
pen1.CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
pen2.CreatePen(PS_SOLID, 2, RGB(100, 160, 240));
pDC->SelectObject(&pen1);
CRect rc;
rc.SetRect(30, 80, 440, 380);
pDC->Rectangle(rc);
pen1.DeleteObject();
pDC->SelectObject(&pen2);
pDC->MoveTo(110, 230);
pDC->LineTo(380, 230);
pDC->LineTo(365, 220);
pDC->MoveTo(380, 230);
pDC->LineTo(365, 240);
for (int i = -5; i <= 5; i++)
{
pDC->MoveTo(25 * i + 235, 225);
pDC->LineTo(25 * i + 235, 235);
CString Str;
Str.Format(_T("%d"), i);
if ((i != 0)&(i != 1) &(i != -1))
{
pDC->TextOut(25 * i + 230, 235, Str);
}
}
pDC->MoveTo(235, 355);
pDC->LineTo(235, 85);
pDC->LineTo(225, 100);
pDC->MoveTo(235, 85);
pDC->LineTo(245, 100);
for (int j = -5; j <= 5; j++)
{
pDC->MoveTo(230, 230 + 25 * j);
pDC->LineTo(240, 230 + 25 * j);
CString Str;
Str.Format(_T("%d"), 0 - j);
if ((j != 0) & (j != 1) & (j != -1))
{
pDC->TextOut(210, 220 + 25 * j, Str);
}
}
pen2.DeleteObject();
ReleaseDC(pDC);
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR ChuaquxianDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void ChuaquxianDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
CString str1, str2;
str1 = m_coeff1;
str2 = m_coeff2;
double a, b;
a = atof(str1);
b = atof(str2);
CPoint point1, point2,point3;
point1.x = 160;
point1.y = 230 + 3 * a * 25 - 25 * b;
point2.x = 310;
point2.y = 230 - 3 * a * 25 - 25 * b;
point3.x = 235 + 25 * a;
point3.y = 230 - 25 * b;
CDC *pDC = GetDC();
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 200));
pDC->SelectObject(&pen);
pDC->MoveTo(point1.x, point1.y);
pDC->LineTo(point2.x, point2.y);
int offset = 5;
pDC->MoveTo(point3.x - offset, point3.y - offset);
pDC->LineTo(point3.x + offset, point3.y + offset);
pDC->MoveTo(point3.x - offset, point3.y - offset);
pDC->LineTo(point3.x + offset, point3.y + offset);
pDC->MoveTo(point3.x - offset, point3.y + offset);
pDC->LineTo(point3.x + offset, point3.y - offset);
pDC->MoveTo(point3.x - offset, point3.y);
pDC->LineTo(point3.x + offset, point3.y);
pDC->MoveTo(point3.x, point3.y- offset);
pDC->LineTo(point3.x, point3.y+ offset);
//pDC->LineTo(point3.x, point3.y);
bool flag = pDC->SetPixelV(point3, RGB(255, 255, 0));
pen.DeleteObject();
ReleaseDC(pDC);
UpdateData(FALSE);
}
void ChuaquxianDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
// 清空绘制区域
CRect rectDlg;
GetClientRect(rectDlg); // 获得窗体的大小
int pointWidth = rectDlg.Width(); // 获取窗体宽度
int pointHeight = rectDlg.Height(); // 获取窗体高度
RedrawWindow(CRect(0, 0, pointWidth, pointHeight)); // 重绘指定区域
UpdateData(TRUE);
CString st1 = NULL, st2 = NULL;
m_coeff1 = st1;
m_coeff2 = st2;
UpdateData(FALSE);
}