带有自定义边框的 WINAPI 编辑控件 [英] WINAPI Edit control with custom border

查看:25
本文介绍了带有自定义边框的 WINAPI 编辑控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在纯 WinAPI(无 MFC)中为 EDIT 控件实现自定义圆角边框的正确方法是什么?我需要这样的边框

what is the proper way of implementing custom rounded border for EDIT control in pure WinAPI (no MFC)? I need an edit with border like this:

我应该将编辑控件子类化并在 WM_NCPAINT 或类似的东西中进行自定义绘画吗?

Should I subclass edit control and do custom painting in WM_NCPAINT or something like that?

推荐答案

我猜你有两个选择:

  • 如您所说,您可以子类化并覆盖 WM_NCPAINT 等以提供您自己的非客户区
  • 或者,您可以简单地关闭编辑控件上的边框样式,让父窗口负责绘制框架.
  • As you said, you could sub-class and override WM_NCPAINT, etc to provide your own non-client area
  • Alternatively, you could simply turn off the border styles on the edit control and make the parent window responsible for drawing the frame.

使用选项 #1,您需要覆盖 WM_NCCALCSIZE 以使编辑控件的非客户区更大(即使客户区更小),然后 WM_NCPAINT 来呈现您的自定义框架.您可能还需要处理 WM_NCHITTEST.当然,您需要使控件本身在物理上更大,以解决额外的框架厚度.

With option #1, you would need to override WM_NCCALCSIZE to make the non-client area of the edit control larger (i.e. make the client area smaller), and then WM_NCPAINT to render your custom frame. You may also need to handle WM_NCHITTEST. And of course you'd need to make the control itself physically larger to account for the extra frame thickness.

这取决于您的应用程序设计以及您希望使用多少这样的控件,但如果是我,我会选择选项 #2.修改系统控件的标准绘图行为通常并不像您想象的那么容易,其中许多控件都附有数十年累积的杂项和兼容性修复程序.

It depends on your application design and how many controls like this you wish to use, but if it were me I would go with option #2. Modifying the standard drawing behaviour of system controls, many of which have decades of accumulated kludges and compatibility fixes attached to them, is often not as easy as you might expect.

如果您确保 WS_BORDERWS_EX_CLIENTEDGE 样式未在编辑控件上设置,则它将没有自己的可见边框.然后你所要做的就是拥有父窗口,在处理 WM_PAINT 时,在它周围绘制框架.确保在父窗口上设置 WS_CLIPCHILDREN 样式,以便您的自定义绘图不会覆盖编辑控件.

If you make sure the WS_BORDER and WS_EX_CLIENTEDGE styles aren't set on the edit control, it will have no visible border of its own. Then all you have to do is have the parent window, when processing WM_PAINT, draw the frame around it. Make sure you set the WS_CLIPCHILDREN style on the parent window so that your custom drawing doesn't overwrite the edit control.

不管怎样,最后一条路都可能行得通,所以这取决于你走哪条路.

Either path would probably work in the end though so it's up to you which way you go.

这篇关于带有自定义边框的 WINAPI 编辑控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆