Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi everyone,

I developing new software for education. But I don't wanna use Windows Border Style. So I need to change Windows Border Style or I need to create new Form Style.

I will deliver this project end of this month. Because of this, anyone suggest to me simple way?

C#
protected override void WndProc(ref Message m)
        {
            const UInt32 WM_NCHITTEST = 0x0084;
            const UInt32 WM_MOUSEMOVE = 0x0200;

            const UInt32 HTLEFT = 10;
            const UInt32 HTRIGHT = 11;
            const UInt32 HTBOTTOMRIGHT = 17;
            const UInt32 HTBOTTOM = 15;
            const UInt32 HTBOTTOMLEFT = 16;
            const UInt32 HTTOP = 12;
            const UInt32 HTTOPLEFT = 13;
            const UInt32 HTTOPRIGHT = 14;

            const int RESIZE_HANDLE_SIZE = 10;
            bool handled = false;
            if (m.Msg == WM_NCHITTEST || m.Msg == WM_MOUSEMOVE)
            {
                Size formSize = this.Size;
                Point screenPoint = new Point(m.LParam.ToInt32());
                Point clientPoint = this.PointToClient(screenPoint);

                Dictionary<UInt32, Rectangle> boxes = new Dictionary<UInt32, Rectangle>() {
            {HTBOTTOMLEFT, new Rectangle(0, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTBOTTOM, new Rectangle(RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, formSize.Width - 2*RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTBOTTOMRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2*RESIZE_HANDLE_SIZE)},
            {HTTOPRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) },
            {HTTOP, new Rectangle(RESIZE_HANDLE_SIZE, 0, formSize.Width - 2*RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) },
            {HTTOPLEFT, new Rectangle(0, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) },
            {HTLEFT, new Rectangle(0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2*RESIZE_HANDLE_SIZE) }
        };

                foreach (KeyValuePair<UInt32, Rectangle> hitBox in boxes)
                {
                    if (hitBox.Value.Contains(clientPoint))
                    {
                        m.Result = (IntPtr)hitBox.Key;
                        handled = true;
                        break;
                    }
                }
            }

            if (!handled)
                base.WndProc(ref m);
        }


I found this code block but it isn't work very well. My form must to be perfectly Resizable.

I waiting for your help. Thanks for answers...
Posted
Comments
BillWoodruff 13-Mar-15 13:43pm    
How exactly do you want to change the BorderStyle ?
Umut Comlekcioglu 13-Mar-15 14:35pm    
There is no borders appearing in Form but Form can be resizable. I want to do this, or like this.

Or Border style can like Gom Player borders.. Thanks for interesting...
BillWoodruff 14-Mar-15 0:09am    
Hi, Yes, this can be done, and done simply. I'll post a reply today.
Sergey Alexandrovich Kryukov 13-Mar-15 14:55pm    
Why? Windows "perfect resize" is already implemented for you...
—SA
Umut Comlekcioglu 13-Mar-15 17:39pm    
I need to change Windows Border Style because it's not compatible with my form design.

Right now, my formborderstyle property is None and I draw border in dynamicly. But in this time, I can't resize my form. I need to help resize form without border.

If you look at the border styles, you can see which ones are sizeable and which ones are not:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/hw8kes41%28v=vs.110%29.aspx[^].

That's not all. You can also control the size grip:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.sizegripstyle%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.sizegripstyle%28v=vs.110%29.aspx[^].

Now, if you really want to bite a bullet, you can render your own border from scratch in the client area. You can even use System.Windows.Forms.SizeGripStyle.None and implement resize also from scratch using mouse events. It is not as hard as you may think. I would estimate this part of work of some 20 minutes. Most important thing here is: you will need pure .NET FCL for that, nothing system-dependent.

As to your approach: it makes little sense, is extremely bulky and can even compromise your platform compatibility, because you use Windows-specific constants.

—SA
 
Share this answer
 
Here's a typical example of enabling both moving, and re-sizing a WinForm that has no TitleBar, FormBorderStyle set to 'None, etc.

The strategy used here is:

1. define two "zones:" a zone where the mouse held down and moving will move the Form, and a zone where the mouse held down and moving will re-size the Form.

2. check in the MouseDown EventHandler for a hit in either of those two zones, and set boolean flags.

3. in the MouseMove EventHandler take appropriate action.

4. if the Form has been re-sized, reset the re-size zone in the Form Resize EventHandler:
C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ResizableMovableForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Rectangle MoveZone;
        private Rectangle ResizeZone;

        private const int HitZoneSize = 12;

        private bool isMoving = false;
        private bool isResizing = false;
        private bool IsMouseUp = false;
        private int startWidth, startHeight, startMouseX, startMouseY;
        private int newWidth, newHeight;

        private void Form1_Load(object sender, EventArgs e)
        {
            MoveZone = new Rectangle(0, 0, HitZoneSize, HitZoneSize);
            ResizeZone = new Rectangle(Width - HitZoneSize, Height - HitZoneSize, HitZoneSize, HitZoneSize);
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseUp = true;
            isMoving = false;
            isResizing = false;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            startMouseX = e.X;
            startMouseY = e.Y;
            startWidth = this.Width;
            startHeight = this.Height;

            if (MoveZone.Contains(e.Location))
            {
                isMoving = true;
                IsMouseUp = false;
                return;
            }

            if (ResizeZone.Contains(e.Location))
            {
                isResizing = true;
                IsMouseUp = false;
                return;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseUp) return;

            if (isMoving)
            {
                this.Left += e.X - startMouseX;
                this.Top += e.Y - startMouseY;
                return;
            }
            else
            {
                if (isResizing)
                {
                    newWidth = startWidth + e.X - startMouseX;
                    if (newWidth < this.MinimumSize.Width || newWidth > this.MaximumSize.Width) return;
                    this.Width = newWidth;

                    newHeight = startHeight + e.Y - startMouseY;
                    if (newHeight < this.MinimumSize.Height || newHeight > this.MaximumSize.Height) return;
                    this.Height = newHeight;
                }
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            ResizeZone = new Rectangle(Width - HitZoneSize, Height - HitZoneSize, HitZoneSize, HitZoneSize);
        }
    }
}
Notes:

1. I strongly encourage you to set appropriate Minimum and Maximum sizes for your Form at design-time. The code above (for re-size) depends on having some meaningful values for those properties.

2. Even with the Form's DoubleBuffer property set to 'true, there may be some graphic side-effects of re-sizing which cannot be controlled by using SuspendLayout and ResumeLayout.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900