如何在 C++ Win32 应用程序中创建 Windows 风格的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7598067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:12:25  来源:igfitidea点击:

How to create a Windows-style textbox in a C++ Win32 application

c++winapi

提问by EFanZh

I tried this:

我试过这个:

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

I use:

我用:

CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL); 

to create a textbox, and it's just an ugly black-solid-border textbox.

创建一个文本框,它只是一个丑陋的黑色实心边框文本框。

How to create a windows style textbox (with a 3D light blue border)?

如何创建 Windows 样式的文本框(带有 3D 浅蓝色边框)?

回答by Thierry Franzetti

Instead of using CreateWindow, use CreateWindowExand specify WS_EX_CLIENTEDGEas the first parameter.

代替 using CreateWindow,使用CreateWindowEx并指定WS_EX_CLIENTEDGE为第一个参数。

You can compare the styles of your created edit control with a stock one (for example, when you show 'Properties' on a file in the explorer) with the Spy++ tool that comes along Visual Studio.

您可以使用 Visual Studio 附带的 Spy++ 工具将您创建的编辑控件的样式与常规控件的样式(例如,当您在资源管理器中的文件上显示“属性”时)进行比较。

回答by MD XF

The OP edited his question, removing the original code and therefore invalidating the answers, saying:

OP 编辑​​了他的问题,删除了原始代码,因此使答案无效,他说:

This is a simple example to create a window with a textbox.

这是创建带有文本框的窗口的简单示例。

I rolled it back to restore the original code, but I thought the working example was nice, so here it is:

我回滚它以恢复原始代码,但我认为工作示例很好,所以这里是:

#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 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"),
                               WS_CHILD | WS_VISIBLE, 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;
}

回答by Aliarth

In Codeblocks put manifest file near project: program_name.exe.manifest

在代码块中将清单文件放在项目附近:program_name.exe.manifest

screenshot

截屏

And to program_name.exe.manifest write this:

并在 program_name.exe.manifest 中写入:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="CompanyName.ProductName.YourApplication"
type="win32"
/>
<description>Program name</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="*"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>

And then your program look like this:

然后你的程序看起来像这样:

Screenshot

截屏