WPF 建立自訂表單

來源:互聯網
上載者:User

標籤:alt   review   mini   enter   temp   foreach   break   mes   template   

在前面的一篇部落格"WPF 自訂Metro Style表單",展示了如何建立一個類似於Metro Style的Window,並在程式中使用。但是這個表單不能夠自由的改變大小。今天的部落格中將展示如何建立一個可以通過拖拽來改變大小的Metro Style表單。

實現思路,在Windows ControlTemplate中增加8個背景透明Rectangle,分別放置於Left, Right, Top, TopLeft, TopRight, Bottom, BottomLeft, BottomRight這8個位置,

XAML:

    <ControlTemplate x:Key="MetroWindowControlTemplate" TargetType="{x:Type Window}">        <Border BorderThickness="1" BorderBrush="LightBlue" Background="White">            <Grid>                <Grid>                    <Grid.RowDefinitions>                        <RowDefinition Height="Auto"/>                        <RowDefinition />                    </Grid.RowDefinitions>                    <Grid.ColumnDefinitions>                        <ColumnDefinition />                        <ColumnDefinition Width="Auto"/>                    </Grid.ColumnDefinitions>                    <Rectangle x:Name="MoveableRectangle" Fill="LightGray" Grid.Row="0" Grid.Column="0"/>                    <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Background="LightGray">                        <Button x:Name="MinimizeButton" Style="{StaticResource WindowButtonStyle}" Content="0" />                        <Button x:Name="RestoreButton" Style="{StaticResource WindowButtonStyle}" Content="1" />                        <Button x:Name="CloseButton" Style="{StaticResource WindowButtonStyle}" Content="r" />                    </StackPanel>                    <Grid Background="{TemplateBinding Background}" Grid.Row="1" Grid.ColumnSpan="2" Margin="5,5,5,5">                        <AdornerDecorator>                            <ContentPresenter/>                        </AdornerDecorator>                    </Grid>                </Grid>                <Grid x:Name="ResizeGrid">                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            VerticalAlignment="Top"                            Height="5"                            x:Name="Top"                            Margin="5,0,5,0" />                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            x:Name="Bottom"                            Height="5"                            VerticalAlignment="Bottom"                            Margin="5,0,5,0" />                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            HorizontalAlignment="Left"                            Margin="0,5,0,5"                            Width="5"                            x:Name="Left"/>                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            Margin="0,5,0,5"                            Width="5"                            HorizontalAlignment="Right"                            x:Name="Right" />                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            HorizontalAlignment="Left"                            VerticalAlignment="Bottom"                            Width="5"                            Height="5"                            x:Name="BottomLeft" />                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            VerticalAlignment="Bottom"                            Height="5"                            Width="5"                            HorizontalAlignment="Right"                            x:Name="BottomRight" />                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            HorizontalAlignment="Right"                            Width="5"                            Height="5"                            VerticalAlignment="Top"                            x:Name="TopRight" />                    <Rectangle                            Stroke="{x:Null}"                            Fill="Transparent"                            HorizontalAlignment="Left"                            Width="6"                            VerticalAlignment="Top"                            Height="5"                            x:Name="TopLeft" />                </Grid>            </Grid>        </Border>    </ControlTemplate>

C#:

    ControlTemplate template = App.Current.FindResource("MetroWindowControlTemplate") as ControlTemplate;    if(template != null)    {        //....        Grid resizeGrid = template.FindName("ResizeGrid", this) as Grid;        if(resizeGrid != null)        {            foreach (UIElement element in resizeGrid.Children)            {                Rectangle resizeRectangle = element as Rectangle;                if (resizeRectangle != null)                {                    resizeRectangle.PreviewMouseDown += ResizeRectangle_PreviewMouseDown;                    resizeRectangle.MouseMove += ResizeRectangle_MouseMove;                }            }        }    }                private void ResizeRectangle_PreviewMouseDown(object sender, MouseButtonEventArgs e)        {            Rectangle rectangle = sender as Rectangle;            if(rectangle != null)            {                switch(rectangle.Name)                {                    case "Top":                        Cursor = Cursors.SizeNS;                        ResizeWindow(ResizeDirection.Top);                        break;                    case "Bottom":                        Cursor = Cursors.SizeNS;                        ResizeWindow(ResizeDirection.Bottom);                        break;                    case "Left":                        Cursor = Cursors.SizeWE;                        ResizeWindow(ResizeDirection.Left);                        break;                    case "Right":                        Cursor = Cursors.SizeWE;                        ResizeWindow(ResizeDirection.Right);                        break;                    case "TopLeft":                        Cursor = Cursors.SizeNWSE;                        ResizeWindow(ResizeDirection.TopLeft);                        break;                    case "TopRight":                        Cursor = Cursors.SizeNESW;                        ResizeWindow(ResizeDirection.TopRight);                        break;                    case "BottomLeft":                        Cursor = Cursors.SizeNESW;                        ResizeWindow(ResizeDirection.BottomLeft);                        break;                    case "BottomRight":                        Cursor = Cursors.SizeNWSE;                        ResizeWindow(ResizeDirection.BottomRight);                        break;                    default:                        break;                }            }        }        private void ResizeRectangle_MouseMove(object sender, MouseEventArgs e)        {            Rectangle rectangle = sender as Rectangle;            if (rectangle != null)            {                switch (rectangle.Name)                {                    case "Top":                        Cursor = Cursors.SizeNS;                        break;                    case "Bottom":                        Cursor = Cursors.SizeNS;                        break;                    case "Left":                        Cursor = Cursors.SizeWE;                        break;                    case "Right":                        Cursor = Cursors.SizeWE;                        break;                    case "TopLeft":                        Cursor = Cursors.SizeNWSE;                        break;                    case "TopRight":                        Cursor = Cursors.SizeNESW;                        break;                    case "BottomLeft":                        Cursor = Cursors.SizeNESW;                        break;                    case "BottomRight":                        Cursor = Cursors.SizeNWSE;                        break;                    default:                        break;                }            }        }                [DllImport("user32.dll", CharSet = CharSet.Auto)]        private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);        private void ResizeWindow(ResizeDirection direction)        {            SendMessage(_hwndSource.Handle, 0x112, (IntPtr)(61440 + direction), IntPtr.Zero);        }

到此為止,就實現了一個可以拖拽改變大小的自訂表單。

運行效果:

感謝您的閱讀,代碼點擊這裡下載。

WPF 建立自訂表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.