I use ShellExecute
to do something, and the first parameters is HWND
, the documentation on MSDN says:
A handle to the parent window used for displaying a UI or error messages. This value can be NULL if the operation is not associated with a window.
but I find whichever HWND
value gets the same result.
for example :
ShellExecute(0, 'open', 'c:\', nil, nil, SW_SHOWNORMAL);
ShellExecute(Self.Handle, 'open', 'c:\', nil, nil, SW_SHOWNORMAL);
ShellExecute(123456, 'open', 'c:\', nil, nil, SW_SHOWNORMAL);
just gets the same thing (Opens disk C), so I wonder what's the use using different HWND
?
by the way, when 'HWND = 0' is the DeskTop's Handle used?
property Handle: HWND; Description. Use Handle when calling Windows API functions that require a parent window handle. For example, a DLL that displays its own top-level pop-up windows needs a parent window to display its windows in the application.
Custom Commands support the “ShellExecute” function, which lets you tell the Windows shell to perform an operation on a specified file. The nice thing about ShellExecute is that you don't need to know what specific application is registered to handle a particular type of file.
To use ShellExecute or ShellExecuteEx, your application must specify the file or folder object that is to be acted on, and a verb that specifies the operation. For ShellExecute, assign these values to the appropriate parameters. For ShellExecuteEx, fill in the appropriate members of a SHELLEXECUTEINFO structure.
1 Answer 1. That HWND is used as the owner window for any UI that is shown as a result of the call to ShellExecute. For example, any error message dialogs will be owned by that window. The implications of a window being owned are described in the MSDN documentation.
ShellExecute is the code equivalent of a user double clicking a file icon. It causes Windows to work out what application the document file is associated with, launch the program and have it load the document file. By using ShellExecute, you don’t need to know the name or location of the program that’s registered to a particular file type.
ShellExecute in Delphi – Launch external applications. ShellExecute is Delphi Windows API function that is mostly used for launch external applications from our Delphi application. This function is linked to the ShellExecute Windows API function.
In the code samples, the call to ShellExecute is not showing any UI at all. So it makes no difference what you pass. But if your calls resulted in an error dialog being shown, then the window handle that you pass would become relevant. Thanks for contributing an answer to Stack Overflow!
That HWND
is used as the owner window for any UI that is shown as a result of the call to ShellExecute
. For example, any error message dialogs will be owned by that window.
The implications of a window being owned are described in the MSDN documentation. Key excerpts:
Being owned places several constraints on a window.
- An owned window is always above its owner in the z-order.
- The system automatically destroys an owned window when its owner is destroyed.
- An owned window is hidden when its owner is minimized.
The important one is the first one. If you are calling ShellExecute
from a GUI app then you want any windows to be owned by the window that is currently active in your app. So pass MyForm.Handle
.
If you have no GUI in your app then pass 0.
In the code samples, the call to ShellExecute
is not showing any UI at all. So it makes no difference what you pass. But if your calls resulted in an error dialog being shown, then the window handle that you pass would become relevant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With