Using the Popup Object(关于html里面的POPUP对象

本文介绍如何利用浏览器的POPUP对象实现自定义的TOOLTIP提示功能。通过简单的JScript代码示例展示了创建POPUP窗口的过程,并指出其可用于实现MESSAGEBOX、TOOLTIP等功能。
很多人都想着能不能自定义一个TOOLIP,其实微软已经在MSDN的帮助里面提到了此类功能,完全可以使用POPUP对象来实现,参见MSDN地址
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.WEBDEV.v10.en/dhtml/workshop/author/om/popup_overview.htm

关于popup对象是
popup Object

----------------------------------------------------------------------
A special type of overlapped window typically used for dialog boxes, message boxes, and other temporary windows that appear separate from an application's main window.


使用的方法也挺简单
<SCRIPT LANGUAGE="JScript">
var oPopup = window.createPopup();
var oPopupBody = oPopup.document.body;

oPopupBody.innerHTML = "Display some <B>HTML</B> here.";
oPopup.show(100, 100, 200, 50, document.body);
</SCRIPT>

通过popup对像可以建立如MESSAGEGOX,TOOLTIP及自定义下拉框等,具体的可参见MSDN的帮助文档
在WPF中,让Popup控件实现自动关闭可采用以下几种方法: ### 使用StaysOpen属性 可将 `StaysOpen` 属性设为 `false`,这样当Popup控件失去焦点时就会自动关闭。以下是示例代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Show Popup" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="75"> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Duration="0:0:0"> <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> <Popup Name="myPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=myButton}" Placement="Bottom"> <Border Background="LightBlue" Padding="10"> <TextBlock Text="This is a Popup!"/> </Border> </Popup> </Grid> </Window> ``` 在上述代码里,`StaysOpen="False"` 让Popup在失去焦点时自动关闭。 ### 利用定时器 借助 `DispatcherTimer` 设定一个定时器,在特定时间后关闭Popup。示例代码如下: ```csharp using System; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; namespace WpfApp1 { public partial class MainWindow : Window { private DispatcherTimer timer; public MainWindow() { InitializeComponent(); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(5); // 设置5秒后关闭 timer.Tick += Timer_Tick; } private void Button_Click(object sender, RoutedEventArgs e) { myPopup.IsOpen = true; timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { myPopup.IsOpen = false; timer.Stop(); } } } ``` 在上述代码中,点击按钮打开Popup,同时启动定时器,5秒后定时器触发 `Timer_Tick` 事件,关闭Popup。 ### 监听外部点击事件 监听窗口的鼠标点击事件,若点击发生在Popup外部,就关闭Popup。示例代码如下: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" MouseDown="Window_MouseDown"> <Grid> <Button Content="Show Popup" Name="myButton" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> <Popup Name="myPopup" PlacementTarget="{Binding ElementName=myButton}" Placement="Bottom"> <Border Background="LightBlue" Padding="10"> <TextBlock Text="This is a Popup!"/> </Border> </Popup> </Grid> </Window> ``` ```csharp using System.Windows; using System.Windows.Input; namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { myPopup.IsOpen = true; } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { if (myPopup.IsOpen && !myPopup.Child.IsMouseOver) { myPopup.IsOpen = false; } } } } ``` 在上述代码中,监听窗口的 `MouseDown` 事件,若点击发生在Popup外部,就关闭Popup
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值