[英]Change default code Visual Studio 2015 Community
如何更改VS為Win32項目自動生成的代碼? 代替這個:
// testproj.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "testproj.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_TESTPROJ, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTPROJ));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTPROJ));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_TESTPROJ);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(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:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
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;
}
我希望它自動生成其他代碼。 我該怎么辦?
編輯:另外,如何使VS不為全局生成預編譯的標頭(我知道如何為每個項目更改它,但這很煩人)。 以及如何在默認情況下使用Win32的A
版本而不是W
?
在2015年,您不應該使用A
版本而不是W
版本的函數。不是每個代碼頁都包含您要使用的每個字符,將來您可能在某些重大項目中最終不僅僅使用英語,而且實際上我們在Internet上交流的所有數據都是采用Unicode形式的。 對於Windows程序,您將必須包含寬字符串和UTF-16。 Microsoft尚未發明可以讓我們直接在API中使用UTF-8的時間機器。 (Microsoft采用Unicode之前的UTF-8。) A
函數不適用於UTF-8,因為UTF-8不是有效的過程代碼頁(僅在字符集轉換函數中有效)。
如果您確實想盡可能避免使用UTF-16,則需要在每個API調用點之間在UTF-8和UTF-16之間進行轉換。 但是,不再建議不要對文本使用UTF-8或UTF-16。 實際上,根據您上面的代碼示例,VS2015放棄了以前版本的TCHAR
瘋狂,這一直令人困擾初學者Windows程序員( 尤其是此處的Stack Overflow),這是一件好事。
從https://msdn.microsoft.com/zh-cn/library/ms185319.aspx :
找到包含模板的.zip文件。 默認情況下,此文件位於
\\我的文檔\\ Visual Studio版本\\我的導出模板\\解壓縮.zip文件。
修改或刪除當前模板文件,或將新文件添加到模板。
打開,修改和保存.vstemplate XML文件,以處理更新的行為或新文件。 有關.vstemplate架構的更多信息,請參見Visual Studio模板架構參考。 有關可以在源文件中參數化的內容的更多信息,請參見模板參數。
選擇模板中的文件,右鍵單擊,單擊“發送到”,然后單擊“壓縮(壓縮)文件夾”。 您選擇的文件被壓縮為.zip文件。
將新的.zip文件與舊的.zip文件放在同一目錄中。
刪除提取的模板文件和舊的模板.zip文件。
(以管理員身份)啟動Developer Command Prompt實例(在開始菜單上,在Visual Studio 2010 / Visual Studio Tools / Developer Command Prompt下)。
運行以下命令:devenv / installvstemplates。
其他提到的事情也可以更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.