请问为什么只能绘制出直线,而折线和弓弧绘制不出来??
代码如下
#include "stdafx.h"
#include "LineTest.h"
#include "LineTestDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifclass CAboutDlg : public CDialog
{
public:
CPoint m_point[5];
CPen m_pen[5];
CAboutDlg()
{
m_pen[0].CreatePen(PS_SOLID,1,RGB(255,0,0));
m_pen[1].CreatePen(PS_SOLID,6,RGB(0,255,0));
m_pen[2].CreatePen(PS_DASH,1,RGB(255,0,0));
m_pen[3].CreatePen(PS_DOT,1,RGB(0,0,255));
m_pen[4].CreatePen(PS_DASHDOTDOT,1,RGB(255,0,0));
m_point[0].x =10;
m_point[0].y =100;
m_point[1].x =10;
m_point[1].y =120;
m_point[2].x =100;
m_point[2].y =105;
m_point[3].x =170;
m_point[3].y =120;
m_point[4].x =170;
m_point[4].y =100;
}
protected:
DECLARE_MESSAGE_MAP()
};
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()/////////////////////////////////////////////////////////////////////////////
// CLineTestDlg dialogCLineTestDlg::CLineTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLineTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLineTestDlg)
// 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 CLineTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLineTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CLineTestDlg, CDialog)
//{{AFX_MSG_MAP(CLineTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CLineTestDlg message handlersBOOL CLineTestDlg::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 CLineTestDlg::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 CLineTestDlg::OnPaint()
{
CPaintDC dc(this);
CBrush brush(RGB(190,190,190));
dc.SelectObject(&brush);
DrawLine(&dc);
DrawPolyline(&dc);
DrawChord(&dc);
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLineTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//添加绘制直线的函数DrawLine()
void CLineTestDlg::DrawLine(CDC *pDc)
{
CPen *oldPen = pDc->SelectObject(&m_pen[0]);
for(int i = 0;i<5;i++)
{
pDc->SelectObject(&m_pen[i]);
pDc->MoveTo(10,20+i*15);
pDc->LineTo(170,20+i*15);
}
pDc->SelectObject(oldPen);
}
//添加绘制折线的函数DrawPolyline()
void CLineTestDlg::DrawPolyline(CDC *pDc)
{
CPen *oldPen = pDc->SelectObject(&m_pen[0]);
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
m_point[j].y+=35;
}
pDc->SelectObject(&m_pen[i]);
pDc->Polyline(m_point,5);
}
pDc->SelectObject(oldPen);
}
//添加绘制弓弧的函数DrawChord()
void CLineTestDlg::DrawChord(CDC *pDc)
{
CRect rect(150,420,220,480);
CPoint ptStart(170,460);
CPoint ptEnd(210,420);
CPen *oldPen=pDc->SelectObject(&m_pen[0]);
for(int i=0;i<5;i++)
{
rect.OffsetRect(80,0);
ptStart+=CPoint(80,0);
ptEnd+=CPoint(80,0);
pDc->SelectObject(&m_pen[i]);
pDc->Chord(&rect,ptStart,ptEnd);
}
pDc->SelectObject(oldPen);
}
代码如下
#include "stdafx.h"
#include "LineTest.h"
#include "LineTestDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifclass CAboutDlg : public CDialog
{
public:
CPoint m_point[5];
CPen m_pen[5];
CAboutDlg()
{
m_pen[0].CreatePen(PS_SOLID,1,RGB(255,0,0));
m_pen[1].CreatePen(PS_SOLID,6,RGB(0,255,0));
m_pen[2].CreatePen(PS_DASH,1,RGB(255,0,0));
m_pen[3].CreatePen(PS_DOT,1,RGB(0,0,255));
m_pen[4].CreatePen(PS_DASHDOTDOT,1,RGB(255,0,0));
m_point[0].x =10;
m_point[0].y =100;
m_point[1].x =10;
m_point[1].y =120;
m_point[2].x =100;
m_point[2].y =105;
m_point[3].x =170;
m_point[3].y =120;
m_point[4].x =170;
m_point[4].y =100;
}
protected:
DECLARE_MESSAGE_MAP()
};
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()/////////////////////////////////////////////////////////////////////////////
// CLineTestDlg dialogCLineTestDlg::CLineTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLineTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLineTestDlg)
// 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 CLineTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLineTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CLineTestDlg, CDialog)
//{{AFX_MSG_MAP(CLineTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CLineTestDlg message handlersBOOL CLineTestDlg::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 CLineTestDlg::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 CLineTestDlg::OnPaint()
{
CPaintDC dc(this);
CBrush brush(RGB(190,190,190));
dc.SelectObject(&brush);
DrawLine(&dc);
DrawPolyline(&dc);
DrawChord(&dc);
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLineTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//添加绘制直线的函数DrawLine()
void CLineTestDlg::DrawLine(CDC *pDc)
{
CPen *oldPen = pDc->SelectObject(&m_pen[0]);
for(int i = 0;i<5;i++)
{
pDc->SelectObject(&m_pen[i]);
pDc->MoveTo(10,20+i*15);
pDc->LineTo(170,20+i*15);
}
pDc->SelectObject(oldPen);
}
//添加绘制折线的函数DrawPolyline()
void CLineTestDlg::DrawPolyline(CDC *pDc)
{
CPen *oldPen = pDc->SelectObject(&m_pen[0]);
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
m_point[j].y+=35;
}
pDc->SelectObject(&m_pen[i]);
pDc->Polyline(m_point,5);
}
pDc->SelectObject(oldPen);
}
//添加绘制弓弧的函数DrawChord()
void CLineTestDlg::DrawChord(CDC *pDc)
{
CRect rect(150,420,220,480);
CPoint ptStart(170,460);
CPoint ptEnd(210,420);
CPen *oldPen=pDc->SelectObject(&m_pen[0]);
for(int i=0;i<5;i++)
{
rect.OffsetRect(80,0);
ptStart+=CPoint(80,0);
ptEnd+=CPoint(80,0);
pDc->SelectObject(&m_pen[i]);
pDc->Chord(&rect,ptStart,ptEnd);
}
pDc->SelectObject(oldPen);
}
{
CPoint m_point[5];
CPen m_pen[5];
……
};
CLineTestDlg::CLineTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLineTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLineTestDlg)
// 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); //初始化
{
m_pen[0].CreatePen(PS_SOLID,1,RGB(255,0,0));
m_pen[1].CreatePen(PS_SOLID,6,RGB(0,255,0));
m_pen[2].CreatePen(PS_DASH,1,RGB(255,0,0));
m_pen[3].CreatePen(PS_DOT,1,RGB(0,0,255));
m_pen[4].CreatePen(PS_DASHDOTDOT,1,RGB(255,0,0));
m_point[0].x =10;
m_point[0].y =100;
m_point[1].x =10;
m_point[1].y =120;
m_point[2].x =100;
m_point[2].y =105;
m_point[3].x =170;
m_point[3].y =120;
m_point[4].x =170;
m_point[4].y =100;
}
}
在OnInitDialog() 中加上 ShowWindow(SW_MAXIMIZE) 最大化窗口显示 试试看
程序使用了绝对坐标,可能是在窗口之外的区域里另外
CBrush *pOldBrush = dc.SelectObject(&brush);
……
dc.SelectObject(pOldBrush); //绘制玩了要恢复,否则容易导致句柄死锁而造成资源泄露