根据标题栏中的文本设置表单的宽度 [英] Setting form's width according to text in title bar

查看:33
本文介绍了根据标题栏中的文本设置表单的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当(或之后)我调用我的 Form.Show() 方法时,我想根据标题栏中的字符串(Form.Text),因为这个文本会根据用户的选择而改变.表单宽度最终应该是完美的数量,以便标题栏文本不会被截断并以..."结尾.
目前,我只是将 MinimumSize 属性设置为适合所有选择的值,但自动调整大小看起来会稍微整洁一些.我的问题是标题栏文本没有任何属性来获取其实际宽度.AutoSize 也只是根据表单中的控件改变大小.

有什么我可以做的吗?

解决方案

您可以获取系统菜单的宽度、标题文本的宽度和控制框按钮的宽度,然后计算首选宽度并检查当前宽度小于首选宽度,然后将 for 的宽度更改为首选宽度.

要获取有关标题栏元素的信息,您可以发送

但在运行时:

替代解决方案 - 为标题栏设置工具提示

作为替代解决方案,您可以

When (or after) I call my Form.Show() method, I would like to set the form's width (or minimum width) according to the string in the title bar (Form.Text), since this text is changing according to a user's choice. The form width should end up being the perfect amount so that the title bar text doesn't cut off and end with "...".
Currently, I'm simply setting the MinimumSize property to a value that accommodates for all choices, but an automatic sizing would look slightly neater. My problem here is that the title bar text doesn't have any properties to get its actual width. AutoSize also only changes the size according to the controls in the form.

Is there anything I can do?

解决方案

You can get the width of the system menu, width of the title text and width of control box buttons, then calculate the preferred width and check if the current width is less that the preferred with, then change the widths of the for to preferred width.

To get the information about titlebar elements, you can send a WM_GETTITLEBARINFOEX message to window and get an instance of TITLEBARINFOEX structure which contains information about titlebar.

You can also use SystemInformation.CaptionButtonSize to get control button sizes and SystemInformation.SmallIconSize to get system menu icon size and add a bit of extra padding space.

Example

You can override OnShown method of the form like this:

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);

    var info = NativeMethods.GetTitleBarInfo(this.Handle);
    var systemMenuWidth = info.rcTitleBar.Left - this.Left;
    var controlBoxWidth =
        info.rgrect[(int)NativeMethods.TitleBarRectangles.CloseButton].Right -
        info.rgrect[(int)NativeMethods.TitleBarRectangles.MinimizeButton].Left;
    var titleWidth = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width;

    //var preferred = titleWidth + systemMenuWidth + controlBoxWidth + 16;
    //if (this.Width < preferred)
    //   this.Width = preferred;

    var preferred = titleWidth + systemMenuWidth + controlBoxWidth;
    if (this.Width < preferred)
        this.ClientSize = new Size(preferred, this.ClientSize.Height);
}

And here is NativeMethods:

using System;
using System.Drawing;
using System.Runtime.InteropServices;

public static class NativeMethods
{
    public const int WM_GETTITLEBARINFOEX = 0x033F;
    public static TITLEBARINFOEX GetTitleBarInfo(IntPtr hwnd)
    {
        var info = new TITLEBARINFOEX()
        { cbSize = (uint)Marshal.SizeOf(typeof(TITLEBARINFOEX)) };
        SendMessage(hwnd, WM_GETTITLEBARINFOEX, IntPtr.Zero, ref info);
        return info;
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr SendMessage(
        IntPtr hWnd, int Msg, IntPtr wParam, ref TITLEBARINFOEX lParam);

    [StructLayout(LayoutKind.Sequential)]
    public struct TITLEBARINFOEX
    {
        public const int CCHILDREN_TITLEBAR = 5;
        public uint cbSize;
        public RECT rcTitleBar;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]
        public uint[] rgstate;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]
        public RECT[] rgrect;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left, Top, Right, Bottom;
        public Rectangle ToRectangle() => Rectangle.FromLTRB(Left, Top, Right, Bottom);
    }
    public enum TitleBarRectangles
    {
        TitleBar = 0,
        Reserved = 1,
        MinimizeButton = 2,
        MaximizeButton = 3,
        HelpButton = 4,
        CloseButton = 5
    }
}

So having such form in designer:

But at runtime:

Alternative solution - Set tooltip for titlebar

As an alternative solution you can add a ToolTip to the Form titlebar:

这篇关于根据标题栏中的文本设置表单的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆