记录学习点滴-《Windows 程序设计》-3-1

以前写过,不明不白地,现在算是有所了解了。不看书手写了一遍代码:

/*------------------------------------------------------------------
APP.cpp -- Testing Application function.
Muais, 2014
QQ:848506517
--------------------------------------------------------------------*/
#include <windows.h>

LRESULT CALLBACK WndProc(  HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							);
	HWND hwnd;
	HINSTANCE hInst;
int WINAPI WinMain(  HINSTANCE hInstance,      // handle to current instance
				   HINSTANCE hPrevInstance,  // handle to previous instance
				   LPSTR lpCmdLine,          // command line
				   int nCmdShow              // show state
				   )
{
	static TCHAR szAppName[]=TEXT("APP");
	WNDCLASS wndcls;
	MSG msg;

	//初始化
	wndcls.cbClsExtra=0;
	wndcls.cbWndExtra=0;
	wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndcls.hInstance=hInstance;
	wndcls.lpfnWndProc=WndProc;
	wndcls.lpszClassName=szAppName;
	wndcls.lpszMenuName=NULL;
	wndcls.style=CS_VREDRAW|CS_HREDRAW;
	//注册
	if (0==RegisterClass(&wndcls))
	{
		MessageBox(NULL,TEXT("注册窗口失败!"),TEXT("App Error"),MB_ICONERROR|MB_OK);
		return false;
	}
	hInst=hInstance;
	//创建
    hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,NULL,NULL);
	if (NULL==(hwnd))
	{
		MessageBox(NULL,TEXT("创建窗口失败!"),TEXT("App Error"),MB_ICONERROR|MB_OK);
		return false;
	}
	//显示
	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);
	//消息循环
	BOOL bRet;
	while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
	{ 
		if (bRet == -1)
		{
			// handle the error and possibly exit
			MessageBox(hwnd,TEXT("Error"),szAppName,MB_ICONERROR|MB_OK);
			break;
		}
		else
		{
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}
	}
	return msg.wParam;
}
 
LRESULT CALLBACK WndProc(  HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rt;
	static TCHAR info[]=TEXT("App is Runing");
	switch (uMsg)
	{
	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);
		GetClientRect(hwnd,&rt);
		if (!DrawText(hdc,info,wcslen(info),&rt,DT_VCENTER|DT_SINGLELINE|DT_CENTER))
		{
			MessageBox(hwnd,TEXT("显示文本出错!"),TEXT("APP ERROR"),MB_ICONERROR|MB_OK);
			return false;
		}
		EndPaint(hwnd,&ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

VS2012 程序运行结果:

Mark:

GetClientRect 函数用于获得窗口显示的矩形区域.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值