Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

windows - Win32: How to custom draw an Edit control?

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

Example of cue banner in edit control

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

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

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.

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 (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)

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.

This is going to be fun. :rolleyes:

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


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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...