MFC视频教学第一课,做一个简单的界面,理解应用程序和操作系统之间的消息传递机制

 

#include <windows.h> #include <stdio.h> LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); //WinMain是操作系统调用的,是系统的入口函数 int WINAPI WinMain( HINSTANCE hInstance,//当前应运程序的实例号 HINSTANCE hPrevInstance, LPSTR lpCmdLine,//字符串类型的指针,命令行参数 int nCmdShow//程序窗口显示的状态 ) { //创建一个窗口类 WNDCLASS wndclass; wndclass.cbClsExtra=0;//为类分配的额外内存,一般置0 wndclass.cbWndExtra=0;//为窗口分配的额外内存,一般置0 wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//窗口的背景 wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);//鼠标的类型 wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);//图标的类型 wndclass.hInstance=hInstance;//当前应运程序的实例号 wndclass.lpfnWndProc=WinSunProc;//指向窗口过程函数的指针 wndclass.lpszClassName="Winxin2010";//类的名字 wndclass.lpszMenuName=NULL;//菜单的名字 wndclass.style=CS_HREDRAW | CS_VREDRAW;//窗口特性的组合 //注册一个类 RegisterClass(&wndclass); //创建一个窗口 HWND hwnd; hwnd=CreateWindow("Winxin2010","索广宇自学MFC",WS_OVERLAPPEDWINDOW, 100,100,600,400,NULL,NULL,hInstance,NULL); //显示及更新窗口 ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); //消息循环 MSG msg; while(GetMessage(&msg,NULL,0,0))//GetMessage()的作用是从消息队列中取出消息 { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch(uMsg) { case WM_CHAR: char szChar[20]; sprintf(szChar,"char is %d",wParam); MessageBox(hwnd,szChar,"索广宇加油",MB_OK); break; case WM_LBUTTONDOWN: MessageBox(hwnd,"Mouse Click!","索广宇加油",MB_OK); HDC hdc; hdc=GetDC(hwnd); TextOut(hdc,0,50,"索广宇爱邢飘!",strlen("索广宇爱邢飘!")); ReleaseDC(hwnd,hdc); break; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC=BeginPaint(hwnd,&ps); TextOut(hDC,0,0,"执子之手,与子协老!",strlen("执子之手,与子协老!")); EndPaint(hwnd,&ps); break; case WM_CLOSE: if(IDYES==MessageBox(hwnd,"真的要退出吗?","索广宇加油",MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } 

你可能感兴趣的:(null,mfc,callback,include,winapi)