图1
图2
图3
就爱UI - 分享UI设计的点点滴滴
tml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>点击弹窗遮罩</title>
</head>
<body>
<button id="btn">点击</button>
<!-- 遮罩 -->
<div class="cover" id="cover"></div>
<!-- 弹框 -->
<div class="grid" id="grid_box">
<span class="grid_close">x</span>
<h3>标题</h3>
<div>hahaha</div>
</div>
</body>
</html>
css
<style type="text/css">
.cover{
display: none;
position: fixed;
width:100%;
height:100%;
z-index:2;
background: rgba(102,102,102,0.7);
top:0;
left:0;
}
.grid{
display: none;
position: fixed;
z-index: 3;
width: 500px;
min-height: 300px;
top:50%;
left:50%;
margin: -150px 0 0 -250px;
background: #fff;
padding: 15px;
border-radius:10px;
}
.grid_close{
background: #fff;
float:right;
}
</style>
js
<script type="text/javascript">
var Btn = document.getElementById('btn'),
Cover = document.getElementById('cover'),
Box = document.getElementById('grid_box'),
GridClose = Box.getElementsByClassName('grid_close')[0];
//点击弹出弹窗
Btn.onclick =function(){
ShowHide(true,Cover,Box);
}
//点击关闭按钮关闭弹窗
GridClose.onclick=function(){
ShowHide(false,Cover,Box);
}
//点击遮罩空白关闭弹窗
Cover.onclick=function(){
ShowHide(false,Cover,Box);
}
//关闭公用方法
function ShowHide(Bool,item1,item2){
for (var i = 1,len=arguments.length; i<len; i++) {
if (Bool) {
arguments[i].style.display="block";
}else{
arguments[i].style.display="none";
}
}
}
</script>
间如流水,只能流去不流回!
点赞再看,养成习惯,这是您给我创作的动力!
本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform、WPF、ASP.NET Core、Xamarin.Forms等,亦有C++桌面相关的Qt Quick和Qt Widgets等,只分享自己熟悉的、自己会的。
阅读导航:
YouTube Design com WPF 大神处习得,常用的遮罩对话框实现,使用的开源 C# WPF控件库 MaterialDesignInXAML ,本站曾有介绍:开源C# WPF控件库《MaterialDesignInXAML》。
站长使用.Net Core 3.1创建的WPF工程,创建“ScreenOverlayMessage”解决方案后,需要添加两个Nuget库:MaterialDesignThemes和MaterialDesignColors,上图的效果是使用该控件库实现的,非常强大。
不需要截图,只修改了两个文件,App.xaml添加MD控件4个样式文件,MainWindow主窗口实现效果。
<Application x:Class="DropDownMenu.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DropDownMenu"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml,整体布局,看上图加上下面的界面代码,添加界面左上角logo图标、左侧导航菜单等,下面即是全部代码。
<Window x:Class="ScreenOverlayMessage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ScreenOverlayMessage"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" MouseDown="Window_MouseDown"
Title="MainWindow" Height="576" Width="1024" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="#FF423A3A">
<Grid>
<materialDesign:DialogHost BorderBrush="{DynamicResource MaterialDesignDivider}">
<materialDesign:DialogHost.DialogContent>
<Grid Width="300" Height="150" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" Margin="15">
<materialDesign:PackIcon Kind="Folder" Foreground="{StaticResource PrimaryHueMidBrush}" Width="50" Height="50"/>
<TextBlock Foreground="Gray" Width="200" Margin="15 5" TextWrapping="Wrap">
允许应用程序访问您计算机上的照片、媒体和文件?
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="15">
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" Margin="4" VerticalAlignment="Center">
拒绝
</Button>
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" Margin="4" VerticalAlignment="Center">
允许
</Button>
</StackPanel>
</Grid>
</materialDesign:DialogHost.DialogContent>
<Grid>
<StackPanel Width="200" HorizontalAlignment="Left" Background="#FF472076">
<Grid Height="150" Background="White">
<Image Source="https://img.dotnet9.com/logo.png"/>
</Grid>
<Button Style="{StaticResource MaterialDesignFlatButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}">
<StackPanel Orientation="Horizontal" Height="30">
<materialDesign:PackIcon Kind="PhotoAlbum" Width="20" Height="20" VerticalAlignment="Center"/>
<TextBlock Text="照片" Margin="20 0" FontSize="15" VerticalAlignment="Center"/>
</StackPanel>
</Button>
<Button Style="{StaticResource MaterialDesignFlatButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}">
<StackPanel Orientation="Horizontal" Height="30">
<materialDesign:PackIcon Kind="Music" Width="20" Height="20" VerticalAlignment="Center"/>
<TextBlock Text="音乐" Margin="20 0" FontSize="15" VerticalAlignment="Center"/>
</StackPanel>
</Button>
</StackPanel>
</Grid>
</materialDesign:DialogHost>
</Grid>
</Window>
重点讲解:
后台有个拖动窗体代码:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
建议直接打开大神视频学习,他的YouTube上还有很多代码视频哦,参考:
参考视频: Design com WPF: https://www.youtube.com/watch?v=Dwi9o3K73XM
文章中代码已经全部贴出。
除非注明,文章均由 Dotnet9 整理发布,欢迎转载。
转载请注明本文地址:https://dotnet9.com/6769.html
*请认真填写需求信息,我们会在24小时内与您取得联系。