Organize your recent WPF little knowledge.
1, Windowstyle=none, no border window drag:
This method is the most perfect and concise.
Adds a MouseLeftButtonDown event to the custom title bar and writes this to the event. DragMove ();
Code:
The code is as follows |
Copy Code |
<border x:name= "Logo1" horizontalalignment= "left" height= "margin=" 0,0,-640,0 "mouseleftbuttondown=" logo1_ MouseLeftButtonDown " Verticalalignment= "Top" width= "1272" > <Border.Background> <lineargradientbrush endpoint= "0.5,1" startpoint= "0.5,0" > <gradientstop color= "#FF0D61A1" offset= "0"/> <gradientstop color= "#FF02A0EA" offset= "1"/> </LinearGradientBrush> </Border.Background> <label x:name= "logo" content= "The Choice and task" horizontalalignment= "left" height= "margin=" and "20,10,0,0" verticalalignment= "Top" width= "style=" {DynamicResource logolable} "> <Label.Background> <imagebrush imagesource= "Themes/zcthemes/skin/icon/icon_case.png" stretch= "Uniform"/> </Label.Background> </Label> </Border> private void Logo1_mouseleftbuttondown (object sender, MouseButtonEventArgs e) { This. DragMove (); } |
2. No zoom (zoom Out) window
Only need to set
Resizemode= "Noresize"
3, WPF window Adaptive: take advantage of the Viewbox container space in WPF. A viewbox is a container space that automatically scales the child space in the container to fill itself, and it can have only one child control. However, we can use the canvas control as a child control of the Viewbox control. The other controls are then laid out in the canvas control.
Code:
The code is as follows |
Copy Code |
<window x:class= "Window1" Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" Title= "Wpftest" Mc:ignorable= "D" Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008" Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006" Sizetocontent= "Manual" width= "height=" > <dockpanel name= "DockPanel1" lastchildfill= "True" > <menu height= name= "Menu1" dockpanel.dock= "Top" > <menuitem header= "Menu1" > <menuitem header= "H1"/> <menuitem header= "H2"/> </MenuItem> <menuitem header= "MENU2" > <menuitem header= "L1"/> <menuitem header= "L2"/> <separator/> <menuitem header= "L4"/> </MenuItem> </Menu> <statusbar height= "Name=" "StatusBar1" dockpanel.dock= "Bottom" > <statusbaritem content= "S1" name= "S1"/> <statusbaritem content= "S2" name= "S2"/> <statusbaritem content= "S3" name= "S3"/> </StatusBar> <viewbox name= "Viewbox1" stretch= "Fill" > <canvas height= "name=" Canvas1 "width=" background= "#FF8EDE75" > <button canvas.left= "canvas.top=" content= "button" height= "Notoginseng" name= "Button1" width= "the"/> </Canvas> </Viewbox> </DockPanel> </Window> |
Description
The outermost is the window container, which sets the title (wpftest) and size (400x300), and it can only have one child control.
Window's child controls are DockPanel containers and are automatically docked container controls. Set lastchildfill= "True" to indicate that the last child control automatically fills the remaining space. The size is not set, and the default size is the client area size of window.
The DockPanel control has three child controls
Menu control: Menus control, automatically docked at the top of the container
StatusBar Control: Status bar control, automatically docked at the bottom of the container
Viewbox control: A container control that automatically populates DockPanel remaining controls. No size is set, the size of the fill. Sets the fill pattern to fill, which indicates the size of the container that the child control fills itself
A canvas control is placed in the Viewbox, setting the size (note: Be sure to set the size, otherwise the default will produce a variety of magical effects), set the background color
A button control was placed in the canvas. Just an example, other controls can also be placed in canvas
4, remove the maximum minimization of the belt. Maximize customization and minimize the size of your customizations.
Masks the properties windowstyle= "None" for the maximum, minimum, and Shutdown Settings window with the WPF windows
Customize your own buttons:
The code is as follows |
Copy Code |
<button content= "minimize" horizontalalignment= "left" verticalalignment= "top" click= "Windowsmin_" Click ' style= ' {DynamicResource buttonlist} ' <button.background> <imagebrush imagesource= ' Themes/zcthemes/skin/btn/icon_min.png "stretch=" None "/> </button.background> </button> <button content= "Exit" horizontalalignment= "left" click= "Windwowsclose_click" verticalalignment= "Top" style= "{DynamicResource buttonlist}" margin= "10,0,0,0" <button.background> <imagebrush Imagesource= "Themes/zcthemes/skin/btn/icon_quit.png" stretch= "None"/> </button.background> </ BUTTON> Background event code: private void Windowsmin_click (object sender, RoutedEventArgs e) { This. WindowState = System.Windows.WindowState.Minimized; } private void Windwowsclose_click (object sender, RoutedEventArgs e) { This. Close (); if (MessageBox.Show (OK) exit assistant? ", "Confirm Exit", messageboxbutton.yesno) = = Messageboxresult.yes) { Close the window Application.Current.Shutdown (); } Else { Don't close the window }
} |
Note: WPF close is not able to shut down the process. Need to use Application.Current.Shutdown ();