首页 > 代码库 > [游戏模版1] MFC最小框架(base function including)

[游戏模版1] MFC最小框架(base function including)

 

>_<:Here are the template of mini-MFC include:

  • CPen,CBrush,Front,Paint Line and some other graph.
  • OnPaint message,OnLeftButtonDown message,you can through it know more

Hello.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     afx_msg void OnLButtonDown(UINT m_nFlags,CPoint point);
14     DECLARE_MESSAGE_MAP();
15 };

Hello.cpp

  1 #include<afxwin.h>
  2 #include<math.h>
  3 #include"Hello.h"
  4 #define SEGMENTS 500
  5 #define PI 3.1415926
  6 
  7 int PenStyle[7]={PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};//实线\宽\窄\宽+窄\宽+2窄\没有\实线不外伸
  8 int PenNum=0;
  9 int BrushStye[6]={HS_BDIAGONAL,HS_FDIAGONAL,HS_CROSS,HS_HORIZONTAL,HS_DIAGCROSS,HS_VERTICAL};//45\135\90交叉\0\45交叉\90
 10 int BrushNum=0;
 11 
 12 CMyApp myApp;
 13 //////////////////////////////////////////////
 14 //CMyApp member function
 15 BOOL CMyApp::InitInstance()                   //初始化函数
 16 {
 17     m_pMainWnd=new CMainWindow;
 18     m_pMainWnd->ShowWindow(m_nCmdShow);
 19     m_pMainWnd->UpdateWindow();
 20     return TRUE;
 21 }
 22 /////////////////////////////////////////////
 23 //CMainWindow message map and member function
 24 BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)           //消息映射
 25     ON_WM_PAINT()
 26     ON_WM_LBUTTONDOWN()
 27 END_MESSAGE_MAP()
 28 
 29 CMainWindow::CMainWindow()      //创建窗口
 30 {
 31     Create(NULL,_T("The Hello"),WS_OVERLAPPED,CRect(20,40,640,440));//CFrameWnd下的成员函数8个参数,6个默认
 32 }                               //这个参数产生垂直滚动条
 33 
 34 void CMainWindow::OnPaint()
 35 {
 36 
 37     CRect rect;
 38     GetClientRect(&rect);
 39 
 40     /*72点,Arial,并带有下拉阴影生成的“Hello MFC" 
 41     CFont font;
 42     font.CreatePointFont(720,_T("Arial"));//创建72点,Arial字体
 43 
 44     CPaintDC dc(this);
 45     dc.SelectObject(&font);
 46     dc.SetBkMode(TRANSPARENT);//背景模式 设为透明
 47 
 48     CString string=_T("Hello MFC");
 49 
 50     rect.OffsetRect(16,16);//从窗口中心向右向下偏移几个像素点的位置
 51     dc.SetTextColor(RGB(192,192,192));
 52     dc.DrawText(string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
 53 
 54     rect.OffsetRect(-16,-16);
 55     dc.SetTextColor(RGB(0,0,0));
 56     dc.DrawText(string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);//单行、水平、竖直居中的文本
 57     */
 58 
 59     /*LOFGFONT 自定义字体 呈现出旋转效果
 60     CPaintDC dc(this);
 61     dc.SetViewportOrg(rect.Width()/2,rect.Height()/2);
 62     dc.SetBkMode(TRANSPARENT);
 63 
 64     for(int i=0;i<3600;i+=360){
 65         LOGFONT lf;//自己创建一种字体
 66         ::ZeroMemory (&lf,sizeof(lf));
 67         lf.lfHeight=180;     //26个像素点
 68         lf.lfWeight=FW_BOLD; //粗黑
 69         //lf.lfItalic=TRUE;    //倾斜
 70         lf.lfEscapement=i;     //配合使用每次旋转36度
 71         lf.lfOrientation=i;
 72         ::lstrcpy(lf.lfFaceName,_T("Arial"));
 73         CFont font;
 74         font.CreatePointFontIndirect(&lf);
 75 
 76         CFont* pOldFont=dc.SelectObject(&font);
 77         dc.TextOutA(0,0,CString(_T("Hello,MFC")));
 78         dc.SelectObject(pOldFont);
 79     }
 80     */
 81 }
 82 
 83 void CMainWindow::OnLButtonDown(UINT m_nFlags,CPoint point)//点击鼠标左键画图
 84 {
 85     //CClientDC dc(this);
 86     CPaintDC dc(this);
 87     CRect rect;
 88     GetClientRect(&rect);
 89     dc.Rectangle(rect);
 90     
 91 
 92     //dc.SetViewportOrg(rect.Width()/2,rect.Height()/2);
 93     
 94 
 95     //dc.SetROP2(R2_BLACK);//绘图模式:颜色操作;2.1.3
 96     //dc.MoveTo(rect.left,rect.top);
 97     //dc.LineTo(rect.right,rect.bottom);
 98 
 99     //POINT aPoint[5]={0,0,0,100,100,100,100,0,0,0};
100     //dc.Polyline(aPoint,5);         //将一系列点用线段连起来
101 
102     //POINT bPoint[4]={0,100,100,100,100,0};
103     //dc.MoveTo(0,0);
104     //dc.PolylineTo(bPoint,4);          //从当前位置开始将一系列的点用线段连起来,并将当前位置移折致折线的终点
105     
106     //int nWidth=rect.Width();            //画正弦曲线
107     //int nHeight=rect.Height();
108 
109     //CPoint aPoint[SEGMENTS];
110     //for(int i=0;i<SEGMENTS;i++){
111     //    aPoint[i].x=(i*nWidth)/SEGMENTS;
112     //    aPoint[i].y=(int)((nHeight/2)*(1-(sin((2*PI*i)/SEGMENTS))));
113     //}
114     //dc.Polyline(aPoint,SEGMENTS);
115 
116     /*画笔的创建和加载
117     CPen cPen(PenStyle[(PenNum++)%7],1,RGB(192,0,0));//==cPen.CreatePen(...);
118     dc.SelectObject(&cPen);
119     CBrush cBrush(BrushStye[(BrushNum++)%6],RGB(255,0,0));
120     dc.SelectObject(&cBrush);
121     */
122 
123     /*扩展笔____必须是通路
124     LOGBRUSH lb;
125     lb.lbStyle=BS_SOLID;
126     lb.lbColor=RGB(0,255,0);
127     CPen pen(PS_GEOMETRIC|PenStyle[(PenNum++)%7]|PS_ENDCAP_FLAT|
128         PS_JOIN_ROUND,3,&lb);
129     CPen* pOldPen=dc.SelectObject(&pen);
130     */
131 
132 
133     /*画刷原点
134     CPoint point(x1,y1);     //矩形左上角的逻辑坐标
135     dc.LPtoDP(&point);       //逻辑坐标转换成设备坐标     
136     point.x %= 8;            
137     point.y %= 8;
138     cBrush.UnrealizeObject();//允许画刷原点移动
139     dc.SetBrushOrg(point);   //0~7之间
140     dc.SelectObject(&cBrush);
141     dc.Rectangle(x1,y1,x2,y2);
142     */
143 
144 
145     /*建立通路
146     dc.BeginPath();
147     dc.MoveTo(0,0);
148     dc.LineTo(100,200);
149     dc.LineTo(200,100);
150     dc.CloseFigure();
151     dc.EndPath();
152     dc.StrokePath();
153     */
154 
155 
156     /*ARC
157     CRect rect0(0,0,200,100);
158     CPoint cPoint1(0,0);
159     CPoint cPoint2(200,100);
160     dc.Rectangle(rect0);
161     dc.Arc(rect0,cPoint1,cPoint2);//外接矩形+从中心引出的两个边界线所夹的范围
162     dc.MoveTo(0,0);
163     dc.LineTo(200,100);
164 
165     CRect rect1(0,102,200,202);
166     CPoint cPoint3(0,102);
167     CPoint cPoint4(200,202);    
168     dc.Rectangle(rect1);
169     dc.ArcTo(rect1,cPoint3,cPoint4);
170     CPoint cPointNow=dc.GetCurrentPosition();//终点坐标
171     */
172 
173     /*纳克
174     POINT aPoint1[4]={120,100,120,200,250,150,500,40};
175     POINT aPoint2[4]={120,100,50,350,250,200,500,40};
176     dc.PolyBezier(aPoint1,4);
177     dc.PolyBezier(aPoint2,4);
178     */
179 
180 
181 
182     //dc.SetMapMode(MM_TEXT);//映射模式2.1.4 按比例缩放输出
183     //dc.Ellipse(0,0,100,100);//矩形内切圆(弧)
184     //dc.Ellipse(rect.right-100,rect.bottom-100,rect.right,rect.bottom);
185     //dc.Ellipse(point.x,point.y,point.x+100,point.y+100);//鼠标位置
186 
187     //dc.SetMapMode(MM_ISOTROPIC);//可编程映射模式,自定义比例缩放(MM_ANISOTROPIC单位长度可以不同;MM_ISOTROPIC单位长度相同)
188     //dc.SetWindowExt(500,500);     //设置窗口逻辑值为(0,0)到(500,500)
189     //dc.SetViewportExt(rect.Width(),rect.Height());
190     //dc.Ellipse(0,0,500,500);
191 
192 }