How would I go about starting a Run Dialog window and having my application input a command to start Steam?
I would like my application to open the standard windows run dialog pre-filled with input.
This is what I have tried:
HINSTANCE result;
result = ShellExecute(NULL, "open", "rundll32.exe", "steam://connect/192.69.96.168:27023", NULL, SW_SHOWDEFAULT);
I don't really understand the parameters for ShellExecute and would like some clarification.
UPDATE
I think I have gotten it to work:
HINSTANCE result;
result = ShellExecute( // WinExec is obsolete.
0, // hwnd
"open", // operation/verb.
"steam://connect/192.69.96.168:27023", // executable.
"", // parameters.
NULL, // directory
SW_SHOW); // how to be displayed
Have a look here, taken from this related question. This will open the run dialog.
Then if your process has the admin rights, you might be able to call SetWindowText on the handle of the input box of the dialog, which you'll find with FindWindowEx or similar.
I really think it's easier to create your own run dialog which looks like the windows one and calls ShellExecute when "ok" is clicked.
Related
I used to hide my background app's console window until I needed it with
ShowWindow(console_hwnd,SW_HIDE);
My Windows got an update and switched to "Windows Terminal" as the default console app.
That line of code doesn't work anymore it just minimizes the console to taskbar. What is the proper way to fully hide Windows Terminal?
EDIT:
Please read the question carefully. It says HIDE the console window. NOT remove the console completely. I need to be able to show it again later. Thats the purpose of SW_HIDE and SW_SHOW that are no longer working with the new Windows Terminal.
If you want to show the console at any time, you could hide and show the console by changing the coordinates of the console.
int main() {
HWND consoleWindow = GetConsoleWindow();
SetWindowPos(consoleWindow, 0, -600, -600, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
return 0;
}
EDIT:
According to this document, the second parameter to ShowWindow() might get ignored the first time it is called. Try calling it twice.
ShowWindow(hWnd, SW_HIDE);
ShowWindow(hWnd, SW_HIDE);
In addition, did you run this program as administrator? If so, I suggest you cancel it, since the window is in a higher privileged process, so your call will be ignored.
Hope it helps.
Is there a way to programmatically hide an application on windows? I want to achieve the same thing as the windows+D shortcut, but for a single application. I want to do this from within that application (application consists of several windows, one of those can't be moved, resized, closed or minimized by the user). Application is written in c++ and uses Qt for the UI.
to do so it's so easy:
1- retrieve the handle to that window:
HWND hChild = GetDlgItem(hWnd, ID_MYCHILD);
2- send to it SW_SHOW either using ShowWindow or via SendMessage:
ShowWindow(hChild, SW_HIDE); // hide
ShowWindow(hChild, SW_SHOW); // show
SendMessage(hChild, SW_HIDE, 0, 0); // hide
SendMessage(hChild, SW_SHOW, 0, 0); // show
if the window doesn't belong to your application then:
1 - retrieve the main window with:
HWND hWnd = GetForegroundWindow(void);
2- use the above to hide/show it
ShowWindow(HwndWindow, SW_MINIMIZE);
Here's the MSDN ShowWindow documentation.
In addition you may find EnumChildWindows useful for finding all these windows if their handles aren't readily available to you.
When trying to execute cmd.exe with the SW_HIDE paramter:
ShellExecute(NULL, "open", "cmd.exe", NULL, NULL, SW_HIDE);
cmd.exe is actually executed hidden! I don't understand why this happened, does cmd.exe's console window considered to be a "normal" window that accepts the nCmdShow parameter, or does cmd.exe when executed sees that I passed SW_HIDE for the nCmdShow parameter and decides not to show the console window?
Note: I tried this code on Windows 7.
The implication is that cmd.exe simply uses the nCmdShow argument to WinMain (or that it uses SW_SHOWDEFAULT which means the same thing) when showing its window via ShowWindow, without checking or modifying the value first.
It's not documented that it will work and so you shouldn't rely on it, but it's a nice side effect that can be a good way to run batch scripts without a visible window appearing.
Of course if you actually launch an interactive cmd.exe in a hidden window it will be quite difficult to ever make it visible :)
I use:
Hwnd hStart = ::FindWindow ("Shell_TrayWnd",NULL); // get HWND of taskbar first
hStart = ::FindWindowEx (hStart, NULL,"BUTTON", NULL); // get HWND of start button
to get start button's handle. It's running properly on Windows XP,
but in Windows 7, ::FindWindowEx (hStart, NULL,"BUTTON", NULL) always returns 0, and GetLastError() returns 0, too.
Why is that?
In Windows 7 the start button, which has class name "Button", is a child of the desktop window. Your code assumes that the start button is a child of the window named "Shell_TrayWnd" which does indeed appear to be the way the taskbar and start menu were implemented on XP.
For Windows 7 you want to use something like this:
hStart = ::FindWindowEx(GetDesktopWindow(), NULL, "Button", NULL);
Although I think it would be better search for it by name to be sure that you get the right button.
hStart = ::FindWindowEx(GetDesktopWindow(), NULL, "Button", "Start");
I'm not sure how Vista implements its taskbar and start menu, but you can use Spy++ to find out.
Having said all of this, it would be much better if you can find a way to achieve your goals without poking around in such implementation specific details.
::FindWindow (L"Shell_TrayWnd",NULL);
this code is for complete taskbar
How do you open a path in explorer by code in c++. I googled and couldn't find any thing but systems commands to do this, however, i dont want it to block or show the console window.
You probably are looking for the ShellExecute() function in shellapi.h. It is called with an "action verb", a path, and optional parameters. In your case this will want either "open" or "explore" as follows:
ShellExecuteA(NULL, "open", "C:\\", NULL, NULL, SW_SHOWDEFAULT);
This will open an unattached explorer window at C:. ShellExecute() will give basically the same action as typing in a command at the Run dialog. It will also handle URLs, so the following will open up the user's default browser:
ShellExecuteA(NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWDEFAULT);
Although make sure to pay attention to the note in the documentation that ShellExecute relies on COM (Although your code doesn't have to worry about any COM objects itself).
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)
This does not show the command window, just opens the directory.
system("explorer C:\\");
I'm now using VS2017, using as follows works:
ShellExecute(NULL, L"open", L"YourFolderPath\\YourFile.xxx", NULL, NULL, SW_RESTORE);
also reference ShellExecute to open an .exe in C++