MFC 画任意圆

7 篇文章 0 订阅

代码是在网上搜的,
添加的控件ID

#define IDM_ABOUTBOX              0x0010
#define IDD_ABOUTBOX              100
#define IDS_ABOUTBOX              101
#define IDD_DRAWELLIPSE_DIALOG    102
#define IDR_MAINFRAME             128
#define IDC_EDIT_HALFLONGAXIS     1000
#define IDC_EDIT_HALFSHORTAXIS    1001
#define IDC_EDIT_LONGAXISANGLE    1002
#define IDC_STATIC_DISP           1003
#define IDC_BTN_ZHIDAO            1005

头文件

// DrawEllipseDlg.h : header file
//

#if !defined(AFX_DRAWELLIPSEDLG_H__5A12C89A_F700_4666_ABA9_D639DED308FD__INCLUDED_)
#define AFX_DRAWELLIPSEDLG_H__5A12C89A_F700_4666_ABA9_D639DED308FD__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CDrawEllipseDlg dialog

class CDrawEllipseDlg : public CDialog
{
// Construction
public:
    CDrawEllipseDlg(CWnd* pParent = NULL);  // standard constructor

// Dialog Data
    //{{AFX_DATA(CDrawEllipseDlg)
    enum { IDD = IDD_DRAWELLIPSE_DIALOG };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CDrawEllipseDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    void DrawEllipse();
    // 角度转弧度
    double Deg2Rad(const double &dwDegree) const;
    HICON m_hIcon;
    double  m_dwHalfLongAxis;
    double  m_dwHalfShortAxis;
    double  m_dwLongAxisAngle;
    // Generated message map functions
    //{{AFX_MSG(CDrawEllipseDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnAppAbout();
    afx_msg void OnChangeEditHalflongaxis();
    afx_msg void OnChangeEditHalfshortaxis();
    afx_msg void OnChangeEditLongaxisangle();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DRAWELLIPSEDLG_H__5A12C89A_F700_4666_ABA9_D639DED308FD__INCLUDED_)

实现

// DrawEllipseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DrawEllipseDlg.h"

#include <cmath>
const double PI = 3.1415926535898;

#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 void OnBtnZhidao();
    //}}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)
    ON_BN_CLICKED(IDC_BTN_ZHIDAO, OnBtnZhidao)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CDrawEllipseDlg dialog

CDrawEllipseDlg::CDrawEllipseDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDrawEllipseDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDrawEllipseDlg)
    m_dwHalfLongAxis = 150.0;
    m_dwHalfShortAxis = 150.0;
    m_dwLongAxisAngle = 0.0;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDrawEllipseDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDrawEllipseDlg)
    DDX_Text(pDX, IDC_EDIT_HALFLONGAXIS, m_dwHalfLongAxis);
    DDV_MinMaxDouble(pDX, m_dwHalfLongAxis, 1., 150.);
    DDX_Text(pDX, IDC_EDIT_HALFSHORTAXIS, m_dwHalfShortAxis);
    DDV_MinMaxDouble(pDX, m_dwHalfShortAxis, 1., 150.);
    DDX_Text(pDX, IDC_EDIT_LONGAXISANGLE, m_dwLongAxisAngle);
    DDV_MinMaxDouble(pDX, m_dwLongAxisAngle, -360., 360.);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDrawEllipseDlg, CDialog)
    //{{AFX_MSG_MAP(CDrawEllipseDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(ID_APP_ABOUT, OnAppAbout)
    ON_EN_CHANGE(IDC_EDIT_HALFLONGAXIS, OnChangeEditHalflongaxis)
    ON_EN_CHANGE(IDC_EDIT_HALFSHORTAXIS, OnChangeEditHalfshortaxis)
    ON_EN_CHANGE(IDC_EDIT_LONGAXISANGLE, OnChangeEditLongaxisangle)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CDrawEllipseDlg message handlers

BOOL CDrawEllipseDlg::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 CDrawEllipseDlg::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 CDrawEllipseDlg::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();
        if (UpdateData(TRUE))
        {
            DrawEllipse();
        }
    }
}

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

void CDrawEllipseDlg::OnAppAbout() 
{
    // TODO: Add your control notification handler code here
    CAboutDlg dlgAbout;
    dlgAbout.DoModal(); 
}


void CDrawEllipseDlg::OnChangeEditHalflongaxis() 
{
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO: Add your control notification handler code here
    if (UpdateData(TRUE))
    {
        DrawEllipse();
    }
}

void CDrawEllipseDlg::OnChangeEditHalfshortaxis() 
{
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO: Add your control notification handler code here
    if (UpdateData(TRUE))
    {
        DrawEllipse();
    }
}

void CDrawEllipseDlg::OnChangeEditLongaxisangle() 
{
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO: Add your control notification handler code here
    if (UpdateData(TRUE))
    {
        DrawEllipse();
    }
}

double CDrawEllipseDlg::Deg2Rad(const double &dwDegree) const
{
    return (dwDegree / 180.0 * PI);
}

void CDrawEllipseDlg::DrawEllipse()
{
    CWnd *pWnd = GetDlgItem(IDC_STATIC_DISP);
    ASSERT(pWnd != NULL);
    if (NULL == pWnd)
    {
        return;
    }
    CClientDC dc(pWnd);
    CRect rtClient;
    pWnd->GetClientRect(&rtClient);
    dc.FillSolidRect(&rtClient, RGB(0xa6, 0xca, 0xf0));

    int iCenterX = rtClient.Width() >> 1;
    int iCenterY = rtClient.Height() >> 1;

    double dwRad = Deg2Rad(360.0 - m_dwLongAxisAngle);
    ::XFORM xForm;
    xForm.eM11 = static_cast<FLOAT>(cos(dwRad));
    xForm.eM12 = static_cast<FLOAT>(sin(dwRad));
    xForm.eM21 = static_cast<FLOAT>(-sin(dwRad));
    xForm.eM22 = static_cast<FLOAT>(cos(dwRad));
    xForm.eDx  = static_cast<FLOAT>(iCenterX);
    xForm.eDy  = static_cast<FLOAT>(iCenterY);
    ::SetGraphicsMode(dc.GetSafeHdc(), GM_ADVANCED);
    ::SetWorldTransform(dc.GetSafeHdc(), &xForm);

    int m_iHalfLongAxis = static_cast<int>(m_dwHalfLongAxis);
    int m_iHalfShortAxis = static_cast<int>(m_dwHalfShortAxis);
    dc.SelectObject(::GetStockObject(NULL_BRUSH));
    dc.Ellipse(-m_iHalfLongAxis, -m_iHalfShortAxis, 
        m_iHalfLongAxis, m_iHalfShortAxis);
    dc.SelectObject(::GetStockObject(WHITE_BRUSH));
}

void CAboutDlg::OnBtnZhidao() 
{
    // TODO: Add your control notification handler code here
    CString strURL;
    GetDlgItemText(IDC_BTN_ZHIDAO, strURL);
    ::ShellExecute(this->GetSafeHwnd(), _T("open"), LPCTSTR(strURL), 
        NULL, NULL, SW_SHOWNORMAL);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值