WPF学习笔记“窗口”三:入门

  

View Code
 1 <Window x:Class="Windows.ModernWindow"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     Title="ModernWindow" Height="300" Width="300"
 5         AllowsTransparency="True" Background="Transparent"
 6         WindowStyle="None" ResizeMode="CanResizeWithGrip">
 7   <Border Width="Auto" Height="Auto" Name="windowFrame"
 8     
 9           BorderBrush="#395984"
10           BorderThickness="1"
11           CornerRadius="0,20,30,40" >
12     <Border.Background>
13       <LinearGradientBrush >
14         <GradientBrush.GradientStops>
15           <GradientStopCollection>
16             <GradientStop Color="#E7EBF7" Offset="0.0"/>
17             <GradientStop Color="#CEE3FF" Offset="0.5"/>
18   
19           </GradientStopCollection>
20         </GradientBrush.GradientStops>
21       </LinearGradientBrush>
22     </Border.Background>
23     <Grid>
24 
25       <Grid.RowDefinitions>
26         <RowDefinition Height="Auto"></RowDefinition>
27         <RowDefinition></RowDefinition>
28         <RowDefinition Height="Auto"></RowDefinition>
29       </Grid.RowDefinitions>
30 
31       <TextBlock Text="Title Bar" Margin="1" Padding="5" MouseLeftButtonDown="titleBar_MouseLeftButtonDown"></TextBlock>
32         <Grid Background="#B5CBEF" Grid.Row="1">
33           <Grid.RowDefinitions>
34             <RowDefinition></RowDefinition>
35             <RowDefinition></RowDefinition>
36           </Grid.RowDefinitions>
37 
38           <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="20">Content Goes Here</TextBlock>
39           <Button VerticalAlignment="Top" HorizontalAlignment="Center" Padding="10" Grid.Row="1" Click="cmdClose_Click">Close</Button>
40           </Grid>
41 
42       <TextBlock HorizontalAlignment="Center" Grid.Row="2" Text="Footer" Margin="1,10,1,1" Padding="5"></TextBlock>
43 
44       <!--<Rectangle Grid.RowSpan="3"
45        Cursor="SizeWE" Fill="Transparent" Width="5" VerticalAlignment="Top" HorizontalAlignment="Stretch"
46                  MouseLeftButtonDown="window_initiateWiden"
47                  MouseLeftButtonUp="window_endWiden"
48                  MouseMove="window_Widen"></Rectangle>-->
49 
50       </Grid>
51     
52     
53   </Border>
54 </Window>
View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Windows;
  5 using System.Windows.Controls;
  6 using System.Windows.Data;
  7 using System.Windows.Documents;
  8 using System.Windows.Input;
  9 using System.Windows.Media;
 10 using System.Windows.Media.Imaging;
 11 using System.Windows.Shapes;
 12 using System.Windows.Interop;
 13 
 14 namespace Windows
 15 {
 16     /// <summary>
 17     /// Interaction logic for OfficeWindow.xaml
 18     /// </summary>
 19 
 20     public partial class ModernWindow : System.Windows.Window
 21     {
 22 
 23         public ModernWindow()
 24         {
 25             this.SourceInitialized += new EventHandler(Window1_SourceInitialized);
 26             InitializeComponent();
 27         }
 28 
 29         void Window1_SourceInitialized(object sender, EventArgs e)
 30         {
 31 
 32             HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
 33 
 34             hwndSource.AddHook(new HwndSourceHook(WndProc));
 35 
 36         }
 37         private const int WM_NCHITTEST = 0x0084;
 38 
 39         private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
 40         {
 41 
 42             if (msg == WM_NCHITTEST)
 43             {
 44 
 45                 int xPos = GET_X_LPARAM(lParam.ToInt32());
 46 
 47                 int yPos = GET_Y_LPARAM(lParam.ToInt32());
 48 
 49                 double xIn = xPos - this.Left;
 50 
 51                 double yIn = yPos - this.Top;
 52 
 53                 System.Diagnostics.Debug.WriteLine("x:" + xIn.ToString() + " y:" + yIn.ToString());
 54 
 55                 if (xIn < 2)
 56                 {
 57 
 58                     handled = true;
 59 
 60                     return new IntPtr((int)HitTest.HTLEFT);
 61 
 62                 }
 63 
 64             }
 65 
 66             return IntPtr.Zero;
 67 
 68         }
 69 
 70         public static Int32 GET_X_LPARAM(int lParam)
 71         {
 72 
 73             return (lParam & 0xffff);
 74 
 75         }
 76 
 77         public static Int32 GET_Y_LPARAM(int lParam)
 78         {
 79 
 80             return (lParam >> 16);
 81 
 82         }
 83 
 84         public enum HitTest
 85         {
 86 
 87             HTERROR = -2,
 88 
 89             HTTRANSPARENT = -1,
 90 
 91             HTNOWHERE = 0,
 92 
 93             HTCLIENT = 1,
 94 
 95             HTCAPTION = 2,
 96 
 97             HTSYSMENU = 3,
 98 
 99             HTGROWBOX = 4,
100 
101             HTSIZE = HTGROWBOX,
102 
103             HTMENU = 5,
104 
105             HTHSCROLL = 6,
106 
107             HTVSCROLL = 7,
108 
109             HTMINBUTTON = 8,
110 
111             HTMAXBUTTON = 9,
112 
113             HTLEFT = 10,
114 
115             HTRIGHT = 11,
116 
117             HTTOP = 12,
118 
119             HTTOPLEFT = 13,
120 
121             HTTOPRIGHT = 14,
122 
123             HTBOTTOM = 15,
124 
125             HTBOTTOMLEFT = 16,
126 
127             HTBOTTOMRIGHT = 17,
128 
129             HTBORDER = 18,
130 
131             HTREDUCE = HTMINBUTTON,
132 
133             HTZOOM = HTMAXBUTTON,
134 
135             HTSIZEFIRST = HTLEFT,
136 
137             HTSIZELAST = HTBOTTOMRIGHT,
138 
139             HTOBJECT = 19,
140 
141             HTCLOSE = 20,
142 
143             HTHELP = 21,
144 
145         }
146 
147 
148 
149 
150 
151 
152 
153         bool isWiden = false;
154         private void window_initiateWiden(object sender, System.Windows.Input.MouseEventArgs e)
155         {
156             isWiden = true;
157         }
158         private void window_endWiden(object sender, System.Windows.Input.MouseEventArgs e)
159         {
160             isWiden = false;
161 
162             // Make sure capture is released.
163             Rectangle rect = (Rectangle)sender;
164             rect.ReleaseMouseCapture();
165         }
166 
167         private void window_Widen(object sender, System.Windows.Input.MouseEventArgs e)
168         {
169             Rectangle rect = (Rectangle)sender;
170             if (isWiden)
171             {                
172                 rect.CaptureMouse();
173                 double newWidth = e.GetPosition(this).X + 2;
174                 double newHeight = e.GetPosition(this).Y + 2;
175                 if (newWidth > 0)
176                 {
177                     this.Width = newWidth;
178                     this.Height = newHeight;
179                 }
180             }            
181         }
182 
183         private void titleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
184         {
185             this.DragMove();
186         }
187 
188         private void cmdClose_Click(object sender, RoutedEventArgs e)
189         {
190             this.Close();
191         }
192     }
193 }
原文地址:https://www.cnblogs.com/gengyuanchao/p/2725351.html