简体   繁体   English

更改默认代码Visual Studio 2015社区

[英]Change default code Visual Studio 2015 Community

How can I change the code that VS automatically generates for a Win32 Project? 如何更改VS为Win32项目自动生成的代码? Instead of this: 代替这个:

// 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;
}

I want it to auto generate some other code. 我希望它自动生成其他代码。 How could I do this? 我该怎么办?

Edit: Also, how do I get VS to not generate precompiled headers for globally (I know how to change it for every project, but that is annoying). 编辑:另外,如何使VS不为全局生成预编译的标头(我知道如何为每个项目更改它,但这很烦人)。 And how do I get it to use the A versions of the Win32 by default and not the W ? 以及如何在默认情况下使用Win32的A版本而不是W

You should not want to use the A versions instead of W versions of functions in 2015. Not every code page has every character you'll want to use, you may wind up not using solely English in the future in some major project, and virtually all the data we communicate on the Internet is in some form of Unicode. 在2015年,您不应该使用A版本而不是W版本的函数。不是每个代码页都包含您要使用的每个字符,将来您可能在某些重大项目中最终不仅仅使用英语,而且实际上我们在Internet上交流的所有数据都是采用Unicode形式的。 For Windows programs, you will have to embrace wide strings and UTF-16; 对于Windows程序,您将必须包含宽字符串和UTF-16。 Microsoft has yet to invent the time machine that will let us use UTF-8 directly in APIs. Microsoft尚未发明可以让我们直接在API中使用UTF-8的时间机器。 (Microsoft's adoption of Unicode predated UTF-8.) The A functions do not work with UTF-8 because UTF-8 is not a valid process code page (it is only valid in the character set conversion functions). (Microsoft采用Unicode之前的UTF-8。) A函数不适用于UTF-8,因为UTF-8不是有效的过程代码页(仅在字符集转换函数中有效)。

If you really want to avoid using UTF-16 as much as possible, you will need to convert between UTF-8 and UTF-16 at every API call point. 如果您确实想尽可能避免使用UTF-16,则需要在每个API调用点之间在UTF-8和UTF-16之间进行转换。 Not using either UTF-8 or UTF-16 for text, though, is no longer recommended. 但是,不再建议不要对文本使用UTF-8或UTF-16。 In fact, it's a great thing that VS2015, according to your code sample above, dropped the TCHAR madness of prior versions that continues to confuse beginning Windows programmers ( especially here on Stack Overflow) to this day. 实际上,根据您上面的代码示例,VS2015放弃了以前版本的TCHAR疯狂,这一直令人困扰初学者Windows程序员( 尤其是此处的Stack Overflow),这一件好事。

From https://msdn.microsoft.com/en-us/library/ms185319.aspx : https://msdn.microsoft.com/zh-cn/library/ms185319.aspx

Locate the .zip file that contains the template. 找到包含模板的.zip文件。 By default, this file is located in 默认情况下,此文件位于
\\My Documents\\Visual Studio Version\\My Exported Templates\\ \\我的文档\\ Visual Studio版本\\我的导出模板\\

Extract the .zip file. 解压缩.zip文件。

Modify or delete the current template files, or add new files to the template. 修改或删除当前模板文件,或将新文件添加到模板。

Open, modify, and save the .vstemplate XML file to handle updated behavior or new files. 打开,修改和保存.vstemplate XML文件,以处理更新的行为或新文件。 For more information about the .vstemplate schema, see Visual Studio Template Schema Reference. 有关.vstemplate架构的更多信息,请参见Visual Studio模板架构参考。 For more information about what you can parameterize in the source files, see Template Parameters 有关可以在源文件中参数化的内容的更多信息,请参见模板参数。

Select the files in your template, right-click, click Send To, and then click Compressed (zipped) Folder. 选择模板中的文件,右键单击,单击“发送到”,然后单击“压缩(压缩)文件夹”。 The files that you selected are compressed into a .zip file. 您选择的文件被压缩为.zip文件。

Put the new .zip file in the same directory as the old .zip file. 将新的.zip文件与旧的.zip文件放在同一目录中。

Delete the extracted template files and the old template .zip file. 删除提取的模板文件和旧的模板.zip文件。

Start (as administrator) an instance of the Developer Command Prompt (on the start menu, under Visual Studio 2010 / Visual Studio Tools / Developer Command Prompt). (以管理员身份)启动Developer Command Prompt实例(在开始菜单上,在Visual Studio 2010 / Visual Studio Tools / Developer Command Prompt下)。

Run the following command: devenv /installvstemplates. 运行以下命令:devenv / installvstemplates。

The other mentioned things can be changed too. 其他提到的事情也可以更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM