必须进行哪些更改才能使 2011 年的 Windows 文本框在 2019 年正常运行?
What changes have to be done to make this Windows textbox from 2011, work now, in 2019?
要使 2011 年的 Windows 文本框在 2019 年可以正常工作,需要进行哪些更改?
我尝试编译 this question from 2011 中有关制作 C++ 文本框的代码...
#include <windows.h>
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow)
{
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = windowClass;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWnd;
if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
{
MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWndEdit = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return FALSE;
}
但是在 Visual Studio 2019 年尝试构建它时,我收到有关无法将 "t_char" 转换为 "LPTSTR" 的错误。
那么,我该如何更新代码才能正常工作?是否可以不包含任何其他文件?
使用LPCTSTR
代替LPTSTR
:
LPCTSTR windowClass = TEXT("WinApp");
LPCTSTR windowTitle = TEXT("Windows Application");
LPTSTR
是 TCHAR *
LPCTSTR
是 const TCHAR *
TEXT("literal")
生成 const TCHAR []
.
字符串文字是常量数据。从 C++11 开始,您不能再将字符串文字分配给 pointer-to-non-const.
完全退出 TCHAR 业务。这些 TEXT 宏用于 cross-compiling,代码意在 Windows 9x 上的 运行。
更改这些行:
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
为此:
LPCWSTR windowClass = L"WinApp";
LPCWSTR windowTitle = L"Windows Application";
然后是 TEXT 宏的所有后续用法,如下所示:
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
只是宽字符串:
MessageBox(NULL, L"RegisterClassEx Failed!", L"Error", MB_ICONERROR);
要使 2011 年的 Windows 文本框在 2019 年可以正常工作,需要进行哪些更改?
我尝试编译 this question from 2011 中有关制作 C++ 文本框的代码...
#include <windows.h>
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow)
{
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = windowClass;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWnd;
if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
{
MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWndEdit = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return FALSE;
}
但是在 Visual Studio 2019 年尝试构建它时,我收到有关无法将 "t_char" 转换为 "LPTSTR" 的错误。
那么,我该如何更新代码才能正常工作?是否可以不包含任何其他文件?
使用LPCTSTR
代替LPTSTR
:
LPCTSTR windowClass = TEXT("WinApp");
LPCTSTR windowTitle = TEXT("Windows Application");
LPTSTR
是 TCHAR *
LPCTSTR
是 const TCHAR *
TEXT("literal")
生成 const TCHAR []
.
字符串文字是常量数据。从 C++11 开始,您不能再将字符串文字分配给 pointer-to-non-const.
完全退出 TCHAR 业务。这些 TEXT 宏用于 cross-compiling,代码意在 Windows 9x 上的 运行。
更改这些行:
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
为此:
LPCWSTR windowClass = L"WinApp";
LPCWSTR windowTitle = L"Windows Application";
然后是 TEXT 宏的所有后续用法,如下所示:
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
只是宽字符串:
MessageBox(NULL, L"RegisterClassEx Failed!", L"Error", MB_ICONERROR);