none
How can i make a borderless form fullscreen by dragging it to top? RRS feed

  • Question

  • First i used mouse_up event with `e.Y` but shortly i realized that's not keeping the mouse location from screens 0,0 point but events own 0,0 point, how can i do this?

    This is the code i use for dragging (may be needed for editing):

            int movX, movY;
            bool mov;
    
    private void UpperPanel_MouseClick(object sender, MouseEventArgs e)
            {
                formLoc = this.Location;
            }
    
            private void UpperPanel_MouseMove(object sender, MouseEventArgs e)
            {
    
                if (e.Button == MouseButtons.Left)
                {
                    if (mov == true) this.SetDesktopLocation(MousePosition.X - movX, MousePosition.Y - movY);
    }
            }
    
            private void UpperPanel_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mov = false;
                }
            }
    
            private void UpperPanel_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mov = true;
                    movX = e.X;
                    movY = e.Y;
                }
            }
    • Moved by CoolDadTx Monday, June 17, 2019 1:42 PM Winforms related
    Sunday, June 16, 2019 1:12 AM

All replies

  • I am not sure what you are asking exactly but you are able to create some of the title bar functions on a borderless form using the library function SendMessage.

    For example to drag a form around the screen area you could add a picture box somewhere on your form and use the following code.

    public partial class Form1 : Form
        {
    
            private const int HT_CAPTION = 0x2;
            private const int WM_NCLBUTTONDOWN = 0xA1;
           
            [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                pictureBox1.Capture = false;
                SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

    then add events or controls to modify form properties if rerquired

    Sunday, June 16, 2019 2:19 PM
  • I am so sorry for my poor explanation. What i want to do is, making a borderless (flat border styled) form to get fullscreen by dragging it to top just like how normal windows forms do, i got the moving part but i couldn't figure how to make it fullscreen when i drag it to the top.

    Here is an example for what i want to do with my flat form:

    Sunday, June 16, 2019 3:29 PM
  • Just as an example if you did use the code I posted with a picture box then you would move the form using the left mouse button down inside the picture box. To maximize your form there are several options, here is an example where the left button moves the form and the right button maximizes the form. You have the option to add controls or create events to maximize at a certain location.

     private const int HT_CAPTION = 0x2;
            private const int WM_NCLBUTTONDOWN = 0xA1;
           
            [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                 this.Capture = false;
    
                 SendMessage(this.Handle, WM_NCLBUTTONDOWN,HT_CAPTION, 0);
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                pictureBox1.Capture = true;
                if (e.Button == MouseButtons.Right) { this.WindowState = FormWindowState.Maximized; }
                else { pictureBox1.Capture = false;
                SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); }
               
            }

    Sunday, June 16, 2019 6:31 PM
  • There is this big problem, when you move the form like crazy it crashes with this (tried on 2 different forms one had no problem but the other give this result every try):

    error System.AccessViolationException
      HResult=0x80004003
      Message=(this part was in my language, it basically said "Attempt of reading or writing protected memory. This usually means the other memory is faulty")
      Source=<Cannot evaluate the exception source>
      StackTrace:
    <Cannot evaluate the exception stack trace>

    Also this code overlaps with mouse double click as well which i use. But thank you for the information!

    Edit: Hmm now that i looked at it 0x80004003 was 32 bit integer limit, i don't know how to limit that but at least i understand the reason of crash.

    • Edited by xanrer Sunday, June 16, 2019 10:16 PM Additional info
    Sunday, June 16, 2019 10:06 PM
  • Hi xanrer,

    Thank you for posting here.

    Based on your description, you want to make a borderless form fullscreen by dragging it to top.

    I make a simple code from Jeff's solution.

    Code:

     private const int HT_CAPTION = 0x2;
            private const int WM_NCLBUTTONDOWN = 0xA1;
    
            [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                this.FormBorderStyle = FormBorderStyle.None;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.Image = Image.FromFile(@"D:\pic\5.jpg");
            }
    
            private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                pictureBox1.Capture = true;
                if (e.Button == MouseButtons.Right) { this.WindowState = FormWindowState.Maximized; this.FormBorderStyle = FormBorderStyle.None; }
                else
                {
                    pictureBox1.Capture = false;
                    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 6:39 AM
  • Hello Jack, my intention was doing that without using windowses own system, so its flat bordered from the start. But there is that error happened and flat form doesn't get fullscreen with SendMessage(...) command. Its only working for normal forms.
    Monday, June 17, 2019 5:01 PM
  • Hi xanrer,

    Thanks for the feedback.

    >>my intention was doing that without using windowses own system, so its flat bordered from the start.

    I don't quite understand your meaning, could you give an example to express your thought?

    >>But there is that error happened and flat form doesn't get fullscreen with SendMessage(...) command

    I want to when the error will occur, could you provide the related code?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 9:40 AM
  • Hello Jack sorry for the late response, here is the error message:

    error System.AccessViolationException
      HResult=0x80004003
      Message=(this part was in my language, it basically said "Attempt of reading or writing protected memory. This usually means the other memory is faulty")
      Source=<Cannot evaluate the exception source>
      StackTrace:
    <Cannot evaluate the exception stack trace>

    "my intention was doing that without using windowses own system, so its flat bordered from the start."
    What i meant by that was, from the picture you send, i can see the standart border style of windows at start, then you move it to top and then it becomes flat style. However in my project the form is already flat bordered so when i move it to top it doesn't become fullscreen, that's what im asking, is there a way to do that with a flat bordered form.
    Thursday, June 20, 2019 3:54 PM