0

我正在使用Graphics.CopyFromScreen()方法来捕获我拥有的 HWND 控件的快照。问题是我想避免捕获闪烁的插入符号。有没有办法做到这一点?如果需要的话,我愿意使用 API 调用(BitBlt?)。

注意:我在这里看到了一个非常相似的问题但问题是我的控件不是 WinForms 控件,甚至不是标准的 EDIT 类,所以我没有像DrawToBitmap(). 它是在单元格中按 F2 时出现的 Excel 的编辑框。

4

1 回答 1

1

看起来,HideCaret 和 ShowCaret 函数会有所帮助

http://msdn.microsoft.com/en-us/library/windows/desktop/ms648406(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ ms648403(v=vs.85).aspx

要获取持有插入符号的控件(Edit、ComobBox 等)的句柄,您可以使用函数 GetWindowThreadProcessId 来获取 ThreadId GetGUIThreadInfo 来获取插入符号持有者的句柄

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx

[DllImport("User32",
           CallingConvention = CallingConvention.Winapi,
           ExactSpelling = true,
           EntryPoint = "HideCaret",
           SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean HideCaret(IntPtr hWnd);

[DllImport("User32",
           CallingConvention = CallingConvention.Winapi,
           ExactSpelling = true,
           EntryPoint = "ShowCaret",
           SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean ShowCaret(IntPtr hWnd);


// If the window you have to copy is in your process then 
// handle = IntPtr.Zero
// Otherwise your have to find it out via GetWindowThreadProcessId and GetGUIThreadInfo 

HideCaret(handle);

try {
  // Your code to capture the image 
}
finally {
  ShowCaret(handle);
}
于 2013-04-28T11:28:02.953 回答