{"id":70,"date":"2010-03-17T12:02:20","date_gmt":"2010-03-17T12:02:20","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=70"},"modified":"2022-02-17T06:24:06","modified_gmt":"2022-02-17T06:24:06","slug":"foreground-window-detect-and-set","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/application-control\/foreground-window-detect-and-set","title":{"rendered":"Foreground Window Detect and Set"},"content":{"rendered":"<h4>Define The DLL Functions<\/h4>\n<pre><code>\t[System::Runtime::InteropServices::DllImport(L\"user32.dll\")]\r\n\tstatic System::IntPtr GetForegroundWindow();\r\n\r\n\t[System::Runtime::InteropServices::DllImport(L\"user32.dll\")]\r\n\tstatic bool SetForegroundWindow(System::IntPtr  hWnd);\r\n<\/code><\/pre>\n<h4>Check Some Other Ap Is Not In Foreground<\/h4>\n<pre><code>\r\n\tSystem::IntPtr WinHandle = GetForegroundWindow();\r\n\r\n\t\/\/BRING US TO THE FRONT\r\n\tif (WinHandle == ProcessName-&gt;MainWindowHandle) \/\/ProcessName is a process we created elsewhere\r\n\t\tSetForegroundWindow(this-&gt;Handle);\t\t\/\/Can test return bool value if we want\r\n\r\n\t\/\/Use this if you want a form to be able to pass a handle to its window\r\n\tSystem::IntPtr ApForegroundWindow;\t\t\/\/Variable to store this-&gt;Handle \r\n\r\n\tSetForegroundWindow(ApForegroundWindow);\t\/\/Using it\r\n<\/code><\/pre>\n<h4>Forcing our application form to the foreground<\/h4>\n<h5>Outside of fucntion \/ in header file<\/h5>\n<pre><code>\r\n\t[System::Runtime::InteropServices::DllImport(L\"user32.dll\")]\r\n\tstatic System::IntPtr SetForegroundWindow(System::IntPtr  hWnd);\r\n<\/code><\/pre>\n<h5>In function<\/h5>\n<pre><code>\r\n\tSetForegroundWindow(this-&gt;Handle);\t\t\/\/Can test return bool value if we want\r\n<\/code><\/pre>\n<h4>Which Window Is In Use (In the windows foreground and taking user input)<\/h4>\n<h5>Outside of function \/ in header file<\/h5>\n<pre><code>\r\n\t[System::Runtime::InteropServices::DllImport(L\"user32.dll\")]\r\n\tstatic System::IntPtr GetForegroundWindow();\r\n<\/code><\/pre>\n<h5>In function<\/h5>\n<pre><code>\r\n\tSystem::IntPtr WinHandle;\r\n\tWinHandle = GetForegroundWindow();\r\n\tif (WinHandle == SomeOtherProcess-&gt;MainWindowHandle)\r\n\t{\r\n\/\/or\r\n\tif (WinHandle != this-&gt;Handle)\r\n\t{\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Define The DLL Functions [System::Runtime::InteropServices::DllImport(L&#8221;user32.dll&#8221;)] static System::IntPtr GetForegroundWindow(); [System::Runtime::InteropServices::DllImport(L&#8221;user32.dll&#8221;)] 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-&gt;MainWindowHandle) \/\/ProcessName is a process we created elsewhere SetForegroundWindow(this-&gt;Handle); \/\/Can test return bool value if we want \/\/Use this if you want [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-application-control"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/70\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}