Source File
Buttons are one of the most commonly used controls and an important element in interface development to install and beautify interfaces. Realize button self-drawing, texture on the button will be a great appearance of the interface.
The button class I made implements the self-drawn bitmap button. Unlike the cbitmapbutton provided by MFC, this class implements the normal state, the state of the mouse passing, the state of the mouse pressing, and the state of the disabled, that is, the four statuses of buttons. We know that buttons in windows have 5 states, and the fifth State selected state that I have not implemented is actually of little use and will not affect the overall effect.
To put it simply, cwebmpbutton reloads the drawitem function, and all the drawing work is done here. However, to activate the drawitem function, you must set the bs_ownerdraw attribute of the value button. I wrote the bs_ownerdraw attribute setting statement in the presubclasswindow function to ensure the call of the drawitem function. Here, I can insert a sentence, because more self-painted items are made, and I find that it is best to plot them. Code Written in the onpaint function, which has weaker requirements on control attributes. However, the drawitem parameter lpdrawitemstruct contains a wealth of control information, which only needs to be parsed, and it takes more code to obtain the information in onpaint. Therefore, the two methods have their own advantages and disadvantages, the trade-off depends on the situation.
Knowing where to draw is only the first step, but this is also the most important step. As long as you know what functions are used to draw your own controls, and if we use images for self-painting, the code is actually only a texture, and the amount of code will be greatly reduced. However, I still need to take the button step to introduce the next job, that is, differentiate the status. The purpose of Self-painting is to make the control look good, and to make the control live. Therefore, it is important to draw different states of a space in different ways. In Windows, almost all the controls used are operated with the mouse, which gives them different states. Therefore, I set a variable m_nbuttonstate to record different button states and modify its values in onmousehover, onlbuttondown, and onlbuttonup to make the buttons alive.
Because the source file is provided Article Is not mentioned. Program . We will discuss and improve the Code together if you find any problems in the code or want to improve the code.