我正在尝试使用名为CFindWnd
的类。不幸的是,它是用C语言编写的,我不知道如何将其翻译成C#(或我所知道的任何其他语言)。因为它不是一个非常大的课程,我希望找到一个愿意为我翻译它的乐于助人的灵魂。
////////////////////////////////////////////////////////////////
// MSDN Magazine -- August 2003
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET on Windows XP. Tab size=3.
//
// ---
// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
// CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
//////////////////
// This private function is used with EnumChildWindows to find the child
// with a given class name. Returns FALSE if found (to stop enumerating).
//
static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {
CFindWnd *pfw = (CFindWnd*)lParam;
HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);
if (hwnd) {
pfw->m_hWnd = hwnd; // found: save it
return FALSE; // stop enumerating
}
EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
return TRUE; // keep looking
}
public:
LPCSTR m_classname; // class name to look for
HWND m_hWnd; // HWND if found
// ctor does the work--just instantiate and go
CFindWnd(HWND hwndParent, LPCSTR classname)
: m_hWnd(NULL), m_classname(classname)
{
FindChildClassHwnd(hwndParent, (LPARAM)this);
}
};
编辑: 我想出了以下内容。但是我仍然无法从对象转换为IntPtr,反之亦然。
class CFindWnd
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
private static bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
{
CFindWnd pfw = lParam as CFindWnd; //??? Error: can not be converted
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, pfw.m_classname, IntPtr.Zero);
if (!hwnd.Equals(IntPtr.Zero)) //??? not sure if that is correct
{
pfw.m_hWnd = hwnd; // found: save it
return false; // stop enumerating
}
EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
return true; // keep looking
}
public string m_classname; // class name to look for
public IntPtr m_hWnd; // HWND if found
// ctor does the work--just instantiate and go
public CFindWnd(IntPtr hwndParent, string classname)
{
m_hWnd = IntPtr.Zero;
m_classname = classname;
FindChildClassHwnd(hwndParent, this); //??? conversion error
}
}
答案 0 :(得分:2)
您将不得不使用pInvoke在C#中使用Win32方法
查看http://www.pinvoke.net/default.aspx/user32.findwindowex,了解如何使用pInvoke
来使用FindWindowEx
答案 1 :(得分:1)
我已经解决了。解决方案不是一个直接的翻译,因为原来似乎有点过于迂回。 用法:调用静态方法 handle = CFindWnd.FindHandle(parentHandle,classname)以获取作为 parentHandle 后代的窗口的句柄,其名称为类名。 示例:假设您有一个webbrowser控件并希望直接操作底层的 Internet Explorer_Server ,您可以使用 CFindWnd.FindHandle(webbrowser.Handle,“Internet Explorer_Server”)来获取它的处理。
class CFindWnd
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
static private bool FindChildClassHwnd(IntPtr parentHandle, IntPtr lParam)
{
IntPtr hwnd = FindWindowEx(parentHandle, IntPtr.Zero, _classname, IntPtr.Zero);
if (!hwnd.Equals(IntPtr.Zero))
{
_handle = hwnd; // found: save it
return false; // stop enumerating
}
EnumChildWindows(parentHandle, FindChildClassHwnd, lParam); // recurse
return true; // keep looking
}
static private string _classname; // class name to look for
static private IntPtr _handle; // HWND if found
static public IntPtr FindHandle(IntPtr parentHandle, string classname)
{
_handle = IntPtr.Zero;
_classname = classname;
FindChildClassHwnd(parentHandle, IntPtr.Zero);
return _handle;
}
}
答案 2 :(得分:0)
您可以尝试使用FindWindow和FindWindowEx
答案 3 :(得分:-1)
您无法使用.NET中的C ++类。
将功能公开为C dllexports。