截指定窗口的图

***寻找窗口的代码***

用法:FindWindow fw = new FindWindow(IntPtr.Zero, null, "ThunderDFrame", 10);//查找Title为ThunderDFrame的窗口,如果10秒内还没找到,返回false

-----------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Util
{
   
class FindWindow
    {
        [DllImport(
"user32")]
        [
return: MarshalAs(UnmanagedType.Bool)]
       
//IMPORTANT : LPARAM  must be a pointer (InterPtr) in VS2005, otherwise an exception will be thrown
        private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
       
//the callback function for the EnumChildWindows
        private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

       
//if found  return the handle , otherwise return IntPtr.Zero
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
       
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

       
private string m_classname; // class name to look for
        private string m_caption; // caption name to look for

       
private DateTime start;
       
private int m_timeout;//If exceed the time. Indicate no windows found.

       
private IntPtr m_hWnd; // HWND if found
        public IntPtr FoundHandle
        {
           
get { return m_hWnd; }
        }

       
private bool m_IsTimeOut;
       
public bool IsTimeOut
        {
           
get{return m_IsTimeOut;}
           
set { m_IsTimeOut = value; }
        }

       
// ctor does the work--just instantiate and go
        public FindWindow(IntPtr hwndParent, string classname, string caption, int timeout)
        {
            m_hWnd
= IntPtr.Zero;
            m_classname
= classname;
            m_caption
= caption;
            m_timeout
= timeout;
            start
= DateTime.Now;
            FindChildClassHwnd(hwndParent, IntPtr.Zero);
        }

       
/**/
       
/// <summary>
       
/// Find the child window, if found m_classname will be assigned
       
/// </summary>
       
/// <param name="hwndParent">parent's handle</param>
       
/// <param name="lParam">the application value, nonuse</param>
       
/// <returns>found or not found</returns>
        //The C++ code is that  lParam is the instance of FindWindow class , if found assign the instance's m_hWnd
        private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
        {
            EnumWindowProc childProc
= new EnumWindowProc(FindChildClassHwnd);
            IntPtr hwnd
= FindWindowEx(hwndParent, IntPtr.Zero, m_classname, m_caption);
           
if (hwnd != IntPtr.Zero)
            {
               
this.m_hWnd = hwnd; // found: save it
                m_IsTimeOut = false;
               
return false; // stop enumerating
            }

            DateTime end
= DateTime.Now;

           
if (start.AddSeconds(m_timeout) > end)
            {
                m_IsTimeOut
= true;
               
return false;
            }

            EnumChildWindows(hwndParent, childProc, IntPtr.Zero);
// recurse  redo FindChildClassHwnd
            return true;// keep looking
        }
    }
}

posted on 2011-07-14 11:47 ryhan 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ryhan/archive/2011/07/14/2106186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Delphi中,可以使用Windows API函数来在指定窗口中找到指定片,并模拟鼠标单击事件。具体步骤如下: 1. 获取目标窗口的句柄,可以使用FindWindow或FindWindowEx函数来获取。 2. 获取目标窗口的客户区坐标,可以使用GetClientRect函数来获取。 3. 使用BitBlt函数将目标窗口的屏幕图像复制到内存中。 4. 使用FindBitmap函数在内存图像中查找指定的位,可以使用Graphics.pas单元中的TBitmap对象来表示位。 5. 如果找到位,计算位在目标窗口中的坐标位置,并模拟鼠标单击事件。 下面是一个示例代码: ```delphi var hWnd: HWND; rect: TRect; bmp: TBitmap; pt, ptClient: TPoint; memDC, bmpDC: HDC; memBmp, bmpOld: HBITMAP; begin // 获取目标窗口句柄 hWnd := FindWindow(nil, '窗口标题'); if hWnd <> 0 then begin // 获取目标窗口客户区坐标 GetClientRect(hWnd, rect); // 创建内存DC和内存位 memDC := CreateCompatibleDC(0); memBmp := CreateCompatibleBitmap(GetDC(0), rect.Right - rect.Left, rect.Bottom - rect.Top); bmpOld := SelectObject(memDC, memBmp); // 将目标窗口的屏幕图像复制到内存中 BitBlt(memDC, 0, 0, rect.Right - rect.Left, rect.Bottom - rect.Top, GetDC(hWnd), 0, 0, SRCCOPY); // 加载位文件到TBitmap对象中 bmp := TBitmap.Create; bmp.LoadFromFile('片文件名'); // 在内存图像中查找位 pt := FindBitmap(memDC, bmp, rect, 0); if (pt.X <> -1) and (pt.Y <> -1) then begin // 计算位在目标窗口中的坐标位置 ptClient.X := pt.X + rect.Left; ptClient.Y := pt.Y + rect.Top; // 模拟鼠标单击事件 SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MakeLParam(ptClient.X, ptClient.Y)); SendMessage(hWnd, WM_LBUTTONUP, MK_LBUTTON, MakeLParam(ptClient.X, ptClient.Y)); end; // 释放资源 bmp.Free; SelectObject(memDC, bmpOld); DeleteObject(memBmp); DeleteDC(memDC); end; end; ``` 注意:在使用FindBitmap函数查找位时,需要传入内存DC、TBitmap对象和目标窗口客户区坐标。如果找到位,函数返回位在内存图像中的坐标位置。同时,在模拟鼠标单击事件时,需要传入目标窗口的句柄和位在目标窗口中的客户区坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值