最近在開發本機report列印程式時,碰到列印後程式就會失焦的狀況,試了很多參數還是無法排除現象。目前先用重新取得焦點的方式處理,程式段如下:
using System.Runtime.InteropServices;
public static class NativeMethods
{
[DllImport(“user32.dll”)]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport(“user32.dll”)]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void FocusApplication()
{
Process currentProcess = Process.GetCurrentProcess();
IntPtr hWnd = currentProcess.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
SetForegroundWindow(hWnd);
ShowWindow(hWnd, (int)User32.SW_MAXIMIZE);
}
}
private enum User32
{
SW_FORCEMINIMIZE = 11,
SW_HIDE = 0,
SW_MAXIMIZE = 3,
SW_MINIMIZE = 6,
SW_RESTORE = 9,
SW_SHOW = 5,
SW_SHOWDEFAULT = 10,
SW_SHOWMAXIMIZED = 3,
SW_SHOWMINIMIZED = 2,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_SHOWNOACTIVATE = 4,
SW_SHOWNORMAL = 1,
}
}

Leave a comment