简单描述一下:一个窗口正最小化着呢,但是我想要让他弹出来。
这个过程,类似于:我点击任务栏上小框框,然后他就会弹出来。
------解决思路----------------------
测试通过,你将代码里面的进程名改成自己的就行了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
System.Timers.Timer timer = new System.Timers.Timer(1000);
private void Form6_Load(object sender, EventArgs e)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(SetNormal);
timer.AutoReset = true;
}
private void button1_Click(object sender, EventArgs e)
{
timer.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer.Stop();
}
//向句柄窗体发送消息
[DllImport("user32.dll")]
static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
// 查找窗口句柄
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// 显示/隐藏窗口
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
Process[] processs;
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
private void SetNormal(object sender, System.Timers.ElapsedEventArgs e)
{
//用记事本程序测试,进程名notepad
processs = Process.GetProcessesByName("notepad");
if (processs.Length > 0)
{
foreach (Process p in processs)
{
if (p.MainWindowHandle != IntPtr.Zero)
{
GetWindowPlacement(p.MainWindowHandle, ref placement);
if (placement.showCmd == 2) //如果处于最小化状态
{
IntPtr handle = FindWindow(null, p.MainWindowTitle);
ShowWindow(handle, 1); //恢复窗体状态
}
}
}
}
}
}
}
------解决思路----------------------
SetNormal事件调整一下,加一个空值判断
private void SetNormal(object sender, System.Timers.ElapsedEventArgs e)
{
//用记事本程序测试,进程名notepad
process = Process.GetProcessesByName("notepad");
if (process == null) return;
foreach (Process p in process.Where(s => s.MainWindowHandle != IntPtr.Zero))
{
GetWindowPlacement(p.MainWindowHandle, ref placement);
if (placement.showCmd == 2) //如果处于最小化状态
{
IntPtr handle = FindWindow(null, p.MainWindowTitle);
ShowWindow(handle, 1); //恢复窗体状态
}
}
}