MFC入门级程序

来源:互联网 发布:淘宝买家订单数据诈骗 编辑:程序博客网 时间:2024/05/29 04:31
#include <windows.h>#include <stdio.h>
/*#define CALLBACK    __stdcall#define WINAPI      __stdcall#define WINAPIV     __cdecl#define APIENTRY    WINAPI#define APIPRIVATE  __stdcall#define PASCAL      __stdcall其中VC++默认调用的是__cdecl的C调用模式,而PASCAL默认调用的是__stdcall*/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){ /*结构体 typedef struct _WNDCLASS {     UINT    style;    //类类型    WNDPROC lpfnWndProc;  //指定回调函数    int     cbClsExtra;   //类的额外附加内存    int     cbWndExtra;   //窗口的额外附加内存    HANDLE  hInstance;   //当前应用程序实例号    HICON   hIcon;    //窗口图标,loadIcon    HCURSOR hCursor;   //窗口光标,loadCursor    HBRUSH  hbrBackground;  //获取画刷,GetStockObject    LPCTSTR lpszMenuName;  //long point,32位指针,指向菜单名称    LPCTSTR lpszClassName;  //long point,32位指针,指向窗口名称 } WNDCLASS;  */
 ///设计窗口类 WNDCLASS wndcls;   wndcls.cbClsExtra=0;  wndcls.cbWndExtra=0; wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); //数据类型的强制转换,HGDIOBJ->HBRUSH wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndcls.hInstance=hInstance; wndcls.lpfnWndProc=WinSunProc; //将回调函数指针传给lpfnWndProc wndcls.lpszClassName="Weixin2003"; wndcls.lpszMenuName=NULL; wndcls.style=CS_HREDRAW | CS_VREDRAW; //注册窗口类 RegisterClass(&wndcls);
 //创建窗口 /*创建函数 HWND CreateWindow(   LPCTSTR lpClassName,  // pointer to registered class name:  wndcls.lpszClassName   LPCTSTR lpWindowName, // pointer to window name   DWORD dwStyle,        // window style: WS_OVERLAPPEDWINDOW 叠加类型 WS_POPUPWINDOW   int x,                // horizontal position of window   int y,                // vertical position of window   int nWidth,           // window width   int nHeight,          // window height   HWND hWndParent,      // handle to parent or owner window 父窗口的句柄   HMENU hMenu,          // handle to menu or child-window identifier   HANDLE hInstance,     // handle to application instance   LPVOID lpParam        // pointer to window-creation data ,作为WM_CREATE的参数 ); */  HWND hwnd; hwnd=CreateWindow("Weixin2003","标题",WS_OVERLAPPEDWINDOW /*&~ WS_MAXIMIZEBOX*/,  200,200,600,400,NULL,NULL,hInstance,NULL);
 //显示,更新 ShowWindow(hwnd,SW_SHOWMINIMIZED); UpdateWindow(hwnd);
 //消息循环 /* BOOL GetMessage(  LPMSG lpMsg,         // address of structure with message ,消息结构体的指针  HWND hWnd,           // handle of window  UINT wMsgFilterMin,  // first message  UINT wMsgFilterMax   // last message 若wMsgFilterMin,wMsgFilterMax均为0,则不过滤消息 ); */ MSG msg;  while(GetMessage(&msg,NULL,0,0)) //If the function retrieves a message other than WM_QUIT, the return value is nonzero.即当应用程序发送WM_QUIT时,消息循环结束 {  TranslateMessage(&msg); //对消息对进行转换,产生新的消息 eg: WM_KEYDOWN/KEYUP转化为WM_CHAR  DispatchMessage(&msg); //将msg传给system,system将调用回调函数 } return 0;}
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: //打印按下字母的ASSCII码值  char szChar[20];  sprintf(szChar,"char is %d",wParam);  /*  int MessageBox(    HWND hWnd,          // handle of owner window    LPCTSTR lpText,     // address of text in message box    LPCTSTR lpCaption,  // address of title of message box    UINT uType          // style of message box  );  */  MessageBox(hwnd,szChar,"weixin",0);  break; case WM_LBUTTONDOWN:  MessageBox(hwnd,"mouse clicked","weixin",0);    HDC hdc; //device context的句柄  hdc=GetDC(hwnd);  /*  BOOL TextOut(    HDC hdc,           // handle to device context    int nXStart,       // x-coordinate of starting position    int nYStart,       // y-coordinate of starting position    LPCTSTR lpString,  // pointer to string    int cbString       // number of characters in string  );
  */  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,"是否真的结束?","weixin",MB_YESNO)) //常量卸载前面,如果非法赋值时会编译报错  {   DestroyWindow(hwnd); //销毁窗口,发送WM_DESTROY消息  }  break; case WM_DESTROY:  //The PostQuitMessage function indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.  PostQuitMessage(0);   break; default:  return DefWindowProc(hwnd,uMsg,wParam,lParam); //对不感兴趣的信号,让系统对其缺省处理 } return 0;}
0 0
原创粉丝点击