Win32:如何自定义绘制一个Edit控件? [英] Win32: How to custom draw an Edit control?

查看:752
本文介绍了Win32:如何自定义绘制一个Edit控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现 EM_SETCUEBANNER 的功能,文本提示出现在编辑"控件中:

i need to implement the functionality of EM_SETCUEBANNER, where a text hint appears inside an Edit control:

问题是我不能使用通用控件的版本6,这是获得Microsoft提供的提示横幅实现所必需的.

The catch is that i cannot use version 6 of the Common Controls, which is what is required to get the Microsoft supplied implementation of a cue banner.

我研究了简单地将编辑控件的文本和字体格式更改为

i've looked into simply changing the text of the edit control, and the font format to

Dark Gray Italic Text

但它会引发 Change 事件(提供了组件包装器通过高级组件库),我找不到避免的方法.

but it will throw Change events (component wrapper provided by higher component library) that i can't find a way to avoid.

因此,我改为自定义绘制文本,在控件未聚焦且为空时绘制Cue Banner文本,否则将依赖默认绘制.

So i was instead going to custom draw the text, drawing the Cue Banner text when the control is unfocused and empty, and rely on default painting otherwise.

编辑"控件不能很好地公开自定义绘制机制,如ListView, ,TreeView和其他提供的功能.

The Edit control doesn't nicely expose a custom drawing mechanism, like ListView, TreeView and others provide.

其他人对此进行了调查,但这似乎是几乎不可能完成的任务:

Other people have looked into it, but it seems to be an nearly impossible task:

从外观上看,我会 必须处理以下内容 消息:

From the way things are looking, I'll have to handle the following messages:

  • WM_ERASEBKGND,WM_PAINT (出于明显的原因)
  • WM_SETFOCUS,WM_KILLFOCUS (以防止 白条显示- 如上所述)
  • WM_CHAR (以处理和更新 控件中的文字)
  • WM_ERASEBKGND, WM_PAINT (for obvious reasons)
  • WM_SETFOCUS, WM_KILLFOCUS (to prevent the white bar from displaying -- described above)
  • WM_CHAR (to process and update the text in the control)

我还需要找到一种方法 在控件中显示插入符号, 因为我还没有找到允许的方法 Windows可以帮我做到这一点 画我提到的白条.

And I also need to find a way to display the caret in the control, since I haven't found a way to allow Windows to do that for me without also painting the white bar I mentioned.

这将很有趣. :rolleyes:

This is going to be fun. :rolleyes:

鉴于Windows Edit控件绝不是要自定义绘制的:有人知道如何自定义绘制Windows Edit控件吗?

Given that the Windows Edit control was never meant to be custom drawn: does anyone know how to custom draw a Windows Edit control?

注意:我还将接受能解决我的问题的答案,而不是回答我的问题.但是其他想要自定义绘制Edit控件的人,可能会遇到一个问题.

Note: i will also accept answers that solve my problem, rather than answering my question. But anyone else wanting to custom draw an Edit control, coming across this question, would probably like an answer.

推荐答案

自定义绘制Edit控件实际上是不可能的.在一些特殊情况下,您所做的事情很少,可以解决,但是您可能会在下一版Windows中(或有人在较旧版本上运行应用程序或通过终端服务运行时)严重损坏.

Custom drawing an Edit control is essentially impossible. There are a few specialized cases were you are doing so little that can get away with it, but you risk breaking badly in the next revision of windows (or when someone runs your app on an older version, or via terminal services, etc).

仅接管WM_PAINT和WM_ERASEBKGROUND还不够好,因为该控件有时还会在其他消息上绘制.

Just taking over WM_PAINT and WM_ERASEBKGROUND aren't good enough, because the control will sometimes paint on other messages as well.

最好只编写自己的编辑控件.我知道这是一项艰巨的工作,但从长远来看,这比尝试破解接管所有编辑"控件的绘图代码的工作要少.

You are better off just writing your own edit control. I know that's a huge amount of work, but in the long run it will be less work than trying to hack your way into taking over all of the Edit controls' drawing code.

我回想起过去的美好时光,当时每个人都使用按钮控件的子类来添加颜色和图形等.问题是,有一天,我坐下来编写了自己的按钮窗口类.它比在源代码树中用于子类化和自定义绘制Windows按钮的代码少.

I remember back in the good old days when everyone use to subclass the button control to add color and graphics, etc. The thing is, one day I sat down and just wrote my own button window class. and it was LESS CODE than what we had in our source tree to subclass and custom draw the Windows button.

这篇关于Win32:如何自定义绘制一个Edit控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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