2013-03-26 37 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