現在のアプリを確実に最前面に移動させアクティブにする方法
VB.NET
<DllImport("USER32.DLL",
CharSet:=CharSet.Auto)> _
Private Shared Function
ShowWindow(hWnd As System.IntPtr, nCmdShow As Integer) As Integer
End Function
<DllImport("USER32.DLL",
CharSet:=CharSet.Auto)> _
Private Shared Function
SetForegroundWindow(hWnd As System.IntPtr) As Boolean
End Function
'''
<summary>
''' 表示定数
'''
</summary>
Private Const
SW_NORMAL As Integer = 1
'''
<summary>
''' カレントプロセスを最前面に表示する方法
'''
</summary>
'''
<returns></returns>
Public Shared Function
ShowFormTopMostAndActive() As Boolean
Dim
pcCurrent As Process = Process.GetCurrentProcess()
Dim
pcLists As Process() = Process.GetProcessesByName(pcCurrent.ProcessName)
Dim
intProcessID As Integer = pcCurrent.Id
For Each
pcItem As Process In pcLists
If
pcItem.Id <> intProcessID Then
' 最前面に表示してアクティブにする方法
ShowWindow(pcItem.MainWindowHandle, SW_NORMAL)
SetForegroundWindow(pcItem.MainWindowHandle)
Return True
End If
Next
Return False
End Function
(無料電話サポート付)できるVisual Studio 2015 Windows /Android/iOS アプリ対応
C#
[DllImport("USER32.DLL",
CharSet = CharSet.Auto)]
private static extern int
ShowWindow(System.IntPtr hWnd, int
nCmdShow);
[DllImport("USER32.DLL",
CharSet = CharSet.Auto)]
private static extern bool
SetForegroundWindow( System.IntPtr hWnd);
///
<summary>
/// 表示定数
///
</summary>
private const int
SW_NORMAL = 1;
///
<summary>
/// カレントプロセスを最前面に表示する方法
///
</summary>
///
<returns></returns>
public static bool
ShowFormTopMostAndActive()
{
Process
pcCurrent = Process.GetCurrentProcess();
Process[]
pcLists = Process.GetProcessesByName(pcCurrent.ProcessName);
int
intProcessID = pcCurrent.Id;
foreach (Process
pcItem in pcLists)
{
if (pcItem.Id != intProcessID)
{
// 最前面に表示してアクティブにする方法
ShowWindow(pcItem.MainWindowHandle, SW_NORMAL);
SetForegroundWindow(pcItem.MainWindowHandle);
return true;
}
}
return false;
}
(無料電話サポート付)できるVisual Studio 2015 Windows /Android/iOS アプリ対応