C# – 本機報表列印後程式失焦的解決方案(讓當前程式獲取焦點)

最近在開發本機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

This site uses Akismet to reduce spam. Learn how your comment data is processed.