简体   繁体   English

将标签文本和按钮旋转90度

[英]Rotating label text and buttons through 90 degrees

I am trying to rotate a label through 90 degrees. 我正在尝试将标签旋转90度。 At the moment I can take the text from the label and rotate that, but what I want to do is actually rotate a label of my choice, or if I were to be really flashy, lets say a button control. 目前,我可以从标签中取出文本并旋转它,但是实际上我想旋转的是我选择的标签,或者如果我真的很浮华,可以说是一个按钮控件。 So using the code below, how can I modify it so that I can feed it a control and get it to rotate it? 因此,使用下面的代码,如何修改它,以便可以为它提供控件并使其旋转?

protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;

        string text = label4.Text;

        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.Trimming = StringTrimming.None;

        Brush textBrush = new SolidBrush(this.ForeColor);

        //Getting the width and height of the text, which we are going to write
        float width = graphics.MeasureString(text, this.Font).Width;
        float height = graphics.MeasureString(text, this.Font).Height;

        //The radius is set to 0.9 of the width or height, b'cos not to 
        //hide and part of the text at any stage
        float radius = 0f;
        if (ClientRectangle.Width < ClientRectangle.Height)
        {
            radius = ClientRectangle.Width * 0.9f / 2;
        }
        else
        {
            radius = ClientRectangle.Height * 0.9f / 2;
        }
        int rotationAngle = 90;
        double angle = (rotationAngle / 180) * Math.PI;
        graphics.TranslateTransform(
            (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
            (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
        graphics.RotateTransform((float)rotationAngle);
        graphics.DrawString(text, this.Font, textBrush, 0, 0);
        graphics.ResetTransform();        
    }

Standard windows forms controls (such as a label and button) are rendered by the operating system itself, windows forms doesn't do the actual drawing. 标准Windows窗体控件(例如标签和按钮)由操作系统本身呈现,Windows窗体不进行实际绘制。

Therefore, unfortunately, you have no control over aspects such as rotation and scaling with these sorts of controls. 因此,不幸的是,您无法使用这些控件来控制旋转和缩放等方面。 This is just a limitation of Windows Forms itself and is one of the major reasons Microsoft created WPF. 这只是Windows Forms本身的限制,并且是Microsoft创建WPF的主要原因之一。

WPF controls are entirely rendered by WPF (using DirectX behind the scenes). WPF控件完全由WPF渲染(在后台使用DirectX)。 WPF supports all the standard 2D (and 3D) transaformations such as scaling, rotation and translation. WPF支持所有标准2D(和3D)变换,例如缩放,旋转和平移。

Altrrnatively in windows forms you could create a custom control that you render using GDI+ and can rotate and scale as required. 另外,在Windows窗体中,您可以创建一个自定义控件,该控件使用GDI +呈现,并且可以根据需要旋转和缩放。 Of course now you're doing all the work yourself which it seems is not what you want. 当然,现在您要自己完成所有工作,这似乎不是您想要的。

您可以使用WPF代替WinForms ...然后进行简单的转换;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM