win32模版文件分析(VS2013)

来源:互联网 发布:java支付宝接口demo 编辑:程序博客网 时间:2024/06/05 05:29

在VS2013中建立末模版工程会生成下面这些文件:

External Dependencies包含要用到的所有没有加入工程的文件;

Header Files包含工程中要用到的头文件;

Resource Files各种资源文件;

Source Files各种cpp原文件。


头文件参数


Header Files/resource.h


//{{NO_DEPENDENCIES}}// Microsoft Visual C++ generated include file.// Used by Win32Project4.rc//#define IDS_APP_TITLE103#define IDR_MAINFRAME128#define IDD_WIN32PROJECT4_DIALOG102#define IDD_ABOUTBOX103#define IDM_ABOUT104#define IDM_EXIT105#define IDI_WIN32PROJECT4107#define IDI_SMALL108#define IDC_WIN32PROJECT4109#define IDC_MYICON2#ifndef IDC_STATIC#define IDC_STATIC-1#endif// Next default values for new objects//#ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NO_MFC130#define _APS_NEXT_RESOURCE_VALUE129#define _APS_NEXT_COMMAND_VALUE32771#define _APS_NEXT_CONTROL_VALUE1000#define _APS_NEXT_SYMED_VALUE110#endif#endif



Header Files/stdafx.h


// stdafx.h : include file for standard system include files,// or project specific include files that are used frequently, but// are changed infrequently//#pragma once#include "targetver.h"#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers// Windows Header Files:#include <windows.h>// C RunTime Header Files#include <stdlib.h>#include <malloc.h>#include <memory.h>#include <tchar.h>// TODO: reference additional headers your program requires here



Header Files/targetver.h

#pragma once// Including SDKDDKVer.h defines the highest available Windows platform.// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.#include <SDKDDKVer.h>



Header Files/Win32Project4.h

#pragma once#include "resource.h"





CPP文件参数

Source Files/atdafx.cpp

// stdafx.cpp : source file that includes just the standard includes// Win32Project4.pch will be the pre-compiled header// stdafx.obj will contain the pre-compiled type information#include "stdafx.h"// TODO: reference any additional headers you need in STDAFX.H// and not in this file


Source Files/<工程名>.cpp


这个文件中有5个函数(一个主函数),3个结构体,1个宏定义。

下面是参数&声明定义:

// Win32Project4.cpp : Defines the entry point for the application.//#include "stdafx.h"#include "Win32Project4.h"#define MAX_LOADSTRING 100// 各种全局变量HINSTANCE hInst;// 当前实例TCHAR szTitle[MAX_LOADSTRING];// 标题栏文字TCHAR szWindowClass[MAX_LOADSTRING];// 主窗口类名// 模块函数声明// 注册该窗口应用程序,参数:窗口句柄ATOMMyRegisterClass(HINSTANCE hInstance); // 窗口消息回调函数,保存句柄并创建主窗口 成功返回trueBOOLInitInstance(HINSTANCE, int); // 获取主窗口消息LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);// 处理About对话框的各种消息 INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM); 

主函数_tWinMain共接收4个参数,详情见注释:

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,         // 应用程序当前实例句柄_In_opt_ HINSTANCE hPrevInstance, // 应用实例指向其他实例的句柄_In_ LPTSTR    lpCmdLine,         // 指向应用程序命令行参数的指针_In_ int       nCmdShow           // 指定窗口显示方式)          {UNREFERENCED_PARAMETER(hPrevInstance); // 禁止显示hPrevInstance变量未使用的警告UNREFERENCED_PARAMETER(lpCmdLine); // 禁止显示lpCmdLine变量未使用的警告 // TODO: Place code here.MSG msg;HACCEL hAccelTable;// Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); // 初始化全局字符串LoadString(hInstance, IDC_WIN32PROJECT4, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance); // 装载字符串表资源// 应用初始化,初始化失败返回 FALSEif (!InitInstance (hInstance, nCmdShow)){return FALSE;}//LoadAccelerators调入加速键表//hInstance模块的一个事例的句柄//(LPCTSTR)IDC_FIRSTVIEW指向一个以空结尾的字符串的指针,//该字符串包含了即将调入的加速键表的名字hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT4));// 主消息循环:while (GetMessage(&msg, NULL, 0, 0)){//TranslateAccelerator    翻译加速键表。该函数处理菜单//命令中的加速键。如果失败则执行TranslateMessage,DispatchMessage//msg.hwnd    窗口句柄,翻译该窗口的消息//hAccelTable   加速键表句柄//&msg   MSG结构体指针if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg); // 将虚拟键消息转换为字符消息DispatchMessage(&msg); // 传送消息给窗口程序}}        // 主消息循环结束return (int) msg.wParam; // 返回退出相关消息}

MyRegisterClass用来注册窗口应用程序。定义见下:

ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); // 窗口类大小wcex.style= CS_HREDRAW | CS_VREDRAW; // 窗口类型wcex.lpfnWndProc= WndProc; // 指定窗口处理函数为WndProcwcex.cbClsExtra= 0; // 窗口类扩展wcex.cbWndExtra= 0; // 窗口实例扩展wcex.hInstance= hInstance; // 当前实例句柄wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT4)); // 窗口图标wcex.hCursor= LoadCursor(NULL, IDC_ARROW); // 窗口中使用的光标类型wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1); // 窗口背景色wcex.lpszMenuName= MAKEINTRESOURCE(IDC_WIN32PROJECT4);  // 窗口中菜单样式wcex.lpszClassName= szWindowClass; // 窗口类名wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); // 设置窗口小图标return RegisterClassEx(&wcex); // 注册窗口类,注册成功返回窗口类标识号,失败返回NULL}



WinProc函数定义:

////  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)////  PURPOSE:  Processes messages for the main window.//// WM_COMMAND- 当用户从菜单选中一个命令项目、// 当一个控件发送通知消息给去父窗口// 或者按下一个快捷键将发送 WM_COMMAND 消息// WM_PAINT  - 绘制主窗口// WM_DESTROY - 发送退出程序消息LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message){case WM_COMMAND:wmId    = LOWORD(wParam);wmEvent = HIWORD(wParam);// Parse the menu selections:switch (wmId){case IDM_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_EXIT:DestroyWindow(hWnd);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}

WndProc函数定义:

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return (INT_PTR)TRUE;}break;}return (INT_PTR)FALSE;}


0 0
原创粉丝点击