2013-04-26 19 views
0

我使用WIA ShowTransfer方法從WPF應用程序中的設備掃描圖片。但是用戶可以將焦點放在WPF應用程序上,然後WIA顯示的進度條隱藏在WPF應用程序後面。我如何強制進度條保持一切?如何強制WIA進度條保持焦點

回答

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它將觸發每500ms。所以即使用戶試圖隱藏進度條,它也會每隔500毫秒彈出一次。

另請參閱:http://hintdesk.com/how-to-enumerate-all-opened-windows/