</head> <body> <h1>WinClose</h1> <p>Closes the specified window.</p> <pre class="Syntax"><span class="func">WinClose</span> <span class="optional">, WinTitle, WinText, SecondsToWait, ExcludeTitle, ExcludeText</span></pre> <h2 id="Parameters">Parameters</h2> <dl> <dt>WinTitle</dt> <dd><p>A window title or other criteria identifying the target window. See <a href="../misc/WinTitle.htm">WinTitle</a>.</p></dd> <dt>WinText</dt> <dd><p>If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if <a href="DetectHiddenText.htm">DetectHiddenText</a> is ON.</p></dd> <dt>SecondsToWait</dt> <dd><p>If omitted or blank, the command will not wait at all. If 0, it will wait 500ms. Otherwise, it will wait the indicated number of seconds (can contain a decimal point or be an <a href="../Variables.htm#Expressions">expression</a>) for the window to close. If the window does not close within that period, the script will continue. ErrorLevel is <strong>not</strong> set by this command, so use <a href="WinExist.htm">WinExist()</a>, <a href="IfWinExist.htm">IfWinExist</a> or <a href="WinWaitClose.htm">WinWaitClose</a> if you need to determine for certain that a window is closed. While the command is in a waiting state, new <a href="../misc/Threads.htm">threads</a> can be launched via <a href="../Hotkeys.htm">hotkey</a>, <a href="Menu.htm">custom menu item</a>, or <a href="SetTimer.htm">timer</a>.</p></dd> <dt>ExcludeTitle</dt> <dd><p>Windows whose titles include this value will not be considered.</p></dd> <dt>ExcludeText</dt> <dd><p>Windows whose text include this value will not be considered.</p></dd> </dl> <h2 id="Remarks">Remarks</h2> <p>This command sends a close message to a window. The result depends on the window (it may ask to save data, etc.)</p> <p>If a matching window is active, that window will be closed in preference to any other matching window. In general, if more than one window matches, the topmost (most recently used) will be closed.</p> <p>This command operates only upon a single window except when <em>WinTitle</em> is <a href="GroupAdd.htm">ahk_group GroupName</a> (with no other criteria specified), in which case all windows in the group are affected.</p> <p>WinClose sends a WM_CLOSE message to the target window, which is a somewhat forceful method of closing it. An alternate method of closing is to send the following message. It might produce different behavior because it is similar in effect to pressing <kbd>Alt</kbd>+<kbd>F4</kbd> or clicking the window's close button in its title bar:</p> <pre>PostMessage, 0x0112, 0xF060,,, WinTitle, WinText <em>; 0x0112 = WM_SYSCOMMAND, 0xF060 = SC_CLOSE</em></pre> <p>If a window does not close via WinClose, you can force it to close with <a href="WinKill.htm">WinKill</a>.</p> <p>Window titles and text are case sensitive. Hidden windows are not detected unless <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> has been turned on.</p> <h2 id="Related">Related</h2> <p><a href="WinKill.htm">WinKill</a>, <a href="WinWaitClose.htm">WinWaitClose</a>, <a href="Process.htm">Process</a>, <a href="WinActivate.htm">WinActivate</a>, <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>, <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a>, <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a>, <a href="WinExist.htm">WinExist()</a>, <a href="WinActive.htm">WinActive()</a>, <a href="WinWaitActive.htm">WinWaitActive</a>, <a href="WinWait.htm">WinWait</a>, <a href="GroupActivate.htm">GroupActivate</a></p> <h2 id="Examples">Examples</h2> <div class="ex" id="ExBasic"> <p><a class="ex_number" href="#ExBasic"></a> If Notepad does exist, close it, otherwise close the calculator.</p> <pre>if WinExist("Untitled - Notepad") WinClose <em>; Use the window found by WinExist.</em> else WinClose, Calculator</pre> </div> </body> </html>