更改默认代码Visual Studio 2015社区

时间:2015-11-25 23:27:44

标签: 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 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

2 个答案:

答案 0 :(得分:4)

您不应该在2015年使用A版本而不是W版本的功能。并非每个代码页都包含您要使用的每个字符,您可能最终不会使用未来在一些重大项目中的英语,我们在互联网上传达的几乎所有数据都是某种形式的Unicode。对于Windows程序,您必须使用宽字符串和UTF-16;微软还没有发明时间机器让我们直接在API中使用UTF-8。 (Microsoft采用Unicode早于UTF-8。)A函数不适用于UTF-8,因为UTF-8不是有效的进程代码页(它仅在字符集转换函数中有效)。 / p>

如果您真的想尽可能避免使用UTF-16,则需要在每个API调用点之间在UTF-8和UTF-16之间进行转换。但是,不再使用UTF-8或UTF-16作为文本。事实上,根据你上面的代码示例,VS2015放弃了先前版本的TCHAR疯狂,这使得开始的Windows程序员(尤其是这里的Stack Overflow)继续混淆,这是一件好事。这一天。

答案 1 :(得分:3)

来自https://msdn.microsoft.com/en-us/library/ms185319.aspx

  

找到包含该模板的.zip文件。默认情况下,此文件位于
  \ My Documents \ Visual Studio Version \ My Exported   模板\

     

提取.zip文件。

     

修改或删除当前模板文件,或将新文件添加到   模板。

     

打开,修改和保存.vstemplate XML文件以处理更新   行为或新文件。有关.vstemplate的更多信息   schema,请参阅Visual Studio模板架构参考。更多   有关您可以在源文件中参数化的信息,请参阅   模板参数

     

选择模板中的文件,右键单击,单击“发送到”,然后单击“发送到”   然后单击压缩(zipped)文件夹。您选择的文件是   压缩成.zip文件。

     

将新的.zip文件放在与旧的.zip文件相同的目录中。

     

删除提取的模板文件和旧模板.zip文件。

     

启动(以管理员身份)开发人员命令提示符的实例   (在开始菜单上,在Visual Studio 2010 / Visual Studio Tools /下   Developer Command Prompt)。

     

运行以下命令:devenv / installvstemplates。

其他提到的事情也可以改变。

相关问题