您的位置:首页 > 其它

20160315 第2章 在窗口中绘图(来自《MFC Windows 程序设计(第2版)》)

2016-03-15 15:52 405 查看


2.1.1_Polyline

MyPolyline.h

#include <afxwin.h>

class CMyApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};

class CMainWindow :public CFrameWnd
{
public:
CMainWindow();

protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};

MyPolyline.cpp

#include "MyPolyline.h"
#include <math.h>
#define SEGMENTS 500
#define PI 3.1415926

CMyApp theApp;

BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
Create(NULL, _T("Polyline"));
}

void CMainWindow::OnPaint()
{
CRect rect;
GetClientRect(&rect);
int nWidth = rect.Width();
int nHeight = rect.Height();

CPaintDC dc(this);
CPoint aPoint[SEGMENTS];
for (int i = 0; i < SEGMENTS; i++)
{
aPoint[i].x = (i*nWidth) / SEGMENTS;
aPoint[i].y = (int)((nHeight / 2)*(1-(sin((2 * PI*i) / SEGMENTS))));
}
dc.Polyline(aPoint, SEGMENTS);

//NIKE swoosh商标
POINT aPoint1[4] = { 120, 100, 120, 200, 250, 150, 500, 40 };
POINT aPoint2[4] = { 120, 100, 50, 350, 250, 200, 500, 40 };
dc.PolyBezier(aPoint1, 4);
dc.PolyBezier(aPoint2, 4);
}

2.2.2_Pie_Chord

Pie_Chord.h

#include <afxwin.h>

class CMyApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};

class CMainWindow :public CFrameWnd
{
public:
CMainWindow();

protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};


Pie_Chord.cpp

#include "Pie_Chord.h"
#include <math.h>
#define PI 3.1415926

CMyApp theApp;

BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
Create(NULL, _T("Polyline"));
}

void CMainWindow::OnPaint()
{
CPaintDC dc(this);
int nRevenues[4] = { 125, 376, 252, 184 };
CRect rect;
GetClientRect(&rect);
dc.SetViewportOrg(rect.Width() / 2, rect.Height() / 2);

int nTotal = 0;
for (int i = 0; i < 4; i++)
{
nTotal += nRevenues[i];
}

int x1 = 0;
int y1 = -1000;
int nSum = 0;

for (int i = 0; i < 4; i++)
{
nSum += nRevenues[i];
double rad = ((double)(nSum * 2 * PI) / (double)nTotal) + PI;
int x2 = (int)(sin(rad) * 1000);
int y2 = (int)(cos(rad) * 1000 * 3) / 4;
dc.Pie(-200, -150, 200, 150, x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}


2.2.12 标尺应用程序

Ruler.h

#pragma once
#include &l
d77c
t;afxwin.h>
#include <afxext.h>

class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

class CMainWindow : public CFrameWnd
{
public:
CMainWindow();

protected:
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
DECLARE_MESSAGE_MAP()
};


Ruler.cpp

#include "Ruler.h"

CMyApp theApp;

BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
Create(NULL, _T("Ruler"),WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL);
}

void CMainWindow::OnPaint()
{
CPaintDC dc(this);
//Initialize the device context.
dc.SetMapMode(MM_LOENGLISH);
dc.SetTextAlign(TA_CENTER | TA_BOTTOM);
dc.SetBkMode(TRANSPARENT);
//Draw the body of the ruler.
CBrush brush(RGB(255, 255, 0));
CBrush *pOldBrush = dc.SelectObject(&brush);
dc.Rectangle(100, -100, 1300, -200);
dc.SelectObject(pOldBrush);
//Draw the tick marks and labels.
for (int i = 125; i < 1300; i += 25)
{
dc.MoveTo(i, -192);
dc.LineTo(i, -200);
}
for (int i = 150; i < 1300; i += 50)
{
dc.MoveTo(i, -184);
dc.LineTo(i, -200);
}
for (int i = 200; i < 1300; i += 100)
{
dc.MoveTo(i, -175);
dc.LineTo(i, -200);

CString string;
string.Format(_T("%d"), (i / 100 - 1));
dc.TextOut(i, -175, string);
}
}

void CMainWindow::OnSize(UINT nType, int cx, int cy)
{

}

void CMainWindow::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{

}

void CMainWindow::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{

}


2.3.6 Accel应用程序

Accel.h

#pragma once
#include <afxwin.h>
#include <afxext.h>
#define LINESIZE 8

class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

class CMainWindow :public CFrameWnd
{
protected:
int m_nCellWidth; //Cell width in pixels
int m_nCellHeight; //Cell height in pixels
int m_nRibbonWidth; //Ribbon width in pixels
int m_nViewWidth; //Workspace width in pixels
int m_nViewHeight; //Workspace height in pixels
int m_nHScrollPos; //Horizontal scroll position
int m_nVScrollPos; //Vertical scroll position
int m_nHPageSize; //Horizontal page size
int m_nVPageSize; //Vertical page size

public:
CMainWindow();

protected:
afx_msg void OnPaint();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnHScroll(UINT nCode, UINT nPos,
CScrollBar *pScrollBar);
afx_msg void OnVScroll(UINT nCode, UINT nPos,
CScrollBar *pScrollBar);
DECLARE_MESSAGE_MAP()
};
Accel.cpp

#include "Accel.h"

CMyApp theApp;

BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
Create(NULL, _T("Accel"),
WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL);
}

void CMainWindow::OnPaint()
{
CPaintDC dc(this);
//Set the window origin to reflect the current scroll positions.
dc.SetWindowOrg(m_nHScrollPos, m_nVScrollPos);
//Draw the grid lines.
CPen pen(PS_SOLID, 0, RGB(192, 192, 192));
CPen *pOldPen = dc.SelectObject(&pen);
for (int i = 0; i < 99; i++)
{
int y = (i*m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nViewWidth, y);
}
for (int j = 0; j < 26; j++)
{
int x = (j*m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nViewHeight);
}
dc.SelectObject(pOldPen);
//Draw the bodies of the rows and the column headers.
CBrush brush;
brush.CreateStockObject(LTGRAY_BRUSH);
CRect rcTop(0, 0, m_nViewWidth, m_nCellHeight);
dc.FillRect(rcTop, &brush);
CRect rcLeft(0, 0, m_nRibbonWidth, m_nViewHeight);
dc.FillRect(rcLeft, &brush);
dc.MoveTo(0, m_nCellHeight);
dc.LineTo(m_nViewWidth, m_nCellHeight);
dc.MoveTo(m_nRibbonWidth, 0);
dc.LineTo(m_nRibbonWidth, m_nViewHeight);
dc.SetBkMode(TRANSPARENT);
//Add numbers and button outlines to the row headers.
for (int i = 0; i < 99; i++)
{
int y = (i * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nRibbonWidth, y);
CString string;
string.Format(_T("%d"), i + 1);
CRect rect(0, y, m_nRibbonWidth, y + m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
rect.top++;
dc.Draw3dRect(&rect, RGB(255, 255, 255), RGB(128, 128, 128));
}
//Add letters and button outlines to the column headers.
for (int j = 0; j < 26; j++)
{
int x = (j*m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nCellHeight);
CString string;
string.Format(_T("%c"), j + 'A');
CRect rect(x, 0, x + m_nCellWidth, m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
rect.left++;
dc.Draw3dRect(&rect, RGB(255, 255, 255), RGB(128, 128, 128));
}
}
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
{
return -1;
}
CClientDC dc(this);
m_nCellWidth = dc.GetDeviceCaps(LOGPIXELSX);
m_nCellHeight = dc.GetDeviceCaps(LOGPIXELSY) / 4;
m_nRibbonWidth = m_nCellWidth / 2;
m_nViewWidth = (26 * m_nCellWidth) + m_nRibbonWidth;
m_nViewHeight = 100 * m_nCellHeight;
return 0;
}
void CMainWindow::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
//Set the horizontal scrolling parameters.
int nHScrollMax = 0;
m_nHScrollPos = m_nHPageSize = 0;
if (cx < m_nViewWidth)
{
nHScrollMax = m_nViewWidth - 1;
m_nHPageSize = cx;
m_nHScrollPos = min(m_nHScrollPos, m_nViewWidth - m_nHPageSize - 1);
}
SCROLLINFO si;
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = nHScrollMax;
si.nPos = m_nHScrollPos;
si.nPage = m_nHPageSize;
SetScrollInfo(SB_HORZ, &si, TRUE);
//Set the vertical scrolling parameters.
int nVScrollMax = 0;
m_nVScrollPos = m_nVPageSize = 0;
if (cy < m_nViewHeight)
{
nVScrollMax = m_nViewHeight - 1;
m_nVPageSize = cy;
m_nVScrollPos = min(m_nVScrollPos, m_nViewHeight - m_nVPageSize - 1);
}
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = nVScrollMax;
si.nPos = m_nVScrollPos;
si.nPage = m_nVPageSize;
SetScrollInfo(SB_VERT, &si, TRUE);
}
void CMainWindow::OnHScroll(UINT nCode, UINT nPos,
CScrollBar *pScrollBar)
{
int nDelta = 0;
switch (nCode)
{
case SB_LINELEFT:
nDelta = -LINESIZE;
break;
case SB_PAGELEFT:
nDelta = -m_nHPageSize;
break;
case SB_THUMBTRACK:
nDelta = (int)nPos - m_nHScrollPos;
break;
case SB_PAGERIGHT:
nDelta = m_nHPageSize;
break;
case SB_LINERIGHT:
nDelta = LINESIZE;
break;
default:
break;
}
int nScrollPos = m_nHScrollPos + nDelta;
int nMaxPos = m_nViewWidth - m_nHPageSize;
if (nScrollPos < 0)
{
nDelta = -m_nHScrollPos;
}
else if (nScrollPos > nMaxPos)
{
nDelta = nMaxPos - m_nHScrollPos;
}
if (nDelta != 0)
{
m_nHScrollPos += nDelta;
SetScrollPos(SB_HORZ, m_nHScrollPos, TRUE);
ScrollWindow(-nDelta, 0);
}
}
void CMainWindow::OnVScroll(UINT nCode, UINT nPos,
CScrollBar *pScrollBar)
{
int nDelta = 0;
switch (nCode)
{
case SB_LINEUP:
nDelta = -LINESIZE;
break;
case SB_PAGEUP:
nDelta = -m_nVPageSize;
break;
case SB_THUMBTRACK:
nDelta = (int)nPos - m_nVScrollPos;
break;
case SB_PAGEDOWN:
nDelta = m_nVPageSize;
break;
case SB_LINEDOWN:
nDelta = LINESIZE;
break;
default:
break;
}
int nScrollPos = m_nVScrollPos + nDelta;
int nMaxPos = m_nViewHeight - m_nVPageSize;
if (nScrollPos < 0)
{
nDelta = -m_nVScrollPos;
}
else if (nScrollPos > nMaxPos)
{
nDelta = nMaxPos - m_nVScrollPos;
}
if (nDelta != 0)
{
m_nVScrollPos += nDelta;
SetScrollPos(SB_VERT, m_nVScrollPos, TRUE);
ScrollWindow(0, -nDelta);
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: