#include <windows.h> #include <stdio.h> LRESULT CALLBACK WinSunProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); 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; //应用程序实例句柄由WinMain函数传进来 wndcls.lpfnWndProc=WinSunProc; wndcls.lpszClassName="sunxin2006"; wndcls.lpszMenuName=NULL; wndcls.style=CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls); //创建窗口,定义一个变量用来保存成功创建窗口后返回的句柄 HWND hwnd; hwnd=CreateWindow("sunxin2006","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 ) { switch(uMsg) { case WM_CHAR: char szChar[20]; sprintf(szChar,"char code is %d",wParam); MessageBox(hwnd,szChar,"char",0); break; case WM_LBUTTONDOWN: MessageBox(hwnd,"mouse clicked","message",0); HDC hdc; hdc=GetDC(hwnd); //不能在响应WM_PAINT消息时调用 TextOut(hdc,0,50,"程序员之家",strlen("程序员之家")); //ReleaseDC(hwnd,hdc); break; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC=BeginPaint(hwnd,&ps); //BeginPaint只能在响应WM_PAINT消息时调用 TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www. sunxin.org")); EndPaint(hwnd,&ps); break; case WM_CLOSE: if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } ------------------------------------------------------------------------------------------------------ #include <windows.h> #include <stdio.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); //-------------------以下初始化窗口类--------------------------------------------- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInst,LPSTR lpszCmdLine,int nCmdShow) { HWND hwnd; MSG Msg; WNDCLASS wndclass; //定义窗口类 char lpszClassName[]="哈哈,for your smile!"; //窗口类名 char lpszTitle[]="MZone---我的地盘我作主!"; //窗口标题名 //初始化窗口类 wndclass.style=0; //窗口类型为默认类型 wndclass.lpfnWndProc=WndProc; //窗口处理函数为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) { switch(message) { case WM_DESTROY: PostQuitMessage(0); //向应用程序发出WM_QUIT消息,请求退出 default: return DefWindowProc(hwnd,message,wParam,lParam); //DefWindowProc 系统默认处理过程, } //以确保所有发送到该窗口的消息均被处理 return (0); }