您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> C# 让最小化的窗口不最小化

C# 让最小化的窗口不最小化

来源:网络整理     时间:2016/5/11 17:10:18     关键词:C#,窗口

关于网友提出的“C# 让最小化的窗口不最小化”问题疑问,本网通过在网上对“C# 让最小化的窗口不最小化”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:C# 让最小化的窗口不最小化
描述:

窗体

简单描述一下:一个窗口正最小化着呢,但是我想要让他弹出来。
这个过程,类似于:我点击任务栏上小框框,然后他就会弹出来。
解决方案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); //恢复窗体状态
}
}
}  
解决方案2:

测试通过,你将代码里面的进程名改成自己的就行了


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); //恢复窗体状态
                        }
                    }
                }
            }
        }        
    }
}

以上介绍了“C# 让最小化的窗口不最小化”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1030281.html

相关图片

相关文章