>_<:这是个简单的MFC程序,有利于了解MFC的框架结构
>_<:Ruler.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();
};
>_<:Ruler.cpp
#include<afxwin.h>
#include"Ruler.h"
CMyApp myApp;
//////////////////////////////////////////////
//CMyApp member function
BOOL CMyApp::InitInstance()
{
m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////
//CMainWindow message map and member function
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP() CMainWindow::CMainWindow()
{
Create(NULL,_T("Ruler"),WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL);
} void CMainWindow::OnPaint()
{
CPaintDC dc(this); //屏幕初始化
dc.SetMapMode(MM_LOENGLISH);//0.01in ;1英寸映射
dc.SetTextAlign(TA_CENTER|TA_BOTTOM);
dc.SetBkMode(TRANSPARENT); //画尺子主题
CBrush brush(RGB(,,));
CBrush* pOldBrush=dc.SelectObject(&brush);
dc.Rectangle(,-,,-);
dc.SelectObject(pOldBrush); //画刻度
for(int i=;i<;i+=){
dc.MoveTo(i,-);
dc.LineTo(i,-);
}
for(int i=;i<;i+=){
dc.MoveTo(i,-);
dc.LineTo(i,-);
}
for(int i=;i<;i+=){
dc.MoveTo(i,-);
dc.LineTo(i,-); CString string;
string.Format(_T("%d"),(i/)-);
dc.TextOutA(i,-,string);
} //商标 //CRect rect(125,-125,175,-150);
//CString string=_T("晨光");
//dc.DrawText(string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
dc.SetTextColor(RGB(,,));
dc.TextOutA(,-,"晨光");
}