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)
	{

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.