2013-03-26 29 views
0

我做了一个UserControl库,它包含几个常规的Button控件。Visual c + +,调整mousedown控件

我想调整它的拖动。通过windows消息来完成dragdetction,它的接缝工作无懈可击。

我甚至设法设置了一个正确的光标和..回到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); // < ---不工作! 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

如何进行?

回答

0

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

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

或更好,使用宽度和高度propetries:

这 - >宽度+ = 100;

+0

你是对的'this-> Width',somthing happend ...虽然,我无法想象......究竟是什么......在它上面。 – NaturalDemon 2013-03-27 04:21:28