如何强制WIA进度条保持焦点

时间:2013-04-26 12:23:13

标签: c# wpf wia

我使用WIA ShowTransfer方法从我的WPF应用程序中扫描设备中的图片。但是用户可以将焦点设置在WPF应用程序上,然后WIA显示的进度条隐藏在WPF应用程序后面。如何强制进度条保持最佳状态?

1 个答案:

答案 0 :(得分:0)

我尝试了几件事,例如枚举所有正在运行的进程,通过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

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

另请参阅:http://hintdesk.com/how-to-enumerate-all-opened-windows/