In the past, the script engine was used to copy a self-painted native C ++ GUI framework and switch the GDI or Direct2D rendering mode. Because the advanced automatic layout function of WPF is copied, you must know how to measure the text size. It is troublesome to use Direct2D to measure the text size. It is not like using a direct function in GDI, And no one directly gives the result when searching in both Chinese and English, some people wrote the text "this kind of thing does not seem to be done" on their blogs. However, after traversing MSDN, I still found a method to save the country through the curve, and directly go to the Code:
First, create IDWriteTextFormat:
1 IDWriteFactory * dwriteFactory = GetDirectWriteFactory ();
2 IDWriteTextFormat * format = 0;
3 HRESULT hr = dwriteFactory-> CreateTextFormat (
4 fontProperties. fontFamily. Buffer (),
5 NULL,
6 (fontProperties. bold? DWRITE_FONT_WEIGHT_BOLD: DWRITE_FONT_WEIGHT_NORMAL ),
7 (fontProperties. italic? DWRITE_FONT_STYLE_ITALIC: DWRITE_FONT_STYLE_NORMAL ),
8 DWRITE_FONT_STRETCH_NORMAL,
9 (FLOAT) fontProperties. size,
10 L "",
11 & format );
12 if (! FAILED (hr ))
13 {
14 format-> SetWordWrapping (DWRITE_WORD_WRAPPING_NO_WRAP );
15 return format;
16}
17 else
18 {
19 return 0;
20}
FontProperties is a custom structure, so you don't have to worry about it. Refer to MSDN to learn how to use CreateTextFormat. FontProperties. fontFamily indicates the font name. Then IDWriteTextFormat plays the role of GDI's "font object", which can be drawn using ID2D1RenderTarget. In addition to using IDWriteTextFormat as the font, IDWriteTextLayout can also be used as the "more complex font for adding redundant information" for ID2D1RenderTarget ". The key to measuring text is here.
Next we use IDWriteTextFormat to create IDWriteTextLayout:
1 IDWriteTextLayout * textLayout = 0;
2 HRESULT hr = GetDirectWriteFactory ()-> CreateTextLayout (
3 oldText. Buffer (),
4 oldText. Length (),
5 textFormat,
6 0,
7 0,
8 & textLayout );
9 if (! FAILED (hr ))
10 {
11 DWRITE_TEXT_METRICS metrics;
12 hr = textLayout-> GetMetrics (& metrics );
13 if (! FAILED (hr ))
14 {
15 minSize = Size (int) ceil (metrics. widthIncludingTrailingWhitespace), (int) ceil (metrics. height ));
16}
17 textLayout-> Release ();
18 return;
19}
Here, we can see how to measure the font size of the text.
Put a temporary image here. I copied the WPF method to construct the GUI directly from the layout and drawing elements, so I demonstrated how to use these things to create a Win7 button (with animation) open Vczh Library ++ 3.0, download the code, and open Candidate \ GUI \ GuiDemo. sln, press F5 to see the effect:
This is the result of Direct2D rendering (I specified the absolute path of the DXSDK in the project file, and it can be compiled if you install it differently ). The button is basically the same as that of win7, but Direct2D is used for rendering. When the button size changes, the complex border and the two gradient and text inside can be automatically aligned-but this is not hard code, the "layout function" of the GUI can be configured as this. Like the two buttons, they are always in the lower right corner and are also provided by the "layout function. This is similar to the TableLayoutPanel of C.
Now the button is ready-the template can be changed like WPF, but since no one needs to change the template dynamically, I wrote the template in the constructor, therefore, skin replacement becomes quite simple-as long as you use the layout function to piece together the elements into a complex image, then implement the "template interface" defined by each control to respond to the message of the visual control, and built-in animation support (this can be observed only when running ).
For convenience, in addition to Debug and Release, I added DebugDirect2D and ReleaseDirect2D configurations in the project. You can switch between them and watch the demo freely.
From: λ-calculus