[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!
Now for the functions!Code:// Add event handlers this.Paint += new System.Windows.Forms.PaintEventHandler(this.BorderPaint); this.Resize += new System.EventHandler(this.Invalidate);
--------------------------------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...
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: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 } }
Adding our event handler..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();
Now for our function..Code:this.pTopPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.moveWindow);
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).Code:private void moveWindow(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); SendMessage(Handle, 0xA1, 0x2, 0); } }


Reply With Quote
