简体   繁体   English

更改Windows窗体

[英]Changing Windows Form

I'm looking into a way of altering the frame around a windows form box. 我正在研究一种更改Windows窗体框周围的框架的方法。 I want to make it either transparent, or get rid of it entirely. 我想使其透明或完全摆脱它。 I managed to get rid of the icon, and get rid of the maximize button, as well as restrict the user's ability to resize the window. 我设法摆脱了图标,摆脱了最大化按钮,并限制了用户调整窗口大小的能力。 The bar clashes with the visual theme I'm trying to implement. 该栏与我要实现的视觉主题冲突。

So question - Is there any good way customizing the form border & the top bar in a Windows Form application? 所以问题-在Windows窗体应用程序中自定义窗体边框和顶部栏有什么好方法吗?

我要编辑的栏

FormBorderStyle选择正确的值,例如None

This might be overkill for what you're asking, but here goes... This was a quick application I created with a circular border. 这可能对您要问的内容有些矫kill过正,但是可以解决...这是我创建的带有圆形边框的快速应用程序。 It's not the entire code, but maybe you'll get the idea from it. 这不是全部代码,但是也许您会从中得到灵感。

First, I remove the FormBorderStyle (Set it equal to None) and add a mouse handler to allow movement of the form. 首先,我删除FormBorderStyle(将其设置为None)并添加鼠标处理程序以允许移动表单。 Code below... 下面的代码...

    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;


    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

Then I created a pink mesh to display as the form background. 然后,我创建了一个粉红色的网格以显示为表单背景。 and set the TransparencyKey property to the RGB value of the background color I wanted to be transparent (in this case 255,15,255). 并将TransparencyKey属性设置为我想要透明的背景颜色的RGB值(在本例中为255,15,255)。

透明网格

Then I drew the literal controls with a graphics module. 然后,我使用图形模块绘制文字控件。

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Set the starting coordinants for our graphics drawing
        int y = 0;
        int x = 0;
        // Set the end coordinants for our graphics drawing
        int width = this.Size.Width;
        int height = this.Size.Height;
        // Set our graphics options
        e.Graphics.InterpolationMode = XGraphics.xInterpolation;
        e.Graphics.SmoothingMode = XGraphics.xSmoothingMode;
        e.Graphics.CompositingMode = XGraphics.xComposingMode;
        e.Graphics.CompositingQuality = XGraphics.xComposingQuality;
        e.Graphics.PixelOffsetMode = XGraphics.xPixelOffsetMode;
        // Set the colors, positions, and gradient directions then draw our background

        using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width, height))
        {
            // Create our color blender object
            ColorBlend gpxBlend = null;
            gpxBlend = new ColorBlend(4);
            gpxBlend.Colors = new Color[] { Color.FromArgb(255, 0, 100, 255), Color.FromArgb(205, 255, 240, 240),
            Color.FromArgb(255, 20, 100, 230), Color.FromArgb(105, 105, 100, 255)  };
            gpxBlend.Positions = new float[] { 0.0F, .45F, .55F, 1.0F };
            gpxBrush.InterpolationColors = gpxBlend;
            // Draw our background
            e.Graphics.FillEllipse(gpxBrush, x, y, width, height);
        }

        // Set the end coordinants for our graphics drawing
        width = this.Size.Width-5;
        height = this.Size.Height-5;

        using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width, height))
        {
            // Create our color blender object
            ColorBlend gpxBlend = null;
            gpxBlend = new ColorBlend(2);
            gpxBlend.Colors = new Color[] { Color.White, Color.Gray };
            gpxBlend.Positions = new float[] { 0.0F, 1.0F };
            gpxBrush.InterpolationColors = gpxBlend;
            // Draw our background
            e.Graphics.FillEllipse(gpxBrush, x+5, y+5, width-5, height-5);
        }



        width = this.Size.Width / 2;
        height = this.Size.Height / 2;
        using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width, height))
        {
            // Create our color blender object
            ColorBlend gpxBlend = null;
            gpxBlend = new ColorBlend(2);
            gpxBlend.Colors = new Color[] { Color.White, Color.Gray };
            gpxBlend.Positions = new float[] { 0.0F, 1.0F };
            gpxBrush.InterpolationColors = gpxBlend;
            // Draw our background
            e.Graphics.FillEllipse(gpxBrush, x + width / 2, y + height / 2, width, height);
        }

        width = (this.Size.Width / 2) - 5;
        height = (this.Size.Height / 2) - 5;
        using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width-5, height-5))
        {
            // Create our color blender object
            ColorBlend gpxBlend = null;
            gpxBlend = new ColorBlend(4);
            gpxBlend.Colors = new Color[] { Color.FromArgb(255, 0, 100, 255), Color.FromArgb(205, 255, 240, 240),
            Color.FromArgb(255, 20, 100, 230), Color.FromArgb(105, 105, 100, 255)  };
            gpxBlend.Positions = new float[] { 0.0F, .45F, .55F, 1.0F };
            gpxBrush.InterpolationColors = gpxBlend;
            // Draw our background
            e.Graphics.FillEllipse(gpxBrush, (x + width / 2)  +7, (y + height / 2) + 7, width - 5, height - 5);
        }
    }

没有背景或表格边框

As you can see, there is no border now and the edges are transparent. 如您所见,现在没有边框,并且边缘是透明的。 Just make sure to add a button to exit the application since there isn't an 'X' option anymore! 只需确保添加一个按钮以退出应用程序,因为不再有“ X”选项! ;) ;)

Hope this helps you somewhat! 希望这对您有所帮助!

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

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