C++ 更改默认代码Visual Studio 2015社区
如何更改VS为Win32项目自动生成的代码?与此相反:C++ 更改默认代码Visual Studio 2015社区,c++,winapi,visual-studio-2015,C++,Winapi,Visual Studio 2015,如何更改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 inst
// 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?来自:
找到包含模板的.zip文件。默认情况下,此文件位于
\我的文档\Visual Studio版本\My导出
模板\
解压缩.zip文件
修改或删除当前模板文件,或将新文件添加到
模板
打开、修改和保存.vstemplate XML文件以处理更新的
行为或新文件。有关.vstemplate的详细信息
架构,请参见Visual Studio模板架构参考。更多
有关可以在源文件中参数化的内容的信息,请参阅
模板参数
选择模板中的文件,单击鼠标右键,单击“发送到”,然后单击
然后单击压缩压缩文件夹。您选择的文件是
压缩成一个.zip文件
将新的.zip文件与旧的.zip文件放在同一目录中
删除提取的模板文件和旧的template.zip文件
以管理员身份启动开发人员命令提示符的实例
在“开始”菜单上的Visual Studio 2010/Visual Studio工具下/
开发人员命令提示符
运行以下命令:devenv/installvstemplates
其他提到的内容也可以更改。您不应该希望在2015年使用A版本而不是W版本的函数。并不是每个代码页都有您想要使用的每个字符,将来在一些重大项目中,您可能不会只使用英语,实际上我们在互联网上交流的所有数据都是某种形式的Unicode。对于Windows程序,您必须采用宽字符串和UTF-16;微软还没有发明时间机器,它可以让我们直接在API中使用UTF-8。微软采用Unicode早于UTF-8。A函数不适用于UTF-8,因为UTF-8不是有效的进程代码页,它仅在字符集转换函数中有效
如果您真的希望尽可能避免使用UTF-16,则需要在每个API调用点在UTF-8和UTF-16之间进行转换。不过,不再建议对文本使用UTF-8或UTF-16。事实上,根据您上面的代码示例,VS2015摆脱了以前版本的TCHAR疯狂,这是一件非常棒的事情,直到今天,它仍然困扰着Windows程序员,尤其是在堆栈溢出方面。我知道Unicode非常重要,但我现在正在做一个简单的项目,所以写wchar_t*而不是char*和Lstring而不是stringNo没有意义,使用ANSI真的没有意义。在键盘上找到L键真的那么难吗?