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.Diagnostics;
using System.Runtime.InteropServices;


namespace 最小化其他应用程序
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", EntryPoint = "PostMessage")]
        public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("User32.dll ", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//关键方法
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SendMessage(IntPtr HWnd, uint Msg, int WParam, int LParam);
        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_MAXIMIZE = 0xF030;
        public const uint WM_SYSCOMMAND2 = 0x0112;
        public const uint SC_MAXIMIZE2 = 0xF030;
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 最小化其他应用程序
        /// </summary>
        /// <param name="processName"></param>
        private void MiniMizeAppication(string processName)
        {
            Process[] processs = Process.GetProcessesByName(processName);
            if (processs != null)
            {
                foreach (Process p in processs)
                {
                    IntPtr handle = FindWindow(null, p.MainWindowTitle);
                    //IntPtr handle = FindWindow("YodaoMainWndClass",null);
                    PostMessage(handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
                }
            }
        }

        /// <summary>
        /// 最小化其他应用程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            MiniMizeAppication("notepad++");
        }

        /// <summary>
        /// 最大化其他应用程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("notepad++");
            if (processes.Length > 0)
            {
                IntPtr handle = processes[0].MainWindowHandle;
                SendMessage(handle, WM_SYSCOMMAND2, new IntPtr(SC_MAXIMIZE2), IntPtr.Zero);	// 最大化
                SwitchToThisWindow(handle, true);	// 激活
            }
        }
    }
}


如果您愿意花几块钱请我喝杯茶的话,可以用手机扫描下方的二维码,通过支付宝或者微信直接向我捐赠呦。我会第一时间给您发送源码。

感谢无私捐赠的同胞们,赠人玫瑰之手,经久犹有余香!让我吃个鸡蛋灌饼的钱,同时大家又学习到了知识,想想都好激动呢!

支付宝捐赠二维码: 

微信捐赠二维码: 


衷心感谢打赏者的厚爱与支持!也感谢点赞和评论的博友的支持!打赏后可添加朋友,可作为技术交流或潜在项目合作伙伴!谢谢!




本文转载:CSDN博客