首页 > 代码库 > [游戏学习23] MFC 画尺子

[游戏学习23] MFC 画尺子

 

>_<:这是个简单的MFC程序,有利于了解MFC的框架结构

>_<:Ruler.h

 1 #include<afxwin.h>
 2 class CMyApp:public CWinApp
 3 {
 4 public:
 5     virtual BOOL InitInstance();
 6 };
 7 class CMainWindow:public CFrameWnd
 8 {
 9 public:
10     CMainWindow();
11 protected:
12     afx_msg void OnPaint();
13     DECLARE_MESSAGE_MAP();
14 };

>_<:Ruler.cpp

 1 #include<afxwin.h>
 2 #include"Ruler.h"
 3 CMyApp myApp;
 4 //////////////////////////////////////////////
 5 //CMyApp member function
 6 BOOL CMyApp::InitInstance()
 7 {
 8     m_pMainWnd=new CMainWindow;
 9     m_pMainWnd->ShowWindow(m_nCmdShow);
10     m_pMainWnd->UpdateWindow();
11     return TRUE;
12 }
13 /////////////////////////////////////////////
14 //CMainWindow message map and member function
15 BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
16     ON_WM_PAINT()
17 END_MESSAGE_MAP()
18 
19 CMainWindow::CMainWindow()
20 {
21     Create(NULL,_T("Ruler"),WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL);
22 }
23 
24 void CMainWindow::OnPaint()
25 {
26     CPaintDC dc(this);
27 
28     //屏幕初始化
29     dc.SetMapMode(MM_LOENGLISH);//0.01in  ;1英寸映射
30     dc.SetTextAlign(TA_CENTER|TA_BOTTOM);
31     dc.SetBkMode(TRANSPARENT);
32 
33     //画尺子主题
34     CBrush brush(RGB(255,255,0));
35     CBrush* pOldBrush=dc.SelectObject(&brush);
36     dc.Rectangle(100,-100,1300,-200);
37     dc.SelectObject(pOldBrush);
38 
39     //画刻度
40     for(int i=125;i<1300;i+=25){
41         dc.MoveTo(i,-192);
42         dc.LineTo(i,-200);
43     }
44     for(int i=150;i<1300;i+=50){
45         dc.MoveTo(i,-184);
46         dc.LineTo(i,-200);
47     }
48     for(int i=200;i<1300;i+=100){
49         dc.MoveTo(i,-175);
50         dc.LineTo(i,-200);
51 
52         CString string;
53         string.Format(_T("%d"),(i/100)-1);
54         dc.TextOutA(i,-175,string);
55     }
56 
57     //商标
58     
59     //CRect rect(125,-125,175,-150);
60     //CString string=_T("晨光");
61     //dc.DrawText(string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
62     dc.SetTextColor(RGB(192,192,192));
63     dc.TextOutA(125,-125,"晨光");
64 }