0

i'm working on a simple overlay application, which border style is set to "none".
i want to be able to drag my Winform, no matter if I click on the Form or on its control.

i found this snippet:

protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case 0x84:
            base.WndProc(ref m);
            if ((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;
            return;
    }

    base.WndProc(ref m);
}

move a c# form without border style (by ShaneB)
This snippet allows me to make the form dragable, if I don't click on a control.
The Form should be dragable on other controls too (like groupboxes), while the mouseDown event is triggered.

Remark: A checkbox should still be able to be checked / unchecked, if possible. If the last point should be to complex, I can work around this problem.

Community
  • 1
  • 1
MrMAG
  • 1,194
  • 1
  • 11
  • 34
  • Word of warning... this kind of thing is nearly always problematic and nearly always a bit of a pain for the user. Try not to redefine the windows experience. – BenjaminPaul Sep 22 '14 at 08:44
  • I'm aware that I should try to avoid that kind of redefining the windows experience. But it will be a very simply application, which just shows some current states of another program. And that's why I though a overlay would be the best solution, which I set on TopMost and remove the background from the Form. (by setting TransparencyKey == BackColor). – MrMAG Sep 22 '14 at 08:50
  • If the window should be movable I would give the window a border, if I were you I would consider these issues you are having a code smell. – BenjaminPaul Sep 22 '14 at 08:58
  • This will work just fine for a GroupBox as well. Just derive your own class from GroupBox and override WndProc() the same way. When you look at the result, you'll have a much better understanding why Microsoft made their windows look that way. – Hans Passant Sep 22 '14 at 09:54
  • @Hans Passant - This makes the custom control drag-able. Maybe I did not describe this well enough. The Main-Form should be drag-able even if I can click inside a GroupBox fx. I'm currently trying the `panel1_MouseMove` event to get this work. Just having position issues atm: `if (e.Button == MouseButtons.Left) Location = new Point(Location.X, Location.Y);` – MrMAG Sep 22 '14 at 10:17
  • `Location = new Point(Cursor.Position.X + e.X, Cursor.Position.Y + e.Y);` – MrMAG Sep 22 '14 at 10:23
  • If that didn't invoke the thought "maybe I shouldn't do this" then it gets hard to help you. Return -1 instead of 2. – Hans Passant Sep 22 '14 at 10:23
  • Returning (-1) does exactly what I want! Thank you. Do add this as a answer? Then I'll mark it as solved! – MrMAG Sep 22 '14 at 10:28

1 Answers1

0

I was able to fix my problem by returning -1. (m.Result = (IntPtr)(-1);)
Many thanks to Hans Passant for your advice.

I just needed to derive a new class from a GroupBox and override WndProc() like this:

public sealed partial class CustomGroupBox : GroupBox
{
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 0x84:
                base.WndProc(ref m);
                if ((int)m.Result == 0x1)
                    m.Result = (IntPtr)(-1);
                return;
        }

        base.WndProc(ref m);
    }
}
Community
  • 1
  • 1
MrMAG
  • 1,194
  • 1
  • 11
  • 34