MOOC清华《VC++面向对象与可视化程序设计》第3章:Windows文本-例(1)文本显示示例程序

#include <windows.h>
#include <stdlib.h>
#include <string.h>
LRESULT CALLBACK 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;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
	static long nXChar,nCaps,nYChar;
	HDC hDC;   //定义指向设备上下文的句柄	
	short x;
    TEXTMETRIC tm;
	short LnCount=6;
	PAINTSTRUCT PtStr; //定义指向包含绘图信息的结构体变量
	static char *textbuf[]=
			{
			"This is the first line",
			"This is the second line",
			"This is the third line",
			"This is the fourth line",
			"This is the fifth line",
			"This is the sixth line"
			};
	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)) ;
		}
}

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="WinText";
		WndClass.lpszMenuName=NULL;
		WndClass.style=CS_HREDRAW|CS_VREDRAW;
		return RegisterClass(&WndClass);
}

BOOL InitWindows(HINSTANCE hInstance,int nCmdShow) //初始化窗口
{
	HWND hWnd;
		hWnd=CreateWindow("WinText",  //生成窗口
						"文本显示示例程序",
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT,
						0,
						CW_USEDEFAULT,
						0,
						NULL,
						NULL,
						hInstance,
						NULL);
		if(!hWnd)
			return FALSE;
		ShowWindow(hWnd,nCmdShow);//显示窗口
		UpdateWindow(hWnd);
		return TRUE;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值