Why doesn't TextRenderer.MeasureText work properly? - c#

I want to measure the height of the text given a certain width of available canvas. The text that I pass in is really long and I know will wrap. To that end, I call the following:
using System.Windows.Forms;
...
string text = "Really really long text that is sure to wrap...";
Font font = new Font("Arial", 14);
Size canvas = new Size(1100, 850);
Size size = TextRenderer.MeasureText(text, font, canvas);
No matter what I pass in for canvas, it always returns 14 for size.Height.
Am I missing something simple?

Please, use the TextFormatFlags measure parameter as shown below:
Size size = TextRenderer.MeasureText(text, font, canvas, TextFormatFlags.WordBreak);

DimitryG's solution seems to work great, but only when there is no word big enough to fill more than an entire row. If such word exists, the width will be bigger than the proposed width. There is the flag TextFormatFlags.EndEllipsis for this case, however I didn't manage to combine the flags in a way so the output is correct (if I use TextFormatFlags.WordEllipsis | TextFormatFlags.WordBreak the width is correct, but the height is not updated when Word Ellipsis takes place, which means that the big word will be trimmed, but the height will be the same as if it's not trimmed). I also tried the flag TextFormatFlags.EndEllipsis but with no results.
So until someone makes this clear, I propose using a TextBox for word wrap, and then multiply the number of lines in the TextBox with the Font's height.
Code:
int MeasureMultilineTextHeigh(string text, Font font, int proposedWidth)
{
// Exception handling.
TextBox textBox = new TextBox()
{
Multiline = true,
BorderStyle = BorderStyle.None,
Width = proposedWidth,
Font = font,
Text = text,
};
int lineCount = textBox.GetLineFromCharIndex(int.MaxValue) + 1;
int fontHeight = TextRenderer.MeasureText("X", font).Height;
return lineCount * fontHeight;
}
However this approach has one problem: If, and only if Multiline property of a TextBox is set to true, every Font will have its own left and right padding. See this stackoverflow.com question and this social.msdn.microsoft.com question for more details. So this means that in some situations the returned value might be bigger than expected. To solve this problem you can use the SetPadding function to remove the padding (you can find the method as an answer in the first question), code:
private const int EM_SETRECT = 0xB3;
[DllImport(#"User32.dll", EntryPoint = #"SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public readonly int Left;
public readonly int Top;
public readonly int Right;
public readonly int Bottom;
private RECT(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }
}
public void SetPadding(TextBox textBox, Padding padding)
{
var rect = new Rectangle(padding.Left, padding.Top, textBox.ClientSize.Width - padding.Left - padding.Right, textBox.ClientSize.Height - padding.Top - padding.Bottom);
RECT rc = new RECT(rect);
SendMessageRefRect(textBox.Handle, EM_SETRECT, 0, ref rc);
}
int MeasureMultilineTextHeigh(string text, Font font, int proposedWidth)
{
// Exception handling.
TextBox textBox = new TextBox()
{
Multiline = true,
BorderStyle = BorderStyle.None,
Width = proposedWidth,
Font = font,
};
SetPadding(textBox, Padding.Empty);
textBox.Text = text;
int lineCount = textBox.GetLineFromCharIndex(int.MaxValue) + 1;
int fontHeight = TextRenderer.MeasureText("X", font).Height;
return lineCount * fontHeight;
}
Needed using statements:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
I hope this helps. Sorry for my English, if I made any mistake.

Related

How to align Combobox values on center, without spaces? [duplicate]

I want to align my text in combo box so that it will show in the center of combobox tell me how to do this also you can see there is a default border around a combo box when it is in focus how can i remove that border also
Kindly solve my two problems
Thanks
This article will help you: http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/
The trick is to set the DrawMode-Property of the ComboBox to OwnerDrawFixed as well as subscribe to its event DrawItem.
Your event should contain the following code:
// Allow Combo Box to center aligned
private void cbxDesign_DrawItem(object sender, DrawItemEventArgs e)
{
// By using Sender, one method could handle multiple ComboBoxes
ComboBox cbx = sender as ComboBox;
if (cbx != null)
{
// Always draw the background
e.DrawBackground();
// Drawing one of the items?
if (e.Index >= 0)
{
// Set the string alignment. Choices are Center, Near and Far
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
// Set the Brush to ComboBox ForeColor to maintain any ComboBox color settings
// Assumes Brush is solid
Brush brush = new SolidBrush(cbx.ForeColor);
// If drawing highlighted selection, change brush
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
brush = SystemBrushes.HighlightText;
// Draw the string
e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
}
}
}
To right align the items you can simply replace StringAlignment.Center with StringAlignment.Far.
This isn't supported for ComboBox. The exact reasons are lost in the fog of time, ComboBox has been around since the early nineties, but surely has something to do with the awkwardness of getting the text in the textbox portion to line up with the text in the dropdown. Custom drawing with DrawItem cannot solve it either, that only affects the appearance of the dropdown items.
As a possible workaround, you could perhaps do something outlandish like padding the item strings with spaces so they look centered. You'll need TextRenderer.MeasureText() to figure out how many spaces to add for each item.
The "border" you are talking about is not a border, it is the focus rectangle. You can't get rid of that either, Windows refuses to let you create a UI that won't show the control with the focus. Users that prefer the keyboard over the mouse care about that. No workaround for that one.
Set RightToLeft property to true.
It does NOT reverse the sequence of characters. It only right-justifies.
The post is a bit old but it may be still worth to say:
both requirements are possible for Windows Forms ComboBox:
Text align center (text area and the dropdown)
For the text area, find the Edit control and set the ES_CENTER style for the control.
For the dropdown items or the selected item in drop-down mode, to align text to center, just make the control owner-drawn and draw the text at center.
Get rid of focus rectangle
Make the control owner-drawn and just don't draw focus rectangle.
Example
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyComboBox : ComboBox
{
public MyComboBox()
{
DrawMode = DrawMode.OwnerDrawFixed;
}
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
const int GWL_STYLE = -16;
const int ES_LEFT = 0x0000;
const int ES_CENTER = 0x0001;
const int ES_RIGHT = 0x0002;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public int Width { get { return Right - Left; } }
public int Height { get { return Bottom - Top; } }
}
[DllImport("user32.dll")]
public static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);
[StructLayout(LayoutKind.Sequential)]
public struct COMBOBOXINFO
{
public int cbSize;
public RECT rcItem;
public RECT rcButton;
public int stateButton;
public IntPtr hwndCombo;
public IntPtr hwndEdit;
public IntPtr hwndList;
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
SetupEdit();
}
private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
private void SetupEdit()
{
var info = new COMBOBOXINFO();
info.cbSize = Marshal.SizeOf(info);
GetComboBoxInfo(this.Handle, ref info);
var style = GetWindowLong(info.hwndEdit, GWL_STYLE);
style |= 1;
SetWindowLong(info.hwndEdit, GWL_STYLE, style);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
e.DrawBackground();
var txt = "";
if (e.Index >= 0)
txt = GetItemText(Items[e.Index]);
TextRenderer.DrawText(e.Graphics, txt, Font, e.Bounds,
ForeColor, TextFormatFlags.Left | TextFormatFlags.HorizontalCenter);
}
}
you can do something like this by adding space before Display member in your Query
for example :
combobox1.DataSource = Query(Select col1 , (' '+col2) as Col2 from tableName)
combobox1.DisplayMember = "Col2";
combobox1.ValueMember = "col1";

WINUI 3.0 - Reunion 0.5 window size///

I just start learning WinUI 3.0 and can't find any information in google or books like Learn WinUI 3.0 how to set default window size of application. I know in UWP it can be like
ApplicationView.PreferredLaunchViewSize = new Size(480, 800);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
But actually it doesn't work in WinUI
No need to do these interop calls on your own or use third-party packages for this.
Try this trifecta:
// Use 'this' rather than 'window' as variable if this is about the current window.
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
Then you can finally set the size with:
appWindow.Resize(new Windows.Graphics.SizeInt32 { Width = 480, Height = 800 });
Note that an AppWindow object has several other functions as well, like
MoveAndResize, Show, Hide, and features to modify the title bar.
Take a look at this repository dotMorten/WinUIEx.
It contains a method to set the window size and position
myWindow.SetWindowPositionAndSize(100, 100, 1024, 768);
I also found an example in the WinUI3 Samples
I'm adding the relevant code here for easy reference
private void SetWindowSize(IntPtr hwnd, int width, int height)
{
var dpi = PInvoke.User32.GetDpiForWindow(hwnd);
float scalingFactor = (float)dpi / 96;
width = (int)(width * scalingFactor);
height = (int)(height * scalingFactor);
PInvoke.User32.SetWindowPos(hwnd, PInvoke.User32.SpecialWindowHandles.HWND_TOP,
0, 0, width, height,
PInvoke.User32.SetWindowPosFlags.SWP_NOMOVE);
}
I can't comment on answers yet but to add to Jonas' answer, you can put his answer in the class constructor (where this.InitializeComponent() is) of your main window code-behind or in the OnLaunched method of App.cs. I'm sure that seems obvious to a lot of people but to those coming from other languages/platforms it might not be. Example:
public MainWindow()
{
this.InitializeComponent();
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); // m_window in App.cs
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
var size = new Windows.Graphics.SizeInt32();
size.Width = 480;
size.Height = 800;
appWindow.Resize(size);
// or like Jonas said:
// appWindow.Resize(new Windows.Graphics.SizeInt32 { Width = 480, Height = 800 });
}
This works. You will need to add the nuget package PInvoke.User32.
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
//Get the Window's HWND
var windowNative = m_window.As<IWindowNative>();
m_windowHandle = windowNative.WindowHandle;
m_window.Activate();
SetWindowBounds(0,0,100,100);
}
private Window m_window;
private IntPtr m_windowHandle;
public IntPtr WindowHandle { get { return m_windowHandle; } }
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
IntPtr WindowHandle { get; }
}
public void SetWindowBounds(int x, int y, int width, int height)
{
var dpi = PInvoke.User32.GetDpiForWindow(m_windowHandle);
float scalingFactor = (float)dpi / 96;
width = (int)(width * scalingFactor);
height = (int)(height * scalingFactor);
PInvoke.User32.SetWindowPos(m_windowHandle, PInvoke.User32.SpecialWindowHandles.HWND_TOP,
x, y, width, height,
PInvoke.User32.SetWindowPosFlags.SWP_NOMOVE);
}

C# helpProvider SetHelpString does not support unicode

I am using HelpProvider to show help for my control.
I input the help string for HelpProvider control. But this does not show the string properly.
P/S:
My language is Vietnam, which is a unicode font.
Here is my text when progamming: "Chúc mừng năm mới"
Here is the text when shown:
It's an old bug and it's because of two problems:
The default font which is used by underlying API of the HelpProvider doesn't support unicode characters
The underlying API of the HelpProvider doesn't support Unicode.
After fixing these two problems, you can show Unicode characters correctly:
HelpExtensions.ShowPopup2(button1, "متن آزمایشی", Control.MousePosition);
The first problem is in Help class(.NET 4.X, .NET 5) which has created the HH_POPUP but hasn't specified any font for it. As a result a default font which doesn't support Unicode characters will be used.
A possible fix is using a default font like SystemFonts.CaptionFont which supports Unicode characters.
For the second problem, you need to change a setting in Windows, to do so:
Go to Control Panel → Region → Administrative tab, then in the section "Language for non-Unicode programs", click on "Change system locale ..." button and then in the next dialog, choose the language of your choice, for example Persian.
Or to support other languages as well you can choose: "Beta: Use Unicode UTF-8 for worldwide language support" which is Beta.
And here is HelpExtensions class:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public static class HelpExtensions
{
public static void ShowPopup2(Control parent, string caption, Point location, Font font = null, Color? backColor = null, Color? foreColor = null)
{
font = font ?? SystemFonts.CaptionFont;
backColor = backColor ?? Color.FromKnownColor(KnownColor.Window);
foreColor = foreColor ?? Color.FromKnownColor(KnownColor.WindowText);
var popup = new HH_POPUP();
popup.clrBackground = new COLORREF(backColor.Value);
popup.clrForeground = new COLORREF(foreColor.Value);
popup.pt = new POINT(location);
var pszText = Marshal.StringToCoTaskMemAuto(caption);
popup.pszText = pszText;
var pszFont = Marshal.StringToCoTaskMemAuto(
$"{font.Name}, {font.Size}, , " +
$"{(font.Bold ? "BOLD" : "")}" +
$"{(font.Italic ? "ITALIC" : "")}" +
$"{(font.Underline ? "UNDERLINE" : "")}");
popup.pszFont = pszFont;
try
{
HtmlHelp(parent.Handle, null, HTMLHelpCommand.HH_DISPLAY_TEXT_POPUP, popup);
}
finally
{
Marshal.FreeCoTaskMem(pszText);
Marshal.FreeCoTaskMem(pszFont);
}
}
[Flags()]
public enum HTMLHelpCommand : uint
{
HH_DISPLAY_TOPIC = 0,
HH_DISPLAY_TOC = 1,
HH_DISPLAY_INDEX = 2,
HH_DISPLAY_SEARCH = 3,
HH_DISPLAY_TEXT_POPUP = 0x000E,
HH_HELP_CONTEXT = 0x000F,
HH_CLOSE_ALL = 0x0012
}
[DllImport("hhctrl.ocx", SetLastError = true, EntryPoint = "HtmlHelpW", CharSet = CharSet.Unicode)]
static extern int HtmlHelp(IntPtr hWndCaller,
[MarshalAs(UnmanagedType.LPWStr)] string pszFile,
HTMLHelpCommand uCommand,
[MarshalAs(UnmanagedType.LPStruct)] HH_POPUP dwData);
[StructLayout(LayoutKind.Sequential)]
struct COLORREF
{
int ColorRef;
public COLORREF(int lRGB)
{
ColorRef = lRGB & 0x00ffffff;
}
public COLORREF(Color color) : this(color.ToArgb())
{
}
}
[StructLayout(LayoutKind.Sequential)]
class POINT
{
public int x;
public int y;
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
public POINT(Point p) : this(p.X, p.Y)
{
}
}
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)
{
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
class HH_POPUP
{
internal int cbStruct = Marshal.SizeOf(typeof(HH_POPUP));
internal IntPtr hinst = IntPtr.Zero;
internal int idString = 0;
internal IntPtr pszText;
internal POINT pt;
internal COLORREF clrForeground = new COLORREF(-1);
internal COLORREF clrBackground = new COLORREF(-1);
internal RECT rcMargins = new RECT(-1, -1, -1, -1);
internal IntPtr pszFont;
}
}
HelpProvider2 Component
I've created a HelpProvider2 component which supports Unicode characters. It also exposes Font, ForeColor and BackColor properties:
Download or clone
Repository: r-aghaei/HelpProvider2Example.git
Download master.zip

InputBox Position

How to open the Interaction.InputBox to center of the form? I know there is a code for the position of the InputBox
Interaction.InputBox("Question?", "Title", "Default Text", x,y);
I will be using this InputBox in different form of different sizes. Is there a way to open the InputBox in the center of the form? Or I have to position them individually on each form?
Is it possible also to reposition the OKbutton and Cancelbutton of InputBox?
Here is something simple to calculate the center of a form, the extra offset is for the size of the input box.
{
int x = this.Left + (this.Width / 2) - 200;
int y = this.Top + (this.Height / 2) - 100;
}
Pass these into the input box for x and y
If you want full customisation then creating your own form is the best way to go as indicated in Fabio's comment.
However, if you just want to approximately centre the box and you will be doing it many times then you could write your own extension method to show and position the input box for you:
public static class FormExtensions
{
public static string CentredInputBox(this Form form, string prompt, string title = "", string defaultResponse = "")
{
const int approxInputBoxWidth = 370;
const int approxInputBoxHeight = 158;
int left = form.Left + (form.Width / 2) - (approxInputBoxWidth / 2);
left = left < 0 ? 0 : left;
int top = form.Top + (form.Height / 2) - (approxInputBoxHeight / 2);
top = top < 0 ? 0 : top;
return Microsoft.VisualBasic.Interaction.InputBox(prompt, title, defaultResponse, left, top);
}
}
Usage from within a form:
this.CentredInputBox("MyPrompt", "MyTitle", "MyDefaultResponse");
It's not perfect because if the box is bigger than normal for some reason then it won't quite be in the centre, and I think its size is variable depending on how much text is in it. However, it shouldn't be far off in normal usage.
To center your InputBox, you can try using Win32 functions to deal with it. This code works for you:
[DllImport("user32")]
private static extern int SetWindowPos(IntPtr hwnd, IntPtr afterHwnd, int x, int y, int cx, int cy, int flag);
[DllImport("user32")]
private static extern IntPtr FindWindow(string className, string caption);
[DllImport("user32")]
private static extern int GetWindowRect(IntPtr hwnd, out RECT rect);
//RECT structure
public struct RECT {
public int left, top, right, bottom;
}
public void ShowCenteredInputBox(string prompt, string title, string defaultReponse){
BeginInvoke((Action)(() => {
while (true) {
IntPtr hwnd = FindWindow(null, title + "\n\n\n");//this is just a trick to identify your InputBox from other window with the same caption
if (hwnd != IntPtr.Zero) {
RECT rect;
GetWindowRect(hwnd, out rect);
int w = rect.right - rect.left;
int h = rect.bottom - rect.top;
int x = Left + (Width - w) / 2;
int y = Top + (Height - h) / 2;
SetWindowPos(hwnd, IntPtr.Zero, x, y, w, h, 0x40);//SWP_SHOWWINDOW = 0x40
break;
}
};
}));
Microsoft.VisualBasic.Interaction.InputBox(prompt, title + "\n\n\n", defaultResponse,0,0);
}
Of course you can also change the position of the buttons, label and TextBox on your InputBox but it's very nasty and tricky, we can say that it's not simple. The recommended solution for you is to create new standard form in System.Windows.Forms.Form, add controls to it and use the method ShowDialog() to show your form.. Of course it requires more code to do but it allows you to fully customize the look and feel and its behaviors.
You can set the InputBox's starting position. There's a property for that
InputBox ib = new InputBox();
ib.StartPosition = FormStartPosition.CenterParent;
Where as FormStartPosition is an enum, from which you can select your desired position!
You can sipmly use -1 for x and y:
Interaction.InputBox("Question?", "Title", "Default Text", -1,-1);

How to set border of Shapes inside Word Document to None using c#?

i've created a label in a Word Document like this
public void CreateLabel(string LabelName, int left, int top, int width, int height, string text)
{
var oRange = currentDoc.Bookmarks.get_Item("\\endofdoc").Range;
var oshape = oRange.Document.Shapes.AddLabel(MsoTextOrientation.msoTextOrientationHorizontal, left, top, width, height);
oshape.Name = LabelName;
oshape.TextFrame.ContainingRange.Borders.OutsideLineStyle=WdLineStyle.wdLineStyleNone;
oshape.TextFrame.ContainingRange.Text = text;
oshape.TextFrame.ContainingRange.Font.Size = 14;
}
but it never sets border to none.what's the problem?
There is a good article about Formatting Lines of the Shapes.you can find something about line formathing there,and this is the solution of my problem.hope it's useful for some one else.
oshape.Line.Visible = MsoTriState.msoFalse;

Categories

Resources