孙鑫VC++深入详解之第一个简单win32窗体程序

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <atlstr.h>


LRESULT CALLBACK WinSunProc(
							HWND hwnd,      // handle to window           窗口句柄
							UINT uMsg,      // message identifier         消息代码
							WPARAM wParam,  // first message parameter    消息代码附加参数1
							LPARAM lParam   // second message parameter   消息代码附加参数2
							);

int WINAPI WinMain(
				   HINSTANCE hInstance,      // handle to current instance  该程序当前运行的实例的句柄
				   HINSTANCE hPrevInstance,  // handle to previous instance 当前实例的前一个实例的句柄
				   LPSTR lpCmdLine,          // command line                一个以空终止的字符串
				   int nCmdShow              // show state                  指定程序的窗口应该如何显示
				   )
{
	WNDCLASS wndcls;                //设计一个窗口类
	wndcls.cbClsExtra=0;
	wndcls.cbWndExtra=0;
	wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);      //背景画刷句柄
	wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);     //光标句柄
	wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);         //图标句柄
	wndcls.hInstance=hInstance;                    //包含窗口过程的程序的实例句柄
	wndcls.lpfnWndProc=WinSunProc;                 //函数指针,指向窗口过程函数
	wndcls.lpszClassName=_T("sunxin2006");             //指定窗口类名字
	wndcls.lpszMenuName=NULL;                      //指定菜单资源名字
	wndcls.style=CS_HREDRAW | CS_VREDRAW;          //窗口样式

	RegisterClass(&wndcls);                        //注册窗口类

	HWND hwnd;

	//创建窗口
	hwnd=CreateWindow(_T("sunxin2006"),_T("http://www.sunxin.org"),WS_OVERLAPPEDWINDOW,
		0,0,600,400,NULL,NULL,hInstance,NULL);

	//显示窗口
	ShowWindow(hwnd,SW_SHOWNORMAL);

	//更新窗口
	UpdateWindow(hwnd);

	MSG msg;
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WinSunProc(
							HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							)
{
	ATL::CString data=_T("程序员之家");//原来的乱码是strlen函数在Unicode工程的问题
	//第一种方法,使用了atl的CString,也可以使用MFC中的CString
	
	switch(uMsg)
	{
	case WM_CHAR:
		TCHAR szChar[20];
		//本工程为Unicode工程
		wsprintf(szChar,_T("char code is %d"),wParam);
		MessageBox(hwnd,szChar,_T("char"),0);
		break;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd,_T("mouse clicked"),_T("message"),0);
		HDC hdc;
		hdc=GetDC(hwnd);
		TextOut(hdc,0,150,data,data.GetLength());//第一种方法
		TextOutA(hdc,0,50,"程序员之家",strlen("程序员之家"));//第二种方法使用TextOutA,针对unicode工程
		//ReleaseDC(hwnd,hdc);
		break;
	case WM_PAINT:
		HDC hDC;
		PAINTSTRUCT ps;
		hDC=BeginPaint(hwnd,&ps);      //得到DC
		TextOut(hDC,0,0,_T("http://www.sunxin.org"),strlen("http://www.sunxin.org"));
		EndPaint(hwnd,&ps);            //释放
		break;
	case WM_CLOSE:
		if(IDYES==MessageBox(hwnd,_T("是否真的结束?"),_T("message"),MB_YESNO))
		{
			DestroyWindow(hwnd);      //销毁窗口
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);           //向应用程序的消息队列投递一条WM_QUIT消息并返回
		break;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值