创建刻度标尺

1.程序界面

 

创建刻度标尺_第1张图片

2.实现步骤

 

1.创建一个RulerBox类继承CWnd

RulerBox.h文件

#if !defined(AFX_RULERBOX_H__603E9AFC_6C42_4D69_8EF5_6791BA4133F4__INCLUDED_) #define AFX_RULERBOX_H__603E9AFC_6C42_4D69_8EF5_6791BA4133F4__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // RulerBox.h : header file // ///////////////////////////////////////////////////////////////////////////// // RulerBox window class RulerBox : public CWnd { // Construction public: RulerBox(); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(RulerBox) public: virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL); //}}AFX_VIRTUAL // Implementation public: void Create(CWnd* pParentWnd,DWORD dwStyle,int type,int x,int y,int length); void SetPixPerM(); void DrawBackGround(CDC* pDC); void SetBackGroundColor(COLORREF color); virtual ~RulerBox(); // Generated message map functions protected: //{{AFX_MSG(RulerBox) afx_msg void OnPaint(); //}}AFX_MSG DECLARE_MESSAGE_MAP() public: void DrawHorizontalRuler(CDC* pDC); void DrawVerticalRuler(CDC* pDC); public: CRect rect;//标尺区域 int width;//标尺宽度,单位为像素 int height;//标尺高度,单位为像素 private: COLORREF backColor;//背景色 int rulerType;//标尺类型,0为水平标尺,1为垂直标尺 int nhPixPerM;//获得水平方向上每厘米多少个像素 int nvPixPerM;//获得垂直方向上每厘米多少个像素 int longRuler;//尺子长度,单位为厘米 int widthRuler;//尺子的宽度,单位为厘米 double longPreCM;//厘米刻度线的长度,单位为厘米 double longPreMM;//毫米刻度线的长度,单位为厘米 }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_RULERBOX_H__603E9AFC_6C42_4D69_8EF5_6791BA4133F4__INCLUDED_)

RulerBox.cpp文件

// RulerBox.cpp : implementation file // #include "stdafx.h" #include "Ruler.h" #include "RulerBox.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // RulerBox RulerBox::RulerBox() { backColor = RGB(233,235,234);//初始化背景色 SetPixPerM();//初始化每厘米像素值 widthRuler=2;//尺子宽度为2厘米 longPreCM=0.5;//厘米刻度线长度为0.5厘米 longPreMM=0.25;//毫米刻度线的长度为0.25厘米 } RulerBox::~RulerBox() { } BEGIN_MESSAGE_MAP(RulerBox, CWnd) //{{AFX_MSG_MAP(RulerBox) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // RulerBox message handlers void RulerBox::OnPaint() { CPaintDC dc(this); dc.SetBkMode(TRANSPARENT);//设置背景透明 //DrawBackGround(&dc);//绘制背景 if(rulerType==1)//垂直标尺 { DrawVerticalRuler(&dc); } else if(rulerType==0)//水平标尺 { DrawHorizontalRuler(&dc); } } //设置背景色 void RulerBox::SetBackGroundColor(COLORREF color) { backColor = color; } //画背景 void RulerBox::DrawBackGround(CDC *pDC) { CBrush br(backColor);//创建画刷 CRect rect; GetClientRect(&rect);//获得客户区域 pDC->FillRect(&rect,&br);//填充客户区域 } //设置每厘米的像素值 void RulerBox::SetPixPerM() { HDC hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);//创建一个屏幕设备 int nHpix=GetDeviceCaps(hdcScreen,HORZRES);//获取屏幕水平像素值 int nVpix=GetDeviceCaps(hdcScreen,VERTRES);//获取屏幕垂直像素值 int nhMM=GetDeviceCaps(hdcScreen,HORZSIZE);//获取屏幕宽度,单位为毫米 int nvMM=GetDeviceCaps(hdcScreen,VERTSIZE);//获取屏幕高度,单位为毫米 nhPixPerM=nHpix/nhMM*10;//获取水平方向每厘米的像素值 nvPixPerM=nVpix/nvMM*10;//获取垂直方向每厘米的像素值 } BOOL RulerBox::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) { // TODO: Add your specialized code here and/or call the base class return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext); } //该方法用来创建标尺,type为尺子类型,0为水平直尺,1为垂直直尺,x,y为直尺坐标,longRuler为尺子长度单位为厘米 void RulerBox::Create(CWnd *pParentWnd, DWORD dwStyle,int type,int x,int y,int length) { this->longRuler = length+1;//尺子长度 rulerType = type;//标尺类型 if(type==0)//水平标尺 { rect.left=x;//标尺横坐标 rect.top=y;//标尺纵坐标 rect.right=x+longRuler*nhPixPerM; rect.bottom=y+widthRuler*nvPixPerM; width = longRuler*nhPixPerM;//标尺宽度,单位为像素 height = widthRuler*nvPixPerM;//标尺高度,单位为像素 } else if(type==1)//垂直标尺 { rect.left=x;//标尺横坐标 rect.top=y;//标尺纵坐标 rect.right=x+widthRuler*nhPixPerM; rect.bottom=y+longRuler*nvPixPerM; width = widthRuler*nhPixPerM;//标尺宽度,单位为像素 height = longRuler*nvPixPerM;//标尺高度,单位为像素 } this->Create(NULL,NULL,dwStyle,rect,pParentWnd,NULL); } //画图垂直标尺 void RulerBox::DrawVerticalRuler(CDC *pDC) { //画标尺长度 pDC->MoveTo(nhPixPerM+0.99*nhPixPerM,0.5*nhPixPerM); pDC->LineTo(nhPixPerM+0.99*nhPixPerM,nvPixPerM*(longRuler-1)+0.5*nhPixPerM); //画厘米刻度线 for(int i=0;i<longRuler;i++) { pDC->MoveTo(nhPixPerM+0.99*nhPixPerM,i*nvPixPerM+0.5*nhPixPerM); pDC->LineTo(nhPixPerM-longPreCM*nhPixPerM+0.99*nhPixPerM,i*nvPixPerM+0.5*nhPixPerM); //画刻度标记 CString text; text.Format("%d",i); CRect rect(nhPixPerM-0.9*nhPixPerM+0.99*nhPixPerM,i*nvPixPerM+0.5*nhPixPerM-7,nhPixPerM-0.9*nhPixPerM+15+0.99*nhPixPerM,i*nvPixPerM+0.5*nhPixPerM+13); pDC->SetBkMode(TRANSPARENT);//设置背景透明 pDC->DrawText(text,&rect,DT_LEFT|DT_WORDBREAK); } //画毫米刻度线 for(int j=0;j<longRuler*10-9;j++) { pDC->MoveTo(nhPixPerM+0.99*nhPixPerM,j*nvPixPerM/10+0.5*nhPixPerM); pDC->LineTo(nhPixPerM-longPreMM*nhPixPerM+0.99*nhPixPerM,j*nvPixPerM/10+0.5*nhPixPerM); } } //画水平标尺 void RulerBox::DrawHorizontalRuler(CDC *pDC) { //画标尺长度 pDC->MoveTo(0.5*nvPixPerM,nvPixPerM+0.99*nvPixPerM); pDC->LineTo(nhPixPerM*(longRuler-1)+0.5*nvPixPerM,nvPixPerM+0.99*nvPixPerM); //画厘米刻度线 for(int i=0;i<longRuler;i++) { pDC->MoveTo(i*nhPixPerM+0.5*nvPixPerM,nvPixPerM+0.99*nvPixPerM); pDC->LineTo(i*nhPixPerM+0.5*nvPixPerM,nvPixPerM-longPreCM*nvPixPerM+0.99*nvPixPerM); //画刻度标记 CString text; text.Format("%d",i); CRect rect(i*nhPixPerM+0.42*nvPixPerM,nvPixPerM-longPreCM*nvPixPerM-15+0.99*nvPixPerM,i*nhPixPerM+0.42*nvPixPerM+15,nvPixPerM-longPreCM*nvPixPerM+0.99*nvPixPerM); pDC->SetBkMode(TRANSPARENT);//设置背景透明 pDC->DrawText(text,&rect,DT_LEFT|DT_WORDBREAK); } //画毫米刻度线 for(int j=0;j<longRuler*10-9;j++) { pDC->MoveTo(j*nhPixPerM/10+0.5*nvPixPerM,nvPixPerM+0.99*nvPixPerM); pDC->LineTo(j*nhPixPerM/10+0.5*nvPixPerM,nvPixPerM-longPreMM*nvPixPerM+0.99*nvPixPerM); } }   

2.使用RulerBox类

1.在RulerDlg中定义

private: RulerBox rulerBoxH; RulerBox rulerBoxV;

2.RulerDlgOnInitDialog()方法中创建标尺

rulerBoxV.Create(this,WS_CHILD|WS_VISIBLE,1,159,60,10); rulerBoxH.Create(this,WS_CHILD|WS_VISIBLE,0,220,0,10);

你可能感兴趣的:(function,Microsoft,header,File,null,insert)