标题:C#如何在窗口2关闭已经隐藏的窗口1?
只看楼主
小刀神
Rank: 1
等 级:新手上路
帖 子:69
专家分:5
注 册:2018-5-27
结帖率:91.67%
已结贴  问题点数:5 回复次数:1 
C#如何在窗口2关闭已经隐藏的窗口1?
C#如何在窗口2关闭已经隐藏的窗口1?

[此贴子已经被作者于2019-1-6 15:14编辑过]

搜索更多相关主题的帖子: C# 窗口 关闭 隐藏 
2019-01-06 14:51
oldSlave
Rank: 2
等 级:论坛游民
威 望:1
帖 子:8
专家分:25
注 册:2017-9-19
得分:5 
/*-->思路(仅供参考):先找到窗体的句柄(存放到mainHandle)-->再用SendMessage(mainHandle, 0x10, 0, 0);关闭窗体*/
using BCZG_WinFrom;
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BCZG_WinFrom
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 获取窗体的句柄函数
        /// </summary>
        /// <param name="lpClassName">窗口类名</param>
        /// <param name="lpWindowName">窗口标题名</param>
        /// <returns>返回句柄</returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 设置窗体状态(0:隐藏窗体,1:默认窗体,2:最小化窗体,3:最大化窗体,....)
        /// </summary>
        /// <param name="hwnd">窗体句柄</param>
        /// <param name="nCmdShow">状态</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        /// <summary>
        ///
        /// </summary>
        /// <param name="hwnd">窗体句柄</param>
        /// <param name="wMsg">16进制0x10为关闭窗体</param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        IntPtr mainHandle = IntPtr.Zero;
        private void button1_Click(object sender, EventArgs e)
        {
            FormEg EgFrm1 = new FormEg();
            EgFrm1.Text = "测试窗体";
            EgFrm1.Show();
        }
        /// <summary>
        /// 隐藏窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            // 获取查找窗体句柄(通过窗体标题名)
            mainHandle = FindWindow(null, "测试窗体");
            if (mainHandle != IntPtr.Zero)
            {
                Console.WriteLine("找到窗体");
                //通过句柄设置当前窗体状态(0:隐藏窗体,1:默认窗体,2:最小化窗体,3:最大化窗体,....)
                ShowWindow(mainHandle, 0);
            }else
            {
                MessageBox.Show("没有找到!");
            }

        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if(mainHandle!=IntPtr.Zero)
                SendMessage(mainHandle, 0x10, 0, 0);
            else
            {
                MessageBox.Show("没有可关闭的窗体!");
            }
        }
    }
}


[此贴子已经被作者于2019-1-6 21:30编辑过]

2019-01-06 21:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492534-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.293049 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved