2012-08-12 64 views
1

我一直在尋找小時,沒有發現任何有用的,因爲所有其他問題都過於本地化,所以這裏是我的版本(通用)的問題:力調整透明WPF窗口與自定義鑲邊

我有WindowStyle =無WPF窗口ResizeMode = NoResize(否則我會得到調整,我不想邊界)和最重要的AllowsTransparency =必須堅持這一設置。

更改ResizeModeCanResize並具有自定義調整握以下的MouseDown處理程序

[DllImport("User32.dll")] 
public static extern bool ReleaseCapture(); 

[DllImport("User32.dll")] 
public static extern IntPtr SendMessage(
    IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam); 

private void OnResizeGripMouseDown(object sender, MouseButtonEventArgs e) { 
    ReleaseCapture(); 
    SendMessage(Window.Handle, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, 0); 
} 

讓我完全改變窗口大小,但有調整大小邊界離開。有沒有辦法強制窗口的可調整性,儘管它有ResizeMode = NoResize

(也許通過SetWindowLongGWL_EXSTYLE?如果在需要的消息,我已經有一個WindowProc設置來處理這個問題。)

回答

3

我可以通過其他消息

迫使期望的行爲
SendMessage(Handle, WM_SYSCOMMAND, SC_SIZE + direction, 0); 

其中WM_SYSCOMMAND是默認的Windows消息,SC_SIZE是由0xF000的定義的wParam和方向是命中測試手柄通過該枚舉所定義的數值

public enum SysCommandSize : int { 
    SC_SIZE_HTLEFT = 1, 
    SC_SIZE_HTRIGHT = 2, 
    SC_SIZE_HTTOP = 3, 
    SC_SIZE_HTTOPLEFT = 4, 
    SC_SIZE_HTTOPRIGHT = 5, 
    SC_SIZE_HTBOTTOM = 6, 
    SC_SIZE_HTBOTTOMLEFT = 7, 
    SC_SIZE_HTBOTTOMRIGHT = 8 
} 
0

基督徒,你的解決方案給我帶來了正確的方式,但它缺乏像我一樣愚蠢和懶惰的複製粘貼代碼! 在這裏,我們去,如果你取消註釋的評論部分,你可以移動除了邊界周圍的窗口。

private void Action_LMouseDownAndMove(object sender, MouseEventArgs e) 
    { 
     Point mousePosition = this.PointToClient(System.Windows.Forms.Cursor.Position); 
     const int WM_NCLBUTTONDOWN = 0xA1; 
     //const int HT_CAPTION = 0x2; 
     if (e.Button == MouseButtons.Left) 
     { 
      ReleaseCapture(); 
      if (mousePosition.X < 20) 
      { 
       if (mousePosition.Y < 20) 
        SendMessage(Handle, WM_NCLBUTTONDOWN, 13, 0); 
       else if (mousePosition.Y > this.Size.Height - 20) 
        SendMessage(Handle, WM_NCLBUTTONDOWN, 16, 0); 
       else 
        SendMessage(Handle, WM_NCLBUTTONDOWN, 10, 0); 
      } 
      else if (mousePosition.X > this.Size.Width - 20) 
      { 
       if (mousePosition.Y < 20) 
        SendMessage(Handle, WM_NCLBUTTONDOWN, 14, 0); 
       else if (mousePosition.Y > this.Size.Height - 20) 
        SendMessage(Handle, WM_NCLBUTTONDOWN, 17, 0); 
       else 
        SendMessage(Handle, WM_NCLBUTTONDOWN, 11, 0); 
      } 
      else if (mousePosition.Y < 20) 
       SendMessage(Handle, WM_NCLBUTTONDOWN, 12, 0); 
      else if (mousePosition.Y > this.Size.Height - 20) 
       SendMessage(Handle, WM_NCLBUTTONDOWN, 15, 0); 
      //else 
      // SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
     } 

根據您的喜好更改20像素值。我不是100%肯定左/右和上/下具有完全相同的調整大小區域,或者如果你知道我的意思,是否需要19/21 ... if/else樹有一些減少的可能性, 我知道。而不是20我應該使用一個常數。

要更改光標顯示他可以調整我使用下面的代碼的用戶,它只是一個MouseMove事件處理程序:

 this.pictureBox.MouseMove += new MouseEventHandler((a, e) => 
     { 
      Point h = this.PointToClient(System.Windows.Forms.Cursor.Position); 

      if (h.X < 20) 
      { 
       if (h.Y < 20) 
       { 
        pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNWSE; 
       } 
       else if (h.Y > this.Size.Height - 20) 
       { 
        pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNESW; 
       } 
       else 
       { 
        pictureBox.Cursor = System.Windows.Forms.Cursors.SizeWE; 
       } 
      } 
      else if (h.X > this.Size.Width - 20) 
      { 
       if (h.Y < 20) 
       { 
        pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNESW; 
       } 
       else if (h.Y > this.Size.Height - 20) 
       { 
        pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNWSE; 
       } 
       else 
       { 
        pictureBox.Cursor = System.Windows.Forms.Cursors.SizeWE; 
       } 
      } 
      else if (h.Y < 20) 
      { 
       pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNS; 
      } 
      else if (h.Y > this.Size.Height - 20) 
      { 
       pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNS; 
      } 
      else 
      { 
       pictureBox.Cursor = System.Windows.Forms.Cursors.Default; 
      } 
     });