|
封装了一些常用的api,查找窗体,查找子窗体等简易功能..不是原创,也记不清从哪弄来的了...{:soso_e128:}
操作没有保护的窗体还是可以的..{:soso_e120:}回复可见...
DOM下载地址:
记得好像有几个忘记加了. 单放在这里吧.{:soso_e113:},帖子过长,预览的部分删除了..详细请见附件..[code=csharp]
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(int hWnd); //置顶某窗体 就是让某个窗体显示窗体最前
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); //注入内容,可操作文本框啦,等等
//附带一个方法
public void InputStr(IntPtr myIntPtr, string Input)
{
byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
for (int i = 0; i < ch.Length; i++)
{
SendMessage(myIntPtr, WM_CHAR, int.Parse(ch.ToString()), 0);
}
}
SendKeys.Send("{TAB}"); //模拟键盘输入table
SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
[/code]
祝 :
every body 生活愉快,身体健康..{:soso_e121:}
have Fun.!
by Ro4ters
27-09/2013
[code=csharp]
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
namespace TestWinAPI
{
public class WinAPI
{
#region WinodwsAPI
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "GetParent")]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public static extern bool GetCursorPos(out Point pt);
[DllImport("user32.dll", EntryPoint = "WindowFromPoint", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr WindowFromPoint(Point pt);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowRect(IntPtr hwnd, ref Rectangle rc);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClientRect(IntPtr hwnd, ref Rectangle rc);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int ScreenToClient(IntPtr hWnd, ref Rectangle rect);
#endregion
#region 封装API方法
/// <summary>
/// 找到句柄
/// </summary>
/// <param name="IpClassName">类名</param>
/// <returns></returns>
public static IntPtr GetHandle(string IpClassName)
{
return FindWindow(IpClassName, null);
}
/// <summary>
/// 找到句柄
/// </summary>
/// <param name="p">坐标</param>
/// <returns></returns>
public static IntPtr GetHandle(Point p)
{
return WindowFromPoint(p);
}
/// <summary>
/// 鼠标位置的坐标
/// </summary>
/// <returns></returns>
public static Point GetCursorPosPoint()
{
Point p = new Point();
if (GetCursorPos(out p))
{
return p;
}
return default(Point);
}
/// <summary>
/// 子窗口句柄
/// </summary>
/// <param name="hwndParent">父窗口句柄</param>
/// <param name="hwndChildAfter">前一个同目录级同名窗口句柄</param>
/// <param name="lpszClass">类名</param>
/// <returns></returns>
public static IntPtr GetChildHandle(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass)
{
return FindWindowEx(hwndParent, hwndChildAfter, lpszClass, null);
}
/// <summary>
/// 全部子窗口句柄
/// </summary>
/// <param name="hwndParent">父窗口句柄</param>
/// <param name="className">类名</param>
/// <returns></returns>
public static List<IntPtr> GetChildHandles(IntPtr hwndParent, string className)
{
List<IntPtr> resultList = new List<IntPtr>();
for (IntPtr hwndClient = GetChildHandle(hwndParent, IntPtr.Zero, className); hwndClient != IntPtr.Zero; hwndClient = GetChildHandle(hwndParent, hwndClient, className))
{
resultList.Add(hwndClient);
}
return resultList;
}
/// <summary>
/// 给窗口发送内容
/// </summary>
/// <param name="hWnd">句柄</param>
/// <param name="lParam">要发送的内容</param>
public static void SetText(IntPtr hWnd, string lParam)
{
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, lParam);
}
private const int WM_SETTEXT = 0x000C;
/// <summary>
/// 获得窗口内容或标题
/// </summary>
/// <param name="hWnd">句柄</param>
/// <returns></returns>
public static string GetText(IntPtr hWnd)
{
StringBuilder result = new StringBuilder(128);
GetWindowText(hWnd, result, result.Capacity);
return result.ToString();
}
/// <summary>
/// 找类名
/// </summary>
/// <param name="hWnd">句柄</param>
/// <returns></returns>
public static string GetClassName(IntPtr hWnd)
{
StringBuilder lpClassName = new StringBuilder(128);
if (GetClassName(hWnd, lpClassName, lpClassName.Capacity) == 0)
{
throw new Exception("not found IntPtr!");
}
return lpClassName.ToString();
}
/// <summary>
/// 窗口在屏幕位置
/// </summary>
/// <param name="hWnd">句柄</param>
/// <returns></returns>
public static Rectangle GetWindowRect(IntPtr hWnd)
{
Rectangle result = default(Rectangle);
GetWindowRect(hWnd, ref result);
return result;
}
/// <summary>
/// 窗口相对屏幕位置转换成父窗口位置
/// </summary>
/// <param name="hWnd"></param>
/// <param name="rect"></param>
/// <returns></returns>
public static Rectangle ScreenToClient(IntPtr hWnd, Rectangle rect)
{
Rectangle result = rect;
ScreenToClient(hWnd, ref result);
return result;
}
/// <summary>
/// 窗口大小
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static Rectangle GetClientRect(IntPtr hWnd)
{
Rectangle result = default(Rectangle);
GetClientRect(hWnd, ref result);
return result;
}
#endregion
public struct WindowInfo
{
public IntPtr hWnd;
public string szWindowName;
public string szClassName;
}
static public WindowInfo[] GetAllDesktopWindows()
{
List<WindowInfo> wndList = new List<WindowInfo>();
EnumWindows(delegate(IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder(256);
//get hwnd
wnd.hWnd = hWnd;
//get window name
GetWindowTextW(hWnd, sb, sb.Capacity);
wnd.szWindowName = sb.ToString();
//get window class
GetClassNameW(hWnd, sb, sb.Capacity);
wnd.szClassName = sb.ToString();
//add it into list
wndList.Add(wnd);
return true;
}, 0);
return wndList.ToArray();
}
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
[DllImport("user32.dll")]
private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
}
[/code]
补充内容 (2014-11-3 23:46):
C#调用Win32API 配套视频讲解 http://pan.baidu.com/s/1gdAFesb |
|