简体   繁体   中英

Changing Windows Form

I'm looking into a way of altering the frame around a windows form box. 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?

我要编辑的栏

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. 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. 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).

透明网格

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! ;)

Hope this helps you somewhat!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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