// ProcessShowPrevProcess.cpp : メイン プロジェクト ファイルです。
#include "stdafx.h"
#include "Form1.h"
using namespace ProcessShowPrevProcess;
// 以下の名前空間をインポートする
using namespace System;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;
public class MyProcess {
[DllImport("USER32.DLL", CharSet=CharSet::Auto)]
static bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("USER32.DLL", CharSet=CharSet::Auto)]
static bool SetForegroundWindow(IntPtr hWnd);
static const int SW_NORMAL = 1;
/// ------------------------------------------------------------------------------------
///
/// 同名のプロセスが起動中の場合、メイン ウィンドウをアクティブにします。
///
/// 既に起動中であれば true。それ以外は false。
/// ------------------------------------------------------------------------------------
public:
static bool ShowPrevProcess() {
Process^ hThisProcess = Process::GetCurrentProcess();
array^hProcesses =
Process::GetProcessesByName(hThisProcess->ProcessName);
int iThisProcessId = hThisProcess->Id;
for each (Process^ hProcess in hProcesses) {
if (hProcess->Id != iThisProcessId) {
ShowWindow(hProcess->MainWindowHandle, SW_NORMAL);
SetForegroundWindow(hProcess->MainWindowHandle);
return true;
}
}
return false;
}
};
[STAThreadAttribute]
int main(array ^args)
{
// 同名のプロセスが起動していない時は起動する
if (!MyProcess::ShowPrevProcess()) {
Application::Run(gcnew Form1());
}
return 0;
}