From: http://www.cnblogs.com/walzer/archive/2007/04/08/704996.html
Unit conversion between dlu and pixel
I thought it was over after the button effect was completed. As a result, this evening I encountered a very annoying problem. The XX * XX unit shown in the lower right corner of the IDE is dlu (dialog unit ), this is changed based on the font size of the dialog box you set. This practice is understandable. If the font is changed to a larger value, the dialog and button are naturally "supported", which is more flexible. However, the image I added is calculated by pixel. Finally, there are two methods. According to international practice, it is of course the first stupid method, as is the style on msdn.
(1) Method 1: I checked the Conversion Relationship Between dlu and pixel, there is a more comprehensive web page is http://support.microsoft.com/default.aspx? SCID = KB; en-US; 145994 (how
To caculate Dialog Box units based on the current font in Visual C ++) according to the method two described in this article, a simple test is performed.
1 bool callback dialogproc (hwnd hdlg, uint umsg, wparam, lparam)
2 {
3 Switch (umsg)
4 {
5 case wm_initdialog:
6 {
7 rect rc = {0, 0, 4, 8 };
8
9 mapdialogrect (hdlg, & rc );
10 printf ("baseunitx = % LD \ n", RC. Right );
11 printf ("baseunity = % LD \ n", RC. Bottom );
12
13 // dlu_weight = pixel_weight * 4/baseunitx;
14 // dlu_height = pixel_height * 8/baseunity;
15
16 return true;
17}
18}
19
20 return false;
21}
For the Courier New 10pt font set in my dialog,
Step 1: Obtain baseunitx = 7, baseunity = 16 from the code,
Step 2: Apply the formula
Weight_dlu = weight_pixel * 4/baseunitx
Height_dlu = height_pixel * 8/baseunity
For example, if I want to create a 320*240 pixel dialog box
Weight_dlu = 320*4/7 = 182.86, rounded to 183
Height_dlu = 240*8/16 = 120
Step 3: Drag the border of the dialog box to VC and draw the 183*120 dialog box. After the code runs, run getclientrect to check whether the 320*240 pixel dialog box is displayed.
However, this method has a fatal disadvantage.. For example, if I want to draw a 60*70 button and convert it to 34.28*35 According to baseunitx and baseunity above, but when I draw 34*35 dlu, run to get 59*70 pixel windows; when 35*35 dlu is drawn, run to get 61*70 pixel windows, which cannot be exactly the right, resulting in a 60*70 pixel image stacked up, the button edge may contain discontinuous black spots. Therefore, this text can only be used as the Conversion Relationship Between dlu and pixel, without any practical application value.
(2) Method 2: the correct method
In fact, it is also very simple. After dinner, I suddenly thought of using setwindowpos to forcibly set the length of the window and the coordinates in the upper left corner. I tried it in wm_drawitem message processing:
Setwindowpos (lpdis-> hwnditem, null, 10, 10, 60, 70, swp_nozorder );
The button is 59*70 before the setting, and the button is 60*70 after the setting, and there is no problem in adding images. OK. The entire GWES-based UI solution has been developed. There seems to be no major technical barriers behind.
So far, I have not found any obstacles in the GWES solution. I can use this solution on the board. Compared with writing the UI with only GDI, the workload of software development has been reduced by about 30%. Of course, this process has not ended yet. This plan has a profound significance for my project team. Please refer to the following article: a small step in the code and a huge step in Project Progress Management.
Keywords: VC ++, control size unit, control Size expressed in pixels, unit conversion of dlu and pixel