WPF中自定义窗体标题栏

最新文章:Virson's Blog

这个例子是在看《深入浅出WPF》第5章控件与布局的Canvas控件时,对书上的例子做了一下小扩展;在此记下,以备后用:

XAML代码:

 1 <Window x:Class="TestCanvas.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="登录" Height="110px" Width="300px" WindowStartupLocation="CenterScreen" WindowStyle="None" 
 5         MaxHeight="110px" MaxWidth="300px" MinHeight="110px" MinWidth="300px" MouseMove="Mouse_moveHandler"> 
 6     <Canvas>
 7         <TextBlock Text="用户名:" Canvas.Left="12px" Canvas.Top="12px"/>
 8         <TextBox Height="23px" Width="200px" BorderBrush="Black" Canvas.Left="66px" Canvas.Top="9px"/>
 9         <TextBlock Text="密码:" Canvas.Left="12px" Canvas.Top="40.72px" Height="16px" Width="36px"/>
10         <TextBox Height="23px" Width="200px" BorderBrush="Black" Canvas.Left="66px" Canvas.Top="38px"/>
11         <Button Content="登录" Width="80px" Height="23px" Canvas.Left="66" Canvas.Top="67px"/>
12         <Button Content="退出" Width="80px" Height="23px" Canvas.Left="186" Canvas.Top="67px" Click="Close_clickHandler"/>
13     </Canvas>
14 </Window>

CSharp代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.InteropServices;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 using System.Windows.Controls;
 9 using System.Windows.Data;
10 using System.Windows.Documents;
11 using System.Windows.Input;
12 using System.Windows.Media;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Navigation;
15 using System.Windows.Shapes;
16 
17 namespace TestCanvas
18 {
19     /// <summary>
20     /// MainWindow.xaml 的交互逻辑
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         public MainWindow()
25         {
26             InitializeComponent();
27         }
28 
29         public void Close_clickHandler(Object sender, RoutedEventArgs e)
30         {
31             this.Close();
32         }
33 
34         private void Mouse_moveHandler(object sender, MouseEventArgs e)
35         {
36             if (e.LeftButton == MouseButtonState.Pressed&& e.Source == this) this.DragMove();
37         }
38     }
39 }

 

posted @ 2013-01-17 23:15  学海无涯1999  阅读(7944)  评论(0编辑  收藏  举报