Visual c ++,在mousedown上调整控件的大小

时间:2013-03-26 23:30:26

标签: winforms winapi visual-c++ winforms-interop

我创建了一个包含几个常规Button控件的UserControl库。

我想在拖动时调整它的大小。 dragdetction是通过Windows消息完成的,并且接缝完美无缺。

我甚至设法在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->Size.Width = (this->Location.X - e->Location.X); //&lt; ---不起作用! this->Size.Width将保留其先前由属性窗口设置的默认值400。

我知道可以通过Windows消息设置大小,但我不明白如何。 取自C#示例: 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没有名为SetWindowPos

的属性/方法

如何进行?

1 个答案:

答案 0 :(得分:0)

Size属性返回一个strucutre,它是值类型。所以你得到一个你修改过的副本,但原件保持不变。要更改表单的大小,您可以设置:

this-&gt;尺寸=新尺寸(100,100);

或更好,使用Width和Height propetries:

this-&gt;宽度+ = 100;