Does TextBox use GDI or GDI+

后端 未结 3 1996
旧时难觅i
旧时难觅i 2021-01-16 11:07

Basically, I want to measure the text size in a TextBox, and I found out that TextRenderer gives me the correct values, while Graphics would give me wrong values. So that se

相关标签:
3条回答
  • 2021-01-16 11:37

    All right, some more research have shown that there is a confusion of what is "Default".

    When these sources talk about "Default", they talk about "Default(1)" as in "If no extra code is executed", not "Default(2)" as in "If no extra action is taken".

    Because by "Default(2)", "Application.SetCompatibleTextRenderingDefault(false)" is added to the Program.cs file of a new proiject, which makes TextBox class use GDI for rendering, which is not the "Default(1)" for it, but in fact this is what happens if you, as a programmer, take no additional action.

    This is basically what got me.

    PS: I'm basing the assumtion that it's true by "Default(1)" on the accepted answer from this question UseCompatibleTextRendering property not created by designer when it is set to false

    0 讨论(0)
  • 2021-01-16 11:44

    The .NET Framework uses GDI+ in most places. The Graphics class is a wrapper around GDI+.

    However, WinForms also wraps native Win32 controls, which do not use GDI+. They use GDI. TextBox and RichTextBox are examples of this. They draw their text using GDI.

    To measure GDI+ text, you will use Graphics.MeasureString (note the Graphics class there). To measure GDI text, you will use TextRenderer.MeasureText (note how it is not provided by the Graphics class).

    Things do get slightly more complicated: some controls do use GDI+ for drawing purposes. The Button control is an example of this. Unless you have its FlatStyle set to System, it is owner-drawn by the .NET Framework, and that owner drawing is done using GDI+ (the graphics subsystem used by .NET). However, if you set the FlatStyle to System, the rendering is delegated to the native Win32 Button control, which uses GDI (like all native Win32 controls).

    My suggestion is to use GDI (TextRenderer) whenever possible to draw text. If you use a control that implements the FlatStyle property, set it to System. Text drawn with GDI+ just looks lousy by comparison. As a bonus, you'll get controls that actually look native and blend in with the system. FlatStyle.System is the only way to get a button control that actually pulses and glows. The WinForms renderer doesn't implement this.

    0 讨论(0)
  • 2021-01-16 11:50

    GDI+ error comes usually when we giving path for storing something in folder and that path is incorrect, meaning folder can not access or not able to find that path. So, just correct the path or allow folder security accessible.

    0 讨论(0)
提交回复
热议问题