MFC Dledit design belongs to its own edit box
2012-02-04 13:00 by troublemaker, 3543 Read, 5 reviews, Favorites, compilation
Cause
Accidentally saw the custom edit box control written by Daniel, so I found a time to write one, deepened the understanding of MFC message mechanism and some functions.
Analysis
First look at a QQ2011 login window, found that when the mouse hover over the edit box, there will be a hover border highlighting, so that the user a very beautiful visual enjoyment, but I believe that everyone on the Q every day, thus ignoring these beautiful, careful analysis, QQ every organ even a hair is carefully designed. When the edit box loses the input focus and the text in the edit box is empty, there will be a gray hint text prompting the user to enter.
Also can have a lot of custom edit box function, beautify our edit box.
Specific implementations and details
In the previous "MFC Auto-Hide" the technical details in this article is similar to Dledit, that is, the mouse hover judgment. When the mouse hover, dledit to paint the edit box "hover box bar", so when the "mouse is hovering state" judgment is very important, in order not to cause the article cumbersome, click here "MFC mouse to Stay". In the program there is a bool variable M_BHOVER to flag whether the mouse hovers over the control. So as long as M_bhover is true, you can paint a gorgeous "hover box bar" in Wm_erasebkgnd's handler function.
Yes, almost all of the drawings are in the WM_ERASEBKGND processing function, CEdit no ss_ownerdraw style, so there is no drawitem to draw on themselves, so use WM_ERASEBKGND's processing function to draw the background of the edit box , a large part of the custom beautification work falls on it.
In fact Dledit as a control, a lot of messages can not be accepted and processed, instead, they are sent to the main window (Parentwnd), so as to the main window to handle. Just like the Wm_drawitem message, the control window (assuming it has *s_ownerdraw style) will not accept it at all, and will not call the DrawItem function to draw it, it is sent to the main window, calling the main window of afx_msg void OnDrawItem ( int nidctl,lpdrawitemstruct lpdrawitemstruct) function, whose internal implementation paints the control based on the Nidctl control ID, and of course all controls can be drawn in OnDrawItem, but for many windows of the control, OnDrawItem will feel the pressure, so the drawitem of the control itself has the opportunity to exert. In the Win32 SDK Pure C programming, all the control's self-painting is implemented after "case Wm_drawitem:", but MFC in order to better combine object-oriented thinking, so the control window has its own DRAWITEM, and is called by OnDrawItem. Similarly, the WM_CTLCOLOR message is the same.
Careful people will find that if you add WM_CTLCOLOR or notification code processing messages to the control class (or subclass), in the message map, we will find:
On_control_reflect (En_change,&cdledit::onenchange)
On_wm_ctlcolor_reflect ()
At the end of the macro there are reflect words, that is, the meaning of the map. In the case of On_control_reflect, MFC is defined as follows:
#define ON_WM_CTLCOLOR_REFLECT () \
{wm_ctlcolor+wm_reflect_base, 0, 0, 0, AFXSIG_HDW, \
(afx_pmsg) (AFX_PMSGW) \
(static_cast< hbrush (afx_msg_call CWnd::*) (cdc*, UINT) > (&thisclass:: Ctlcolor))},
With this mapping, the WM_CTLCOLOR and Ctlcolor functions are mapped in the control class. Add the WM_CTLCOLOR message response function in the main window, with a very familiar sentence:
Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
This sentence is based on the child window (or child control, nCtlColor to specify) whether there is a ctlcolor, to decide whether to execute the ctlcolor of the child window, you can debug the next step. In addition, csdn a good article, the idea is very clear, "Wm_drawitem and DrawItem () discussion."
Take a look at the custom edit box:
Description
- Hover border: The border that is drawn when the mouse hovers;
- Default border: A solid frame outside the edit box, close to the hover border;
- Edit the Box bar: close to the frame bar of the edit box, generally choose tints as well.
In the Ctlcolor function of the edit box, Pdc->setbkcolor (CLR), the CLR refers to the background color of the text color, and the return value of the Ctlcolor function is the brush handle (return HBR) of the edit box, so If you cannot guarantee that the CLR and the returned brush are the same color, then the edit box will appear in two colors, above the background color of the text, and the following is the background color of the edit box, the two are different.
function diagram
See what's different from normal edit boxes:
The border box is different, followed by a gray font to prompt for input.
With the input focus, the background color changes.
When you have input focus, the font becomes thicker.
With the input focus, there is a shadow. In short, the above four edit boxes are more personalized than the two below.
About the edit box fillet feature
There are a lot of features that can be customized, such as custom background patterns and so on, to stimulate. If you have good suggestions, welcome to the brick discussion. Look at the online Daniel's code, there is a "round corner edit box" function, as long as the code in the Pdc->rectangle (RCT) to change Pdc->roundrect (..) You can do it.
Harvest and doubt
It's a little comforting to see that you can write something like that. With the guidance of the Daniel, I believe that a lot of things can be pro. I think, the most taboo to learn is detours, in the seemingly fashionable but bored technology wasted a lot of time, the basic course is very important, this is the invariable thing, learn to have the ability to contact the specific technology. There is a question, do not know how to spend time on such trivial technology, will not produce value, I will not waste time?
Attachment Download: Dledit Engineering
Trouble Boy February 4, 2012 Saturday
Source: http://www.cnblogs.com/daoluanxiaozi/archive/2012/02/04/2337016.html
MFC Dledit design belongs to its own edit box _ mouse hover