C#: Hướng dẫn ngăn không cho mở ứng dụng khi ứng dụng đang chạy

Từ khóa

Code:

public static class TienTrinh
{
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(HandleRef hWnd, int nCmdShow);
private const int SW_RESTORE = 9;

[DllImport("user32.dll")]
private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern
bool IsIconic(IntPtr hWnd);
public static bool TienTrinhDangChay()
{
const int swRestore = 9;
var me = Process.GetCurrentProcess();
var arrProcesses = Process.GetProcessesByName(me.ProcessName);

if (arrProcesses.Length > 1)
{
for (var i = 0; i < arrProcesses.Length; i++)
{
if (arrProcesses[i].Id != me.Id)
{
IntPtr hWnd = arrProcesses[i].MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, swRestore);
}
SetForegroundWindow(hWnd);
break;
}
}
return true;
}

return false;
}
}

Video hướng dẫn:

Bài liên quan

Bài liên quan