如何强制WIA进度条保持对焦?

本文关键字:何强制 WIA | 更新日期: 2023-09-27 18:11:12

我使用WIA ShowTransfer方法从我的WPF应用程序内的设备扫描图片。但是用户可以将焦点设置在WPF应用程序上,然后WIA显示的进度条隐藏在WPF应用程序后面。我怎样才能使进度条保持在所有内容的顶部?

如何强制WIA进度条保持对焦?

我尝试了一些事情,例如枚举所有正在运行的进程,枚举WPF应用程序中的所有窗口,但没有运气。

#region Force Scan Progress to front hack
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int _GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
const int MAXTITLE = 255;
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
public static string GetWindowText(IntPtr hWnd)
{
    StringBuilder strbTitle = new StringBuilder(MAXTITLE);
    int nLength = _GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
    strbTitle.Length = nLength;
    return strbTitle.ToString();
}
private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
    string strTitle = GetWindowText(hWnd);
    if (strTitle.StartsWith("Title of progress bar")) SetForegroundWindow(hWnd);
    return true;
}
private void forceScanProgressToFront(object source, EventArgs ea)
{
    EnumDelegate delEnumfunc = new EnumDelegate(EnumWindowsProc);
    bool bSuccessful = EnumDesktopWindows(IntPtr.Zero, delEnumfunc, IntPtr.Zero);
    if (!bSuccessful)
    {
        // Get the last Win32 error code
        int nErrorCode = Marshal.GetLastWin32Error();
        string strErrMsg = String.Format("EnumDesktopWindows failed with code {0}.", nErrorCode);
        throw new Exception(strErrMsg);
    }
}
#endregion

我将其绑定到每500ms触发一次的DispatcherTimer。因此,即使用户试图隐藏进度条,它也会每隔500毫秒弹出一次。

参见:http://hintdesk.com/how-to-enumerate-all-opened-windows/

解决方法是从UI线程中调用ShowTransfer方法,而不是从工作线程中调用。