2012-07-25 133 views
-1

在VS2008中,我在我的解決方案中添加了資源字典模板,我將該模板命名爲「MyWinStyle.xaml」。 下面是我的窗口所使用的樣式: ...... 在WPF ResourceDictionary窗口中獲取窗口對象

<!--Window Template--> 
<ControlTemplate x:Key="MyWindowTemplate" TargetType="{x:Type Window}"> 
..... 
</ControlTemplate> 

<!--Window Style--> 
<Style x:Key="MacWindowStyle" TargetType="Window"> 
    .... 
    <Setter Property="Template" Value="{StaticResource MyWindowTemplate}" /> 
</Style> 

</ResourceDictionary> 

這是我的XMAL代碼,當我想要添加的功能,使的OwnerDraw窗口大小可調,我遇到。一個問題,那就是,我不能讓窗口對象在其構造函數(根據這篇文章,我想讓我的窗口大小可調:http://blog.kirupa.com/?p=256)以下 是我的代碼:

public partial class MyStyledWindow : ResourceDictionary 
{ 
    private const int WM_SYSCOMMAND = 0x112; 
    private HwndSource hwndSource; 
    IntPtr retInt = IntPtr.Zero; 

    public MacStyledWindow() 
    { 
     InitializeComponent(); 
     /** 
     **Would anyone suggest on this? I can't get window object 
     **/ 
Window.SourceInitialized += new EventHandler(MainWindow_SourceInitialized); 
    } 

    private void MainWindow_SourceInitialized(object sender, EventArgs e) 
    { 
     hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource; 
     hwndSource.AddHook(new HwndSourceHook(WndProc)); 
    } 

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 
    { 
     Debug.WriteLine("WndProc messages: " + msg.ToString()); 
     // 
     // Check incoming window system messages 
     // 
     if (msg == WM_SYSCOMMAND) 
     { 
      Debug.WriteLine("WndProc messages: " + msg.ToString()); 
     } 

     return IntPtr.Zero; 
    } 

    public enum ResizeDirection 
    { 
     Left = 1, 
     Right = 2, 
     Top = 3, 
     TopLeft = 4, 
     TopRight = 5, 
     Bottom = 6, 
     BottomLeft = 7, 
     BottomRight = 8, 
    } 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 

    private void ResizeWindow(ResizeDirection direction) 
    { 
     SendMessage(hwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero); 
    } 

    private void ResetCursor(object sender, MouseEventArgs e) 
    { 
     var window = (Window)((FrameworkElement)sender).TemplatedParent; 
     if (Mouse.LeftButton != MouseButtonState.Pressed) 
     { 
      window.Cursor = Cursors.Arrow; 
     } 
    } 

    private void Resize(object sender, MouseButtonEventArgs e) 
    { 
     Rectangle clickedRectangle = sender as Rectangle; 
     var window = (Window)((FrameworkElement)sender).TemplatedParent; 
     switch (clickedRectangle.Name) 
     { 
      case "top": 
       window.Cursor = Cursors.SizeNS; 
       ResizeWindow(ResizeDirection.Top); 
       break; 
      case "bottom": 
       window.Cursor = Cursors.SizeNS; 
       ResizeWindow(ResizeDirection.Bottom); 
       break; 
      case "left": 
       window.Cursor = Cursors.SizeWE; 
       ResizeWindow(ResizeDirection.Left); 
       break; 
      case "right": 
       window.Cursor = Cursors.SizeWE; 
       ResizeWindow(ResizeDirection.Right); 
       break; 
      case "topLeft": 
       window.Cursor = Cursors.SizeNWSE; 
       ResizeWindow(ResizeDirection.TopLeft); 
       break; 
      case "topRight": 
       window.Cursor = Cursors.SizeNESW; 
       ResizeWindow(ResizeDirection.TopRight); 
       break; 
      case "bottomLeft": 
       window.Cursor = Cursors.SizeNESW; 
       ResizeWindow(ResizeDirection.BottomLeft); 
       break; 
      case "bottomRight": 
       window.Cursor = Cursors.SizeNWSE; 
       ResizeWindow(ResizeDirection.BottomRight); 
       break; 
      default: 
       break; 
     } 
    } 

    private void DisplayResizeCursor(object sender, MouseEventArgs e) 
    { 
     Rectangle clickedRectangle = sender as Rectangle; 
     var window = (Window)((FrameworkElement)sender).TemplatedParent; 
     switch (clickedRectangle.Name) 
     { 
      case "top": 
       window.Cursor = Cursors.SizeNS; 
       break; 
      case "bottom": 
       window.Cursor = Cursors.SizeNS; 
       break; 
      case "left": 
       window.Cursor = Cursors.SizeWE; 
       break; 
      case "right": 
       window.Cursor = Cursors.SizeWE; 
       break; 
      case "topLeft": 
       window.Cursor = Cursors.SizeNWSE; 
       break; 
      case "topRight": 
       window.Cursor = Cursors.SizeNESW; 
       break; 
      case "bottomLeft": 
       window.Cursor = Cursors.SizeNESW; 
       break; 
      case "bottomRight": 
       window.Cursor = Cursors.SizeNWSE; 
       break; 
      default: 
       break; 
     } 
    } 

    private void Drag(object sender, MouseButtonEventArgs e) 
    { 
     var window = (Window)((FrameworkElement)sender).TemplatedParent; 
     window.DragMove(); 
    } 
} 
} 

回答

1

爲什麼ÿ你使用ResourceDictionary作爲你的窗口的基類嗎?將您的基類更改爲Window,並且您將能夠訂閱SourceInitialized事件。那是:

public partial class MyStyledWindow : Window 
    { 
    //other code 
+0

我知道,但我所有的窗口(約30)將使用相同的風格,做你說的也許讓我發瘋。 – CharlieShi 2012-07-25 09:51:35

+2

您將只有一個類用於所有窗口(MyStyledWindow),然後使用MyStyledWindow而不是常見的Window類。在你的控件模板和樣式聲明中,使用'TargetType =「{x:Type MyStyledWindow}」'將它們應用到你的MyStyledWindow。看看這裏如何實現自定義窗口:http://wpfwindow.codeplex.com/。希望這可以幫助 – Alexander 2012-07-25 11:08:25