应用程序窗体切至前台的方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace SwitchProgram
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);


        public Form1()
        {
            InitializeComponent();
        }


        public struct WindowInfo
        {
            public IntPtr hWnd;
            public string szWindowName;
            public string szClassName;
        }


        List<WindowInfo> GetAllWindows()
        {
            List<WindowInfo> lwi = new List<WindowInfo>();


            EnumWindows(delegate (IntPtr hWnd, int lparam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);
                wnd.hWnd = hWnd;
                GetWindowText(hWnd, sb, sb.Capacity);
                wnd.szWindowName = sb.ToString();
                GetClassName(hWnd, sb, sb.Capacity);
                wnd.szClassName = sb.ToString();
                Console.WriteLine("className=" + wnd.szClassName + ", windowText=" + wnd.szWindowName);
                lwi.Add(wnd);
                return true;
            }, 0);


            return lwi;
        }


        /// <summary>
        /// 注意目标窗体应当选择应用程序的顶级窗体,比如对Borland程序而言,就是className为TApplication的窗体句柄。
        /// </summary>
        /// <param name="appWindowClassName"></param>
        /// <param name="appWindowText"></param>
        /// <returns></returns>
        bool SwitchToApplication(string appWindowClassName, string appWindowText)
        {
            List<WindowInfo> lwi = GetAllWindows();
            foreach (WindowInfo item in lwi)
            {
                if (item.szClassName == appWindowClassName && item.szWindowName == appWindowText)
                {
                    SetForegroundWindow(item.hWnd);
                    ShowWindow(item.hWnd, 1);
                    return true;
                }
            }
            return false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            SwitchToApplication("TApplication", "测试项目20171025");
        }
    }
}

上面描述了将指定应用程序切至前台的方法。只需要提供className, windowTextName即可。

这两个数据通过调试该程序即可得到。调试时会将系统所有的窗体信息输出至VS的输出窗口,在其中你可以找到对应的应用程序主窗体信息。

主窗体即便当前正在打开着模式对话框,这段代码同样能将模式对话框切至前台。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用Windows API来检测其他应用程序是否全屏,并相应地隐藏您的窗体。以下是一个VB.NET示例: 首先,请确保在代码文件的顶部添加以下命名空间: ```vb.net Imports System.Runtime.InteropServices ``` 然后,您需要声明以下两个Windows API函数: ```vb.net Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr Private Declare Function GetWindowPlacement Lib "user32" (ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean <Serializable, StructLayout(LayoutKind.Sequential)> Public Structure WINDOWPLACEMENT Public Length As UInteger Public flags As UInteger Public ShowCmd As UInteger Public ptMinPosition As POINTAPI Public ptMaxPosition As POINTAPI Public rcNormalPosition As RECT End Structure <Serializable, StructLayout(LayoutKind.Sequential)> Public Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure <Serializable, StructLayout(LayoutKind.Sequential)> Public Structure POINTAPI Public x As Integer Public y As Integer End Structure ``` 接下来,在窗体加载时,您可以使用以下代码来轮询其他应用程序是否全屏,并相应地隐藏您的窗体: ```vb.net Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Timer1.Interval = 100 ' 每100毫秒检查一次 Timer1.Enabled = True End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim hWnd As IntPtr = GetForegroundWindow() Dim wp As WINDOWPLACEMENT = Nothing wp.Length = CUInt(Marshal.SizeOf(wp)) GetWindowPlacement(hWnd, wp) If wp.ShowCmd = 3 Then ' 如果前台窗口是全屏状态 Me.Hide() ' 隐藏本窗口 Else Me.Show() ' 显示本窗口 End If End Sub ``` 这将启动一个计时器,每100毫秒检查一次前台窗口是否全屏。如果是,您的窗体将被隐藏。否则,您的窗体将被显示。请注意,此方法可能会影响您的应用程序的性能,因为它需要在后台运行,并且需要定期轮询其他应用程序。如果您的应用程序需要更高的性能,请考虑其他方法,例如使用Windows消息循环来检测全屏状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子正

thanks, bro...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值