c#: 模态窗口最小化主窗口
2021-07-09 13:05
标签:protected state top test nta ptr rgs 程序 def 起源: 产品中,通常有些耗时操作比如转换等,在模态窗口中执行。此时最小化它,主窗体不能跟着最小化,影响操作。 如果让主窗体最小化,并且可以还原呢?搜索一番,未找到满意结果,自己动手吧! 1、改造Program.cs文件 其目的即以MainForm记录程序主窗体 2、改写Modal窗体SizeChanged事件 在OnSizeChanged事件中处理FormWindowState.Minimized信号,最小化主窗体 或直接用Owner,这样就不需第1步设置MainForm记录主窗体: 3、主窗体调用 显示效果如图: c#: 模态窗口最小化主窗口 标签:protected state top test nta ptr rgs 程序 def 原文地址:https://www.cnblogs.com/crwy/p/9707648.html static class Program
{
///
public partial class TestForm : Form
{
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("User32.dll")]
public static extern int SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
//ShowWindow参数
private const int SW_SHOWMINIMIZED = 2;
private const int SWP_SHOWWINDOW = 0x40;
public TestForm()
{
InitializeComponent();
this.ShowInTaskbar = false;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (this.WindowState == FormWindowState.Minimized)
{
ShowWindow(Program.MainForm.Handle, SW_SHOWMINIMIZED);
this.Visible = true;
//移到屏幕外面,模拟最小化效果
SetWindowPos(this.Handle, IntPtr.Zero, Program.MainForm.Left, Program.MainForm.Top, 0, 0, SWP_SHOWWINDOW);
//Trace.WriteLine(string.Format("x: {0}, y: {1}", Program.MainForm.Left, Program.MainForm.Top));
}
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (this.WindowState == FormWindowState.Minimized)
{
var owner = this.Owner;
if (owner != null)
{
ShowWindow(owner.Handle, SW_SHOWMINIMIZED);
this.Visible = true;
//移到屏幕外面,模拟最小化效果
SetWindowPos(this.Handle, IntPtr.Zero, owner.Left, owner.Top, 0, 0, SWP_SHOWWINDOW);
//Trace.WriteLine(string.Format("x: {0}, y: {1}", Program.MainForm.Left, Program.MainForm.Top));
}
}
}
using (var form = new TestForm())
{
form.ShowDialog(this);
}
上一篇:WPF 画心2.0版之元旦快乐
下一篇:C# 创建、读取Excel公式