Results 1 to 3 of 3

Thread: Borderless Forms with a Border

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Borderless Forms with a Border

    [Written by me for another forum so some text may be out of place]

    So you don't want the Window manager to handle your form's border.. But you want a border.. How do you do it!

    Well if you're like me you'd think about a Panel with a border (BorderStyle: FixedSingle) that overlays the whole form... Which is great! Also yup it works great too, but what? It's black... Do you want it to be black?? Nope you don't, so let's change BorderColor.. ??????? It doesn't exist???? WHAT?!?!

    Well what we have to do is to add an event handler (Paint method), use the following code!
    Code:
    // Add event handlers
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.BorderPaint);
                this.Resize += new System.EventHandler(this.Invalidate);
    Now for the functions!
    Code:
    private void BorderPaint(object sender, PaintEventArgs e)
            {
                // If you're only using this for the form just get rid of Control s = and the if() statement
                Control s = (Control)sender;
                if (s.Name == this.Name) { // the muppet is drawing other things too
                    // Draw rectangle border (need to -1 @ H/W so we're inside the client area)
                    e.Graphics.DrawRectangle(new Pen(Color.DarkGray), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
                }
                // Credz to Strife- for trying to make me Dispose of objects :D
                GC.Collect(); // This is amazing: M:928264|544368
            }
    
            private void Invalidate(object sender, EventArgs e)
            {
                // Invalidate makes the form invalid forces the Paint method so the border isn't drawn and drawn and drawn over itself
                Invalidate();
            }
    --------------------------------

    You may also want some way to move the form by clicking in the client area...
    Code:
    protected override void WndProc(ref Message m)
    {     
        base.WndProc(ref m); // Call original WndProc
        if (m.Msg == 0x84 && m.Result.ToInt32() == 1)
        {
            m.Result = new IntPtr(2); // Tell WndProc they clicked on the title bar
        }
    }
    Or you could want a way to move it from them clicking on a control, which is done by events again.. MouseMove this time.
    Code:
    using System.Runtime.InteropServices;
            [DllImportAttribute("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
            [DllImportAttribute("user32.dll")]
            public static extern bool ReleaseCapture();
    Adding our event handler..
    Code:
    this.pTopPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.moveWindow);
    Now for our function..
    Code:
    private void moveWindow(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(Handle, 0xA1, 0x2, 0);
                }
            }
    You're probably thinking why can't we just create a new message and send it to base.WndProc... Well, you probably could but you'll need to setup a whole message (more than just .Msg & .Result).
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  2. #2
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    Re: Borderless Forms with a Border

    You could also do the following to get a custom border:

    Set the FormBorderStyle = None
    Set the Padding to 1 on all sides (or however thick you want the border to be)
    Set the Background color of the form to DarkGray (or whatever color)
    Drop a Panel control onto your form and set the Dock property to Fill
    Set the Background color of the panel to SystemColors.Control or whatever other color you may want

    then the following code in your mousedown and mousemove events will let you drag the form around your screen:
    Code:
    int xx = 0; int yy = 0;
    private void MainPanel_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            xx = e.X; yy = e.Y;
        }
    }
    
    private void MainPanel_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.Left = this.Left - (xx - e.X);
            this.Top = this.Top - (yy - e.Y);
        }
    }
    And there you go, a borderless bordered form with only managed code.
    Attached Images Attached Images  

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Borderless Forms with a Border

    Oh yes that's a very nice way to do it! Thanks
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width