0

我想创建一个带有圆形边缘的编辑框。我不能使用标准编辑框控件,因为它会绘制矩形编辑框。

任何建议或指示如何做到这一点?

4

1 回答 1

3

你有几个选择: -

  1. 创建一个父控件并在其中有一个子无边框文本编辑控件
  2. 子类一个文本编辑控件并重载 NC_PAINT 以绘制边框

更新

在弄乱了一个示例程序之后,我逐渐意识到子类编辑控件并没有真正起作用,并且在线检查证实了这一点(感谢 MS 如此一致!)。

所以只剩下选项 1 和选项 3(只是想到它):

  1. 创建自己的编辑控件

以下是方法 1 的示例:

#include <windows.h>

LRESULT __stdcall EditboxProc (HWND window, unsigned message, WPARAM w_param, LPARAM l_param)
{
  bool
    use_default = true;

  LRESULT
    result = 0;

  switch (message)
  {
  case WM_CREATE:
    {
      RECT
        client;

      GetClientRect (window, &client);

      HWND
        edit = CreateWindowEx (0,
                               TEXT ("Edit"),
                               0,
                               WS_VISIBLE | WS_CHILD,
                               10,
                               10,
                               client.right - 20,
                               client.bottom - 20,
                               window,
                               0,
                               GetModuleHandle (0),
                               0);

      SetWindowLongPtr (window, GWLP_USERDATA, static_cast <LONG> (reinterpret_cast <LONG_PTR> (edit)));
    }
    break;

  case WM_SIZE:
    {
      RECT
        client;

      GetClientRect (window, &client);
      SetWindowPos (reinterpret_cast <HWND> (static_cast <LONG_PTR> (GetWindowLongPtr (window, GWLP_USERDATA))), 0, 10, 10, client.right - 20, client.bottom - 20, SWP_NOZORDER | SWP_NOOWNERZORDER);

      use_default = false;
    }
    break;
  }

  return use_default ? DefWindowProc (window, message, w_param, l_param) : result;
}

LRESULT __stdcall WindowProc (HWND window, unsigned message, WPARAM w_param, LPARAM l_param)
{
  bool
    use_default = true;

  LRESULT
    result = 0;

  switch (message)
  {
  case WM_CREATE:
    {
      RECT
        client;

      GetClientRect (window, &client);

      CreateWindowEx (0,
                      TEXT ("EditboxClass"),
                      0,
                      WS_VISIBLE | WS_CHILD,
                      client.right / 4,
                      client.bottom / 2 - 20,
                      client.right / 2,
                      40,
                      window,
                      0,
                      GetModuleHandle (0),
                      0);
    }
    break;
  }

  return use_default ? DefWindowProc (window, message, w_param, l_param) : result;
}

int __stdcall WinMain (HINSTANCE instance, HINSTANCE prev_instance, LPSTR command_line, int show)
{
  WNDCLASSEX
    window_class = 
    {
      sizeof window_class,
      0,
      WindowProc,
      0,
      0,
      instance,
      0,
      0,
      reinterpret_cast <HBRUSH> (static_cast <int> (COLOR_WINDOW) + 1),
      0,
      TEXT ("WindowClass"),
      0
    },
    editbox_class = 
    {
      sizeof editbox_class,
      0,
      EditboxProc,
      0,
      0,
      instance,
      0,
      0,
      reinterpret_cast <HBRUSH> (static_cast <int> (COLOR_BTNFACE) + 1),
      0,
      TEXT ("EditboxClass"),
      0
    }; 

  if (RegisterClassEx (&window_class) && RegisterClassEx (&editbox_class))
  {
    HWND
      window = CreateWindowEx (0,
                               TEXT ("WindowClass"),
                               TEXT ("Demo"),
                               WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               0,
                               0,
                               instance,
                               0);

    if (window)
    {
      MSG
        message;

      bool
        quit = false;

      while (!quit)
      {
        switch (GetMessage (&message, window, 0, 0))
        {
        case -1:
        case 0:
          quit = true;
          break;

        default:
          TranslateMessage (&message);
          DispatchMessage (&message);
          break;
        }
      }
    }
  }

  return 0;
}

这有点简单,但应该让你知道如何做你想做的事。它不适用于使用资源脚本定义的对话框(我认为,自从我手动编码资源以来已经有一段时间了),因为它对编辑框的父级(渲染框架的东西)使用了自定义控件。您可能希望将处理程序添加到 EditboxProc 以处理传入和传出编辑控件的消息(如 WM_NOTIFY、WM_COMMAND、WM_GETTEXT 等)并渲染圆角(WM_PAINT、WM_ERASEBKGND)。

于 2012-05-14T08:34:14.883 回答