• 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 }
  • 相关阅读:
    每个Java开发人员都应该知道的10个基本工具
    2019年让程序员崩溃的 60 个瞬间,笑死我了
    面试官:服务器安装 JDK 还是 JRE?可以只安装 JRE 吗?
    腾讯工作近十年大佬:不是我打击你!你可能真的不会写Java
    作为Java开发人员不会饿死的5个理由
    趣味算法:国王和100个囚犯
    阿里第二轮面试:手写Java二叉树
    Linux软件安装——服务管理
    Linux帮助——重要文件
    Linux帮助——常用命令
  • 原文地址:https://www.cnblogs.com/yellow3gold/p/15900974.html
Copyright © 2020-2023  润新知