[游戏学习24] MFC 各种绘图 字体学习

时间:2021-02-03 06:36:20

>_<:这里包含字体设置及各种绘图,只要稍微看一下代码就能理解,这里不多介绍

[游戏学习24] MFC 各种绘图 字体学习

>_<:Hello.h

 #include<afxwin.h>
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow:public CFrameWnd
{
public:
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT m_nFlags,CPoint point);
DECLARE_MESSAGE_MAP();
};

>_<:Hello.cpp

 #include<afxwin.h>
#include<math.h>
#include"Hello.h"
#define SEGMENTS 500
#define PI 3.1415926 int PenStyle[]={PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};//实线\宽\窄\宽+窄\宽+2窄\没有\实线不外伸
int PenNum=;
int BrushStye[]={HS_BDIAGONAL,HS_FDIAGONAL,HS_CROSS,HS_HORIZONTAL,HS_DIAGCROSS,HS_VERTICAL};//45\135\90交叉\0\45交叉\90
int BrushNum=; 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()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP() CMainWindow::CMainWindow() //创建窗口
{
Create(NULL,_T("The Hello"),WS_OVERLAPPED,CRect(,,,));//CFrameWnd下的成员函数8个参数,6个默认
} //这个参数产生垂直滚动条 void CMainWindow::OnPaint()
{ CRect rect;
GetClientRect(&rect); /*72点,Arial,并带有下拉阴影生成的“Hello MFC"
CFont font;
font.CreatePointFont(720,_T("Arial"));//创建72点,Arial字体 CPaintDC dc(this);
dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);//背景模式 设为透明 CString string=_T("Hello MFC"); rect.OffsetRect(16,16);//从窗口中心向右向下偏移几个像素点的位置
dc.SetTextColor(RGB(192,192,192));
dc.DrawText(string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER); rect.OffsetRect(-16,-16);
dc.SetTextColor(RGB(0,0,0));
dc.DrawText(string,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);//单行、水平、竖直居中的文本
*/ /*LOFGFONT 自定义字体 呈现出旋转效果
CPaintDC dc(this);
dc.SetViewportOrg(rect.Width()/2,rect.Height()/2);
dc.SetBkMode(TRANSPARENT); for(int i=0;i<3600;i+=360){
LOGFONT lf;//自己创建一种字体
::ZeroMemory (&lf,sizeof(lf));
lf.lfHeight=180; //26个像素点
lf.lfWeight=FW_BOLD; //粗黑
//lf.lfItalic=TRUE; //倾斜
lf.lfEscapement=i; //配合使用每次旋转36度
lf.lfOrientation=i;
::lstrcpy(lf.lfFaceName,_T("Arial"));
CFont font;
font.CreatePointFontIndirect(&lf); CFont* pOldFont=dc.SelectObject(&font);
dc.TextOutA(0,0,CString(_T("Hello,MFC")));
dc.SelectObject(pOldFont);
}
*/
} void CMainWindow::OnLButtonDown(UINT m_nFlags,CPoint point)//点击鼠标左键画图
{
//CClientDC dc(this);
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.Rectangle(rect); //dc.SetViewportOrg(rect.Width()/2,rect.Height()/2); //dc.SetROP2(R2_BLACK);//绘图模式:颜色操作;2.1.3
//dc.MoveTo(rect.left,rect.top);
//dc.LineTo(rect.right,rect.bottom); //POINT aPoint[5]={0,0,0,100,100,100,100,0,0,0};
//dc.Polyline(aPoint,5); //将一系列点用线段连起来 //POINT bPoint[4]={0,100,100,100,100,0};
//dc.MoveTo(0,0);
//dc.PolylineTo(bPoint,4); //从当前位置开始将一系列的点用线段连起来,并将当前位置移折致折线的终点 //int nWidth=rect.Width(); //画正弦曲线
//int nHeight=rect.Height(); //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); /*画笔的创建和加载
CPen cPen(PenStyle[(PenNum++)%7],1,RGB(192,0,0));//==cPen.CreatePen(...);
dc.SelectObject(&cPen);
CBrush cBrush(BrushStye[(BrushNum++)%6],RGB(255,0,0));
dc.SelectObject(&cBrush);
*/ /*扩展笔____必须是通路
LOGBRUSH lb;
lb.lbStyle=BS_SOLID;
lb.lbColor=RGB(0,255,0);
CPen pen(PS_GEOMETRIC|PenStyle[(PenNum++)%7]|PS_ENDCAP_FLAT|
PS_JOIN_ROUND,3,&lb);
CPen* pOldPen=dc.SelectObject(&pen);
*/ /*画刷原点
CPoint point(x1,y1); //矩形左上角的逻辑坐标
dc.LPtoDP(&point); //逻辑坐标转换成设备坐标
point.x %= 8;
point.y %= 8;
cBrush.UnrealizeObject();//允许画刷原点移动
dc.SetBrushOrg(point); //0~7之间
dc.SelectObject(&cBrush);
dc.Rectangle(x1,y1,x2,y2);
*/ /*建立通路
dc.BeginPath();
dc.MoveTo(0,0);
dc.LineTo(100,200);
dc.LineTo(200,100);
dc.CloseFigure();
dc.EndPath();
dc.StrokePath();
*/ /*ARC
CRect rect0(0,0,200,100);
CPoint cPoint1(0,0);
CPoint cPoint2(200,100);
dc.Rectangle(rect0);
dc.Arc(rect0,cPoint1,cPoint2);//外接矩形+从中心引出的两个边界线所夹的范围
dc.MoveTo(0,0);
dc.LineTo(200,100); CRect rect1(0,102,200,202);
CPoint cPoint3(0,102);
CPoint cPoint4(200,202);
dc.Rectangle(rect1);
dc.ArcTo(rect1,cPoint3,cPoint4);
CPoint cPointNow=dc.GetCurrentPosition();//终点坐标
*/ /*纳克
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);
*/ //dc.SetMapMode(MM_TEXT);//映射模式2.1.4 按比例缩放输出
//dc.Ellipse(0,0,100,100);//矩形内切圆(弧)
//dc.Ellipse(rect.right-100,rect.bottom-100,rect.right,rect.bottom);
//dc.Ellipse(point.x,point.y,point.x+100,point.y+100);//鼠标位置 //dc.SetMapMode(MM_ISOTROPIC);//可编程映射模式,自定义比例缩放(MM_ANISOTROPIC单位长度可以不同;MM_ISOTROPIC单位长度相同)
//dc.SetWindowExt(500,500); //设置窗口逻辑值为(0,0)到(500,500)
//dc.SetViewportExt(rect.Width(),rect.Height());
//dc.Ellipse(0,0,500,500); }