Pergunta

In VS2008, I add a Resource Dictionary template in my solution, I named this template as "MyWinStyle.xaml". Below is the style used in my window: ......

<!--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>

this is my XMAL code, when I want to add the function to make the ownerdraw window resizable, I encounter a problem, that is, I can't get the Window Object in its constructor.(based on this article, I want to make my window resizable: http://blog.kirupa.com/?p=256) below is my code:

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();
    }
}
}
Foi útil?

Solução

Why are you using ResourceDictionary as a base class for your window? Change your base class to Window and you'll be able to subscribe to SourceInitialized event. That is:

    public partial class MyStyledWindow : Window
    {
    //other code
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top