文本与图形的小结

空白C++文件:

1.Window的基本初始化

#include<windows.h>
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);//初始化函数
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);//初始化函数
//主函数
int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{	MSG Message;
	if(!InitWindowsClass(hInstance))		return FALSE;
	if(!InitWindows(hInstance,nCmdShow))	return FALSE;
	while(GetMessage(&Message,0,0,0))		//消息循环
	{	TranslateMessage(&Message);
		DispatchMessage(&Message); }
	return Message.wParam;
}

long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{}//具体内容
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow) //初始化窗口
{	HWND hWnd;
	hWnd=CreateWindow(      "WinFill",  		//类名
				"填充示例程序",           //窗口名
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT,
				0,
				CW_USEDEFAULT,
				0,
				NULL,
				NULL,
				hInstance,
				NULL);
	if(!hWnd)
		return FALSE;
	ShowWindow(hWnd,nCmdShow);			//显示窗口
	UpdateWindow(hWnd);
	return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance) 		//定义窗口类
{WNDCLASS WndClass;
 WndClass.cbClsExtra=0;
 WndClass.cbWndExtra=0;
 WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
 WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
 WndClass.hIcon=LoadIcon(NULL,"END");
 WndClass.hInstance=hInstance;
 WndClass.lpfnWndProc=WndProc;
 WndClass.lpszClassName="WinFill";//类名
 WndClass.lpszMenuName=NULL;
 WndClass.style=CS_HREDRAW|CS_VREDRAW;
 return RegisterClass(&WndClass);
}

2.long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam){}的基本书写方式

1.创建句柄

HDC hdc;//获得设备句柄
	HFONT hf;//获得字体句柄
	PAINTSTRUCT ps;//定义绘图结构体
	HBRUSH hBrush;	//定义画刷句柄
	HPEN hPen,hPen1,hPen2,hPen3;//定义画笔句柄	}

2.switch-case结构

	switch(message)
	{
		case WM_PAINT:
			hdc=BeginPaint(hwnd,&ps);//取得设备环境句柄
			SetWindowPos(hwnd,NULL,0,0,600,600,SWP_NOMOVE);//定义窗口大小为600X600
                        SetWindowOrgEx(hdc,-300,-300,NULL);	//定义坐标在窗口中央
。。。//具体内容
		        EndPaint(hwnd,&ps);	//删除设备环境指针.
		        if(i<=n) InvalidateRect(hwnd,NULL,1);	//刷新用户区.//用此函数,加上合理的条件,不断的刷新用户界面,将messahe传入Switch中,WM_PAINT接收
                        break;
                case WM_DESTROY:
	 		PostQuitMessage(0);
			break;   
                default:
			return  DefWindowProc(hwnd,message,wParam,lParam);
         }

还有其他的一些消息用于switch-case中

WM_CREATE:用于一开始窗口创建的消息(可以用于一些初始化);

switch(message){
     case WM_CREATE:
             SetTimer(hwnd,1,300,null)//0.1s
     case WM_PAINT://具体内容
     case WM_TIMER:
        InValidateRect(hwnd,null,1);//刷新窗口界面
}
switch(iMessage)  //处理消息
		{
			case WM_CREATE:					//处理窗口创建消息
				hDC=GetDC(hWnd) ;			//获取当前设备表句柄
				GetTextMetrics(hDC,&tm);	//获取字体信息
				nXChar=tm.tmAveCharWidth;	//获取字符宽度
				nYChar=tm.tmHeight+tm.tmExternalLeading;
				ReleaseDC(hWnd,hDC);		//释放当前设备句柄
				return 0;
			case WM_PAINT:					//处理重画消息
				hDC=BeginPaint(hWnd,&PtStr); //开始绘画
				for(x=0;x<LnCount;x=x+1)  
					TextOut(hDC,nXChar,nYChar *(1+x),textbuf[x],lstrlen(textbuf[x])); 
				EndPaint(hWnd,&PtStr);
				return 0;
			case WM_DESTROY:				//结束应用程序
				PostQuitMessage(0);
				return 0;
			default:						//其他消息处理程序
				return(DefWindowProc(hWnd,iMessage,wParam,lParam)) ;
		}







eg:	case WM_CHAR:
		if(wParam=='a'||wParam=='A')
		    nMode=MM_ISOTROPIC;
		else if(wParam=='b'||wParam=='B')
			nMode=MM_ANISOTROPIC;
		else if(wParam=='c'||wParam=='C')
			nMode=MM_LOMETRIC;
		else 
			;
		InvalidateRect(hwnd,NULL,1);		//刷新用户区
		break;

3.绘图的工具

3.1画笔

创建 首先在swith-case上面要定义一个句柄 HPEN hp;

3.1.1可以调用GetStockObject()函数来获取windows自定义的四中画笔。WHILE_PEN,BLACK_PEN,DC_PEN,NULL_PEN

hp=(Hpen)GetStockObject(BLACK_PEN);
//强制类型转换(可能为画刷)

3.1.2创建自定义的画笔

基本样式:PS_DASH,PS_DASHDOT,PS_DASHDOTDOT,PS_DOT,PS_NULL,PS_INSTDEFRAME,PS_SOLIDE(最后两个为实线)

hp=CreatePen(PS_SOLIDE,int Width,RGB(int a,int b,int c));

3.1.3

调用selectobject(hDC,hp);将其选入设备环境,用完后要DeleteObject(hp);

3.2画刷

同画笔,定义句柄 HBRUSH hbrush;

创建:1. hBrush=CreateSolidBrush(COLORREF crcolor);

//几种基本颜色:WHILE_BRUSH,LTGREAY_BRUSH,GREY_BRUSH,DKGREY_BRUSH,BLACK_BRUSH,HOLLOW_BRUSH空心画刷,NULL_BRUSH

2.hbrush=CreateHatchBrush(int nIndex,COLORREF crcolor);

nIndex为 HS_HORTZONTAL水平线,HS_VERTICAL垂直,HS_FDIAGONAL正斜线,HS_BDIAGONAL反斜线,HS_CROSS十字线,HS_DIAGCROSS,斜十字线

调用selectobject(hDC,hbrush);将其选入设备环境,用完后要DeleteObject(hbrush);

4.常用绘图函数(前面要包含hdc,设备环境)

1.SetPiex(),GetPiex()

COLORREF SetPiex(hdc,int x,int y,COLERREF COLOE);

COLORREF SetPiex(hdc,Point p,COLERREF COLOE);

COLORREF GetPiex(hdc,int x,int y) const;

COLORREF GetPiex(hdc,Point p) const;

2.LineTo(),MoveTo()

LineTo()从起始点到指定点,MovePoint(),移到指定点。

BOOL LineTo(hdc,int x,int y);

BOOL LineTo(hdc,Point p);

CPoint MoveTo(hdc,int x,int y);

CPoint MoveTo(hdc,POINT point);

3.Polyline()和PolylineTo()

前者是从第一个坐标开始画,后者是重零点。

BOOL Polyline(hdc,LPPOINT p,int count);//POINT结构,点数

BOOL PolylineTo(hdc,LPPOINT p,int count);//POINT结构,点数

4.Rectangle(),RoundRect()

BOOL Rectangle(hdc,int x,int y,int x1,int y1);

BOOL Rectangle(hdc,LPCRECT lpRect);//CRECT结构

BOOL RoundRect(hdc,int x,int y,int x1,int y1,int a,int b);//a,b为圆角的高、宽

BOOL RoundRect(hdc,int x,PCRECT lpRect,POINT p);

5.Arc()和ArcTo()

BOOL Arc(hdc,int x,int y,int x1,int y1,int x2,int y2,int x3,int y3)//(x2,y2),(x3,y3)为起始点前者为矩形内切椭圆

BOOL Arc(hdc,LPCRECT lpRect,POINT p,POINT p);

同Polylineto

6.Ellipse()

BOOL Ellipse(hdc,int x1,int y1,int x2,int y2);

BOOL Ellipse(hdc,LPCRECT lpRect);

7.pie()填充

同Arc()

8.Polygon(hdc,LPPOINT lpPoints,int count);

封闭多边形,最后不用起始点。

5.文本

1.创建 句柄 HFONT hf;

创建字体样式简单函数:

HFONT CreateFont(HDC hDC,int nCharHeight,BOOL bItalic)
{
	HFONT hFont;
	hFont=CreateFont(				//定义字体句柄.
		nCharHeight,				//字体高度.
		0,							//由系统根据高宽比选取字体最佳宽度值.
		0,							//文本倾斜度为0,表示水平.
		0,							//字体倾斜度为0.
		400,						//字体粗度.400为正常.
		bItalic,					//是斜体字?,
		0,							//无下划线.
		0,							//无删除线.
		ANSI_CHARSET,				//表示所用的字符集为ANSI_CHARSET.
		OUT_DEFAULT_PRECIS,			//删除精度为缺省值.
		CLIP_DEFAULT_PRECIS,		//裁剪精度为缺省值.
		DEFAULT_QUALITY,			//输出质量为缺省值.
		DEFAULT_PITCH|FF_DONTCARE,	//字间距和字体系列使用缺省值.
		"Arial");					//字体名称.
	if(hFont == NULL) return NULL;
	else return hFont;
}
eg:hf=CreateFont(hdc,30,FALSE);


2.SelectObject(hdc,hf);SetTextColor(hdc,RGB(255,0,0));DeleteObject(hf);

//可以不用1和2直接使用默认字体,见上面MK_CREATE消息,SetTextColor(hdc,RGB(255,0,0));DeleteObject(hf);

3.文字字符的宽与高等

用事先定义好的:TEXTMETRIC tm;

3.1 GetTextMetrics(hdc,&tm);//得到包含字体信息的结构体.

int y=tm.tmHeight+tm.ExternalLeading;//行距,用其来写一行行字

3.2 GetTextExtentPoint32(hdc,S,count,&A);//S为字符串,count 字符个数,A 为SIZE结构体

size.x为字符串的宽度,size.y为其高度。

4.TextOut()

virtual BOOL TextOut(int x,int y,LPCTSTR,LpszString,int count);

x,y用上述两个函数,

6.常用句柄


自己写的一段程序:

#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define Pi 3.1415926

int i=0;
int a=80,b=10,h=15;//三角形上顶点距离(0,0)为a,三角形的底边为2b,高为c
int c=15,d=10;//长方形的长2c,高为d
int e=50;//两边杆长度为50
int n=72;//把圆划分为n份
POINT p1[]={{-a,0},{-a-b,h},{-a+b,h},{-a,0}};//定义三角形的三点为(-80,0),(-90,15),(-70,15);
POINT p2[]={{a,0},{a+b,h},{a-b,h},{a,0}};//对称三角形
POINT p3[]={{-a,0},{-a,0},{a,0},{a,0}};//杆的初始化4个点坐标

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HFONT CreateFont(HDC hDC,int nCharHeight,BOOL bItalic);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
	HWND hwnd ;
	MSG Msg ;
	WNDCLASS wndclass ;
	char lpszClassName[] = "机械"; 
	char lpszTitle[]= "四杆机构";
    wndclass.style = 0; 
	wndclass.lpfnWndProc = WndProc ;
	wndclass.cbClsExtra	= 0 ;
	wndclass.cbWndExtra	= 0 ;
	wndclass.hInstance = hInstance ;
	wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;
	wndclass.hCursor = LoadCursor( NULL, IDC_ARROW) ;
	wndclass.hbrBackground =(HBRUSH) GetStockObject( WHITE_BRUSH) ;
	wndclass.lpszMenuName = NULL ;
	wndclass.lpszClassName = lpszClassName ;
	if( !RegisterClass( &wndclass))
	{
		MessageBeep(0) ;
		return FALSE ;
	}

	hwnd = CreateWindow
			(
			lpszClassName,
			lpszTitle,
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			NULL,
			NULL,
			hInstance,
			NULL
			);

ShowWindow( hwnd, nCmdShow) ;				//显示窗口
UpdateWindow(hwnd);							//绘制用户区
	while( GetMessage(&Msg, NULL, 0, 0))		//消息循环
	{
		TranslateMessage( &Msg) ;
		DispatchMessage( &Msg) ;
	}
	return Msg.wParam;						//消息循环结束时将信息返回系统
}


LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM  wParam,LPARAM  lParam)

{	
	HDC hdc;
	HFONT hf;
	PAINTSTRUCT ps;
	HBRUSH hBrush;			//定义画刷句柄
	HPEN hPen,hPen1,hPen2,hPen3;//定义画笔句柄
	TEXTMETRIC tm;
	char S[]="四杆机构";
	int count=strlen(S);
	int X=0,Y=-200;
	SIZE A;
	switch(message)
	{
		case WM_PAINT:
			hdc=BeginPaint(hwnd,&ps);//取得设备环境句柄
			SetWindowPos(hwnd,NULL,0,0,600,600,SWP_NOMOVE);//定义窗口大小为600X600
            SetWindowOrgEx(hdc,-300,-300,NULL);	//定义坐标在窗口中央
			hPen=CreatePen(PS_SOLID,3,RGB(96,96,96));
			hPen1=CreatePen(PS_SOLID,3,RGB(255,0,0));
			hPen2=CreatePen(PS_SOLID,3,RGB(255,0,255));
			hPen3=CreatePen(PS_SOLID,3,RGB(255,23,140));

			hf=CreateFont(hdc,30,FALSE);
			SelectObject(hdc,hf);
			SetTextColor(hdc,RGB(255,0,0));
			GetTextMetrics(hdc,&tm);//得到包含字体信息的结构体.
			GetTextExtentPoint32(hdc,S,count,&A);
			X-=A.cx/2;
			TextOut(hdc,X,Y,S,count);
			DeleteObject(hf);				//删除字体句柄.
			
            hBrush=CreateSolidBrush(GRAY_BRUSH);
			SelectObject(hdc,hPen);
			SelectObject(hdc,hBrush);
			Polygon(hdc,p1,4);      //三角形
			Polygon(hdc,p2,4);
		    DeleteObject(hBrush);	//删除画刷.
			hBrush=CreateHatchBrush(HS_BDIAGONAL,RGB(0,0,0));
			SelectObject(hdc,hBrush);
			Rectangle(hdc,-a-c,h,-a+c,h+d);//矩形
			Rectangle(hdc,a+c,h,a-c,h+d);
			
			DeleteObject(hBrush);	//删除画刷.

			p3[1].x=(long)(-a+e*cos(i*2*Pi/n));//杆,POINT类点的类型为LONG,所以需要强制类型转换。
			p3[1].y=(long)(-e*sin(i*2*Pi/n));
			p3[2].x=(long)(a+e*cos(i*2*Pi/n));
			p3[2].y=(long)(-e*sin(i*2*Pi/n));
			if(i<=n/4){
				SelectObject(hdc,hPen);			   
				Polyline(hdc,p3,4);
			}else if(i<=n/2){
			    SelectObject(hdc,hPen1);			   
				Polyline(hdc,p3,4);
			}else if(i<=3*n/4){
			    SelectObject(hdc,hPen2);			   
				Polyline(hdc,p3,4);
			}else {
			    SelectObject(hdc,hPen3);			   
				Polyline(hdc,p3,4);
			}
			
			
			
			SelectObject(hdc,hBrush);
			for(int j=0;j<4;j++){
				int R=3;//铰链的半径
			    Ellipse(hdc,p3[j].x-R,p3[j].y-R,p3[j].x+R,p3[j].y+R);
			}

			i++;
			DeleteObject(hPen);
			DeleteObject(hPen1);
			DeleteObject(hPen2);
			DeleteObject(hPen3);
		    EndPaint(hwnd,&ps);	//删除设备环境指针.
			Sleep(200);//sleep内的数值越大越不容易闪屏.
		    if(i<=n) InvalidateRect(hwnd,NULL,1);	//刷新用户区.
		    
			break;
        case WM_DESTROY:
	 		PostQuitMessage(0);
			break;   
default:
			return  DefWindowProc(hwnd,message,wParam,lParam);
		}
return 0;
}

HFONT CreateFont(HDC hDC,int nCharHeight,BOOL bItalic)
{
	HFONT hFont;
	hFont=CreateFont(				//定义字体句柄.
		nCharHeight,				//字体高度.
		0,							//由系统根据高宽比选取字体最佳宽度值.
		0,							//文本倾斜度为0,表示水平.
		0,							//字体倾斜度为0.
		400,						//字体粗度.400为正常.
		bItalic,					//是斜体字?,
		0,							//无下划线.
		0,							//无删除线.
		ANSI_CHARSET,				//表示所用的字符集为ANSI_CHARSET.
		OUT_DEFAULT_PRECIS,			//删除精度为缺省值.
		CLIP_DEFAULT_PRECIS,		//裁剪精度为缺省值.
		DEFAULT_QUALITY,			//输出质量为缺省值.
		DEFAULT_PITCH|FF_DONTCARE,	//字间距和字体系列使用缺省值.
		"Arial");					//字体名称.
	if(hFont == NULL) return NULL;
	else return hFont;
}

猜你喜欢

转载自blog.csdn.net/black_szy/article/details/79919313