WPF Popup:移入显示,移出隐藏

1.看一下Demo的效果;

2.直接上代码:

 MainWindow.xaml:

复制代码
 1 <Window x:Class="PopupDemo.MainWindow"
 2   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6   xmlns:local="clr-namespace:PopupDemo"
 7   mc:Ignorable="d"
 8   Title="诗词" Height="400" Width="500">
 9     <Grid>
10         <Grid>
11             <Grid.RowDefinitions>
12                 <RowDefinition Height="9*"></RowDefinition>
13                 <RowDefinition Height="1*"></RowDefinition>
14             </Grid.RowDefinitions>
15             <Grid>
16                 <Button x:Name="BtnPopup" Height="80" Width="120" Padding="2 " MouseEnter="BtnPopup_MouseEnter" MouseLeave= "BtnPopup_MouseLeave" >
17                     <Grid >
18                         <TextBlock>杜甫《绝句》</TextBlock>
19                         <local:PopupNonTopmost x:Name="pop1" StaysOpen="True" HorizontalOffset="0" VerticalOffset="0" 
20                         PlacementTarget="{Binding ElementName=BtnPopup}" MouseLeave="pop1_MouseLeave">
21                             <Grid Height="120" Width="150">
22                                 <Border BorderThickness="1" Background="White" BorderBrush="#015eea">
23                                     <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
24                                         <TextBlock Margin="5" Text="两只黄鹂鸣翠柳" TextDecorations="Underline" />
25                                         <TextBlock Margin="5" Text="一行白鹭上青天" TextDecorations="Underline" />
26                                         <TextBlock Margin="5" Text="窗含西岭千秋雪" TextDecorations="Underline" />
27                                         <TextBlock Margin="5" Text="门泊东吴万里船" TextDecorations="Underline" />
28                                     </StackPanel>
29                                 </Border>
30                             </Grid>
31                         </local:PopupNonTopmost>
32                     </Grid>
33                 </Button>
34             </Grid>
35             <Grid Grid.Row="1">
36                 <Label Name="label1" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Calibri" Content="https://www.cnblogs.com/yellow3gold/"
37             FontSize="14" FontWeight="Bold" FontStyle="Italic" Foreground="Red"></Label>
38             </Grid>
39         </Grid>
40     </Grid>
41 </Window>
复制代码

MainWindow.xaml.cs

复制代码
 1 using System;
 2 using System.Windows;
 3 using System.Windows.Input;
 4 
 5 namespace PopupDemo
 6 {
 7     /// <summary>
 8     /// MainWindow.xaml 的交互逻辑
 9     /// </summary>
10     public partial class MainWindow : Window
11     {
12         public MainWindow()
13         {
14             InitializeComponent();
15         }
16 
17         private void pop1_MouseLeave(object sender, MouseEventArgs e)
18         {
19             pop1.IsOpen = false;
20         }
21 
22         private void BtnPopup_MouseEnter(object sender, MouseEventArgs e)
23         {
24             pop1.IsOpen = false;
25             pop1.IsOpen = true;
26         }
27 
28         private void BtnPopup_MouseLeave(object sender, MouseEventArgs e)
29         {
30             var point = Mouse.GetPosition(pop1);
31             if (point.X < 0 || point.Y < 0)
32                 pop1.IsOpen = false;
33             if (point.X > BtnPopup.Height && point.Y > 1)
34                 pop1.IsOpen = false;
35         }
36     }
37 }
复制代码

PopupNonTopmost.cs

复制代码
 1 using System;
 2 using System.Runtime.InteropServices;
 3 using System.Windows;
 4 using System.Windows.Controls.Primitives;
 5 using System.Windows.Interop;
 6 
 7 namespace PopupDemo
 8 {
 9     public class PopupNonTopmost : Popup
10     {
11         public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(
12           typeof(PopupNonTopmost),
13           new FrameworkPropertyMetadata(false, OnTopmostChanged));
14 
15         public bool Topmost
16         {
17             get { return (bool)GetValue(TopmostProperty); }
18             set { SetValue(TopmostProperty, value); }
19         }
20 
21         private static void OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
22         {
23             (obj as PopupNonTopmost).UpdateWindow();
24         }
25 
26         protected override void OnOpened(EventArgs e)
27         {
28             UpdateWindow();
29         }
30 
31         private void UpdateWindow()
32         {
33             var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
34             RECT rect;
35 
36             if (GetWindowRect(hwnd, out rect))
37             {
38                 SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
39             }
40         }
41 
42         #region P/Invoke imports & definitions
43 
44         [StructLayout(LayoutKind.Sequential)]
45         public struct RECT
46         {
47             public int Left;
48             public int Top;
49             public int Right;
50             public int Bottom;
51         }
52 
53         [DllImport("user32.dll")]
54         [return: MarshalAs(UnmanagedType.Bool)]
55         private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
56 
57         [DllImport("user32", EntryPoint = "SetWindowPos")]
58         private static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
59 
60         #endregion
61     }
62 }
复制代码

 

posted @   新*  阅读(730)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示