winforms - Visual c++, resize a control on mousedown

I made a UserControllibrary that contains several regular Button control.

i would like to resize it on drag. dragdetction is done via windows messages and it seams to work flawless.

I even managed to set a correct cursor and .. back on WM_MOUSELEAVE.

virtual void WndProc( Message %m ) override
{
    // Listen for operating system messages
    switch ( m.Msg )
    {
        // more code
        // .
        // ..
        // ...
        case WM_MOUSEMOVE:
            if(m.WParam.ToInt32() ==  MK_CONTROL)
            {
                Debug::WriteLine("MK_CONTROL");
                return;
            }
            else if(m.WParam.ToInt32() ==  MK_LBUTTON)
            {
                Debug::WriteLine("MK_LBUTTON");
                if(isMouseDown)
                {
                    Debug::WriteLine("drag Detected");
                    Debug::WriteLine("isMouseDown: " + isMouseDown.ToString());
                    int tempX = (short)(m.LParam.ToInt32() & 0x0000FFFF);
                        this->Size.Width = (this->Location.X - tempX); // <--- does not work!
                    return;
                }
                return;
            }
            else if(m.WParam.ToInt32() ==  MK_MBUTTON)
            {
                Debug::WriteLine("MK_MBUTTON");
                return;
            }
            else if(m.WParam.ToInt32() ==  MK_RBUTTON)
            {
                Debug::WriteLine("MK_RBUTTON");
                return;
            }
            else if(m.WParam.ToInt32() ==  MK_SHIFT)
            {
                Debug::WriteLine("MK_SHIFT");
                return;
            }
            else if(m.WParam.ToInt32() ==  MK_XBUTTON1)
            {
                Debug::WriteLine("MK_XBUTTON1");
                return;
            }
            else if(m.WParam.ToInt32() ==  MK_XBUTTON2)
            {
                Debug::WriteLine("MK_XBUTTON2");
                return;
            }   
        return;
        // more code
        // .
        // ..
        // ...
        return;
    }
    System::Windows::Forms::UserControl::WndProc( m );
}

this However this->Size.Width = (this->Location.X - e->Location.X); // <--- does not work! this->Size.Width will stay on it's default value of 400 previously set by the properties windows.

I know it's possible to set the size via windows messages, but i fail to understand how. taken from a C# example: Controls won't get resized once the nesting hierarchy of windows exceeds a certain depth

    // this doesn't seam the right synthax for C++
    [DllImport("User32.dll", CharSet=CharSet.Auto)]
    public static extern int SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);

    [DllImport("User32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern bool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter,
    int x, int y, int cx, int cy, int flags);

UserControl does not have a property/methode called SetWindowPos

How to proceed?

1 Answer

  1. Glen- Reply

    2019-11-13

    Size property returns a strucutre, which is value type. So you get a copy, which you modify, but the original stays the same. To change form's size, you can set:

    this->Size = new Size(100, 100);

    or better, use Width and Height propetries:

    this->Width += 100;

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>