基本信息
源码名称:C# 查找活动窗体 示例源码(句柄)
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2018-01-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
如何通过WinForm查找当前桌面上运行的窗体

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;

namespace FindWindow
{
    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 GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnum_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = string.Empty;
            StringBuilder lpString = new StringBuilder(200);
            // 遍历所有windows
            EnumWindows(delegate(IntPtr hWnd, int lParam)
            {
                lpString = new StringBuilder(200);
                GetWindowTextW(hWnd, lpString, lpString.Capacity);
                if (!string.IsNullOrEmpty(lpString.ToString()))
                {
                    this.textBox1.AppendText(lpString.ToString()   "\r\n");
                }
                return true;
            }, 0);
        }

        private void btnFimd_Click(object sender, EventArgs e)
        {
            IntPtr hwnd = new IntPtr(0);
            hwnd = FindWindow(null, "查找窗体");
            //如果找到窗口句柄
            if (hwnd != IntPtr.Zero)
            {
                this.textBox1.AppendText("找到\"查找窗体\"");
            }
            else
            {
                this.textBox1.AppendText("未找到\"查找窗体\"");
            }
        }
    }
}