突然自己想完整地写一款mm,除了网络通信、文件操作之类的功能类代码外,还要搞一个界面的东西。想了想,为了快速看到成果就学学界面编程吧,也方便看别人的带界面的代码。于是乎,决定windows编程先从界面开始写。
新建一个空的win32工程,只有以下代码:
然后贴上一个示例代码,几乎是同一的格式,vs自动生成的页差不多这样子。
// WIN32APP.cpp : Defines the entry point for the application.//#include "stdafx.h"LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ // TODO: Place code here. char szClassName[]="MainWClass"; WNDCLASSEX wndclass; wndclass.cbSize=sizeof(wndclass); wndclass.style = CS_HREDRAW|CS_VREDRAW; wndclass.lpfnWndProc = MainWndProc; 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 = szClassName; wndclass.hIconSm = NULL; RegisterClassEx(&wndclass); HWND hwnd = CreateWindowEx( 0, szClassName, "My first Window!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL,"create windows error !","error",MB_OK); return -1; } ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam;}LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){ char szText[]="most simple window"; switch(message) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd,&ps); TextOut(hdc,10,10,szText,strlen(szText)); EndPaint(hwnd,&ps); return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam);}这里记录一下如何添加图标资源和菜单:
其实很简单,代码也很容易懂,菜鸟只是记录一下。
再添加一个图标玩玩
继续前进!哈哈!