WPF编程怎么激活指定窗口?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF编程怎么激活指定窗口?相关的知识,希望对你有一定的参考价值。
我在尝试用VS创建一个WPF的Windows应用程序,现在做了两个程序窗口,想要在点击主界面上的某个按钮时呼出另一个窗口,类似点击登陆按钮然后跳出登陆窗口输入账号密码那样的效果(附上想要的效果视频)
参考技术A 在WPF自定义窗体样式时,窗体设置成WindowStyle="None",那么就无法在运行时没有关闭,最小化,最大化按钮。重构窗体时需要加上这三个按钮以及实现功能。看一下自定义窗体最大化,最小化,关闭效果,鼠标移动动关闭按钮时背景色为红色,表示警告关闭功能,最小化和最大化移动到按钮显示差异颜色。
关闭按钮样式代码如下:Style x:Key="SysCloseButtonStyle" TargetType="x:Type local:NbToggleButton" BasedOn="StaticResource SysButtonStyle">
<Setter Property="Command" Value="x:Static shell:SystemCommands.CloseWindowCommand"/>
<Setter Property="NbData" Value="StaticResource Icon-Close"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type local:NbToggleButton">
<Border x:Name="border"
BorderBrush="TemplateBinding BorderBrush"
BorderThickness="TemplateBinding BorderThickness"
Background="TemplateBinding Background"
CornerRadius="TemplateBinding CornerRadius"
Effect="TemplateBinding Effect"
SnapsToDevicePixels="True">
<Border x:Name="innerBorder"
Background="TemplateBinding MaskBackground"
CornerRadius="TemplateBinding CornerRadius"
SnapsToDevicePixels="True">
<Viewbox Margin="TemplateBinding Padding">
<Path Fill="TemplateBinding Foreground"
Data="TemplateBinding NbData"
Stretch="Fill"/>
</Viewbox>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Binding Path=Color.Brush_Danger1, Source=x:Static local:NbTheme.Current"/>
<Setter Property="MaskBackground" Value="Transparent"/>
<Setter Property="Foreground" Value="Binding Path=Color.Brush_Text1, Source=x:Static local:NbTheme.Current"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Binding Path=Color.Brush_Danger1, Source=x:Static local:NbTheme.Current"/>
<Setter Property="MaskBackground" Value="Binding Path=Color.Brush_Base2_OP1, Source=x:Static local:NbTheme.Current"/>
<Setter Property="Foreground" Value="Binding Path=Color.Brush_Text1, Source=x:Static local:NbTheme.Current"/>
</Trigger>
</Style.Triggers>
</Style>追问
你好,我想问的是,点击当前窗口上的按钮控件然后弹出另一个窗口,这个按钮控件里的代码应该怎么写,试过Window.Show()似乎不行,不知道是我语法错误还是什么问题
参考技术B 视频里的这个类似wps的界面是你做的么,也就是你要用wpf实现一个类似wps的软件?追问不是 我是想让我的WPF程序也像这个页面一样 单击页面上的某个控件 然后弹出指定的窗口 只是想问 单击控件-弹出窗口 这个动作中 弹出窗口的代码要怎么写
追答New Window().ShowDialog()
本回答被提问者采纳如何从进程中恢复隐藏的 wpf 窗口
【中文标题】如何从进程中恢复隐藏的 wpf 窗口【英文标题】:How to restore the hidden wpf window from process 【发布时间】:2017-10-25 11:55:35 【问题描述】:在我的 wpf 窗口中,我隐藏了窗口并在关闭时将其从任务栏中删除。
如何从正在运行的进程中激活该窗口。我尝试了很多方法,但都没有成功。
这是我激活隐藏窗口的示例代码。
private void checkIfProcessRunning()
// get the name of our process
string proc = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(proc);
// if there is more than one process...
if (processes.Length > 1)
// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us.
RzLogger.WriteLine("process are running count:" + processes.Length);
// get our process
Process p = Process.GetCurrentProcess();
int n = 0; // assume the other process is at index 0
// if this process id is OUR process ID...
if (processes[0].Id == p.Id)
RzLogger.WriteLine("other process are at 1");
// then the other process is at index 1
n = 1;
// get the window handle
IntPtr hWnd = processes[n].MainWindowHandle;
RzLogger.WriteLine("main handle is:" + hWnd);
// if iconic, we need to restore the window
if (IsIconic(hWnd))
RzLogger.WriteLine("is minimized");
ShowWindowAsync(hWnd, SW_SHOWNORMAL);
ShowWindowAsync(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
// bring it to the foreground
SetForegroundWindow(hWnd);
// exit our process
Application.Current.Shutdown();
return;
// ... continue with your application initialization here.
问题是我总是处理为 0。
有没有办法做到这一点?而且我不想在任务栏中显示任何内容
【问题讨论】:
您是在谈论您自己进程中的 WPF 窗口吗?或者您要恢复哪些窗口? 您可以使用进程间消息连接到您的其他实例并告诉它取消隐藏 - pipe messageing on msdn。本质上,您创建了一个侦听某个名称的管道服务器,如果您的应用程序重新启动,您使用全局命名互斥锁来检查它是否已启动 - 如果是,您会触发“取消隐藏”消息并终止自己。通过使用全局互斥锁,您根本不需要查询进程列表。 @mm8 是 WPF 窗口 那你为什么不直接访问窗口呢? @mm8 很抱歉造成混淆,但问题是我的窗口被隐藏了,并且只有任务管理器中的进程。当我再次运行相同的 exe 时,我想显示第一个 exe 并关闭它。 【参考方案1】:这是一个如何使用管道的示例。
如果您以非常快的速度启动实例,除此之外使用命名互斥体可以解决一些问题(在这种情况下,您最终可能会拥有超过 1 个服务器 - 所以尽早创建一个命名互斥体,如果存在的话 - 只需发送Show() 在管道上并关闭。
从 VS2017 默认的 Wpf-Project 创建。编译它,启动 Debug-Build,转到输出文件夹并再次启动 exe 以显示它的作用。
MainWindow.xaml
<Window x:Class="interproc.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:interproc"
mc:Ignorable="d"
Title="MainWindow"
Height="350"
Width="525"
Loaded="Window_Loaded">
<Grid>
<TextBlock Name="tb"
Background="Yellow"
Text="..."
Margin="50" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.IO.Pipes;
using System.Threading;
using System.Windows.Threading;
namespace InterprocessCommunicationViaPipes
/// <summary>
/// Commands used to communiate via pipe
/// </summary>
public enum EPipeCommands : byte
None, Show, Hide, Close
;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
Title = DateTime.UtcNow.ToString("o");
/// <summary>
/// Name of the pipe used for interprocess communication
/// </summary>
private const string PipeName = "MyCoolApp";
/// <summary>
/// prevents double Close() calls
/// </summary>
private bool isClosing = false;
/// <summary>
/// Server
/// </summary>
private NamedPipeServerStream pipeServerStream = null;
/// <summary>
/// Thread server is running in
/// </summary>
private Thread ServerThread;
void ActOnPipeCommand(EPipeCommands c)
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new ThreadStart(
delegate
tb.Text += $"\nDateTime.UtcNow:o recieved c\n";
switch (c)
case EPipeCommands.None:
return;
case EPipeCommands.Hide:
Hide();
break;
case EPipeCommands.Show:
if (this.WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Visibility = Visibility.Visible;
break;
case EPipeCommands.Close when !isClosing:
Close();
break;
case EPipeCommands.Close:
tb.Text += $"Already closing.\n";
break;
default:
tb.Text += $"Unmapped pipe action: c.ToString()\n";
break;
));
/// <summary>
/// Server running?
/// </summary>
/// <returns></returns>
bool CheckIsRunning()
NamedPipeClientStream clientStream = new NamedPipeClientStream(PipeName);
try
clientStream.Connect(1000);
catch (TimeoutException)
tb.Text = $"No Server found.";
return false;
clientStream.WriteByte((byte)EPipeCommands.Show);
return true;
EPipeCommands InterpretePipeCommand(int v)
if (Enum.TryParse<EPipeCommands>($"v", out var cmd))
return cmd;
return EPipeCommands.None;
/// <summary> Creates the server, listens to connectiontrys,
/// reads 1 byte & disconnects </summary>
/// <param name="data"></param>
void PipeServer(object data)
pipeServerStream = new NamedPipeServerStream(
PipeName, PipeDirection.InOut,
2, PipeTransmissionMode.Byte);
do
pipeServerStream.WaitForConnection();
if (pipeServerStream.IsConnected && !isClosing)
ActOnPipeCommand(
InterpretePipeCommand(
pipeServerStream.ReadByte()));
pipeServerStream.Disconnect();
while (!isClosing);
private void Window_Loaded(object sender, RoutedEventArgs e)
if (CheckIsRunning())
Close();
else
ServerThread = new Thread(PipeServer);
ServerThread.Start();
tb.Text = "Starting new pipe server.\n";
Closing += (a, b) => isClosing = true;
【讨论】:
以上是关于WPF编程怎么激活指定窗口?的主要内容,如果未能解决你的问题,请参考以下文章
WPF编程,我上层容器是dockpanel,里边想放3个canvas,但是实际的效果是后边的canvas会占满整个窗口,怎么办