Define The DLL Functions

	[System::Runtime::InteropServices::DllImport(L"user32.dll")]
	static System::IntPtr GetForegroundWindow();

	[System::Runtime::InteropServices::DllImport(L"user32.dll")]
	static bool SetForegroundWindow(System::IntPtr  hWnd);

Check Some Other Ap Is Not In Foreground


	System::IntPtr WinHandle = GetForegroundWindow();

	//BRING US TO THE FRONT
	if (WinHandle == ProcessName->MainWindowHandle) //ProcessName is a process we created elsewhere
		SetForegroundWindow(this->Handle);		//Can test return bool value if we want

	//Use this if you want a form to be able to pass a handle to its window
	System::IntPtr ApForegroundWindow;		//Variable to store this->Handle 

	SetForegroundWindow(ApForegroundWindow);	//Using it

Forcing our application form to the foreground

Outside of fucntion / in header file

	[System::Runtime::InteropServices::DllImport(L"user32.dll")]
	static System::IntPtr SetForegroundWindow(System::IntPtr  hWnd);
In function

	SetForegroundWindow(this->Handle);		//Can test return bool value if we want

Which Window Is In Use (In the windows foreground and taking user input)

Outside of function / in header file

	[System::Runtime::InteropServices::DllImport(L"user32.dll")]
	static System::IntPtr GetForegroundWindow();
In function

	System::IntPtr WinHandle;
	WinHandle = GetForegroundWindow();
	if (WinHandle == SomeOtherProcess->MainWindowHandle)
	{
//or
	if (WinHandle != this->Handle)
	{

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *