In my working environment I can have up to 10 command prompt windows open, each one set up to work in a different context. Having all of them open, I find myself having to switch between several of them to find the right one that I want to work with.
I am already setting different foreground and background colors of each window based on some criteria, but it would be much more easier to distinguish between them by having a different colored icon in the taskbar. That way, I would not even have to maximize/bring them to focus to find the right one from the get-go.
Is there a way that I can change the taskbar icon of the currently running command prompt window programmatically by executing batch commands in it?
There is no "built-in" way to do that, like there is the color command from cmd.exe to change the colors.
You could either search the internet for some utilitiy, or roll your own, for example in C#, by invoking the SetConsoleIcon Win32 API. Note however, that this API is not officially documented, YMMV.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern uint SetConsoleIcon(IntPtr iconHandle);
static void Main(string[] args)
{
if (args[0].Equals("--reset", StringComparison.OrdinalIgnoreCase))
{
SetConsoleIcon(IntPtr.Zero);
}
else
{
// Use this to load an icon from an icon file instead:
// var icon = new Icon(args[0]); // load from .ico file
// Extract icon from given executable/dll.
using (var icon = Icon.ExtractAssociatedIcon(args[0]))
{
if (icon != null)
SetConsoleIcon(icon.Handle);
}
}
}
}
You should be able to compile this using csc.exe setconico.cs (assuming you named the file setconico.cs). This will generate setconico.exe, which you can use like this:
Set the current console icon of the console you run this in, to the icon of notepad.exe
c:\> setconico.exe c:\windows\notepad.exe
You might also be able to write the above code in PowerShell, if you don't want to compile a separate utility.
Related
On Windows 10, you can press Win+Tab to get a "Task View" view of all your windows. I'm trying to check if this is active at any given time. I have tried using a Low Level Keyboard Hook with WH_KEYBOARD_LL but this only allows me to detect the keypress, not if the switcher is active. I've looked at the Windows DWM API and haven't found anything else either.
I have also tried using EnumWindows() and EnumChildWindows(GetDesktopWindow(), ...) and did not find any difference in the output between having the task view shown and hidden.
Is there any accurate method to detect if this is being shown?
Here's a solution that works very consistently with my version of Windows (1709 build 16299.125) and doesn't require the processor-heavy approach of a call to EnumChildWindows:
bool isTaskView() {
//Get foreground window's name
HWND fgWindow = GetForegroundWindow();
TCHAR windowName[MAX_PATH] = L"";
GetWindowText(fgWindow, windowName, MAX_PATH);
//Compare with magic string name of Task View's window
std::wstring nameStr(windowName);
return nameStr == L"Task View";
}
I want to add command line options to my WPF application but in case command line is sent into my EXE file i don't want the UI open but only command line options.
Is it possible to do something like this ?
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
this.Hide();
ParseArgs(args);
}
else
{
InitializeComponent();
}
}
private void ParseArgs(string[] args)
{
// bla bla
}
}
You should do this in your app.xaml.
I explain :
You put an application startup method handler(instead of using the startup uri). Here you can parse the cl arguments and then set StartupUri = MainWindow.xaml which will open your window. and if you won't open main window then you don't set startup uri.
This Replacing WPF entry point is what you want to do.
In there you can get the command line arguments and decide if you want to show the main window or not.
If you need to write output that is shown in the command line when the application is run, you need to do something like this :
Right click on the project, "Properties", "Application" tab, change "Output Type" to "Console Application", and then it will also have a console.
However, once you do that then you will have a console window pop up even if you start the application not from a command line. There is no way to have it both ways - either the application is a command line application which can launch a window, or it is a window application which cannot write to the console which started it. It is a limitation in Windows - the bit that decides it is in the PE header.
This question discusses this point in great detail, and offers several hacks to achieve what you want.
You might want to actually search for your problems, because I count at least 5 SO questions dedicated to this (new) topic already.
I am creating a GUI in using VC++ CLR windows form and wanted to make a hotkey to restore my windows from the system tray that I have minimized. I've found that RegisterHotKey is a way to make a global hotkey in the system but I don't understand how to make use of it inside my code.
Any thoughts??
First you need to #include the Windows headers, put it in the stdafx.h precompiled header file for example:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#pragma once
#include <Windows.h>
#pragma comment(lib, "user32.lib")
The #pragma ensures that the linker will link the import library for user32.dll, required to link RegisterHotKey().
Next, inside the Form class you need to override the OnHandleCreated() method. It will run whenever the native window for the Form is created, something that can happen more than once. Make that look like this:
protected:
virtual void OnHandleCreated(EventArgs^ e) override {
__super::OnHandleCreated(e);
RegisterHotKey((HWND)this->Handle.ToPointer(), 1,
MOD_ALT | MOD_CONTROL, (UINT)Keys::F1);
}
I hard-coded the hotkey to Ctrl+Alt+F1, change that to the one you want to use. You can add additional hotkeys, change the id argument (2nd argument, I used 1).
Then you need to detect the WM_HOTKEY message that windows will send to you when the user presses the key. That requires overriding the form's WndProc() method, like this:
protected:
virtual void WndProc(Message% m) override {
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == 1) {
this->WindowState = FormWindowState::Normal;
this->BringToFront();
}
__super::WndProc(m);
}
Test this by minimizing the form, pressing Ctrl+Alt+F1 and you'll see the window getting restored and moved back into the foreground.
Thanks for your help Hans, I tried with the codes but it didn't really work like I wanted it to. My program will be minimized into the system tray with the codes below
private:
System::Void MyForm::MyForm_Resize(System::Object^ sender, System::EventArgs^ e) {
if (WindowState == FormWindowState::Minimized)
{
Hide();
}
}
If I commented out the part where it hides as it minimize, it worked out fine though.
sir i want to know that how could i navigate/toggle between the different exe files using c#.net code on my .net application on the button click event..basically i want to do toggling between the current and previously opened running exe files..im able to run an exe file on my .net application but not able to provide an option for opening the previously running exe file...on the whole i want to provide a facility same as we are having in the OS windows that we could toggle between the applications on the taskbar menu
how could i do the same in the c#.net application using c# code.
thanks
Although not sure if I got you correctly but if you want to invoke a new exe on any event use:-
System.Diagnostics.ProcessStartInfo f = new System.Diagnostics.ProcessStartInfo("C:\\windows\\system32\\rundll32.exe",
"C:\\windows\\system32\\shimgvw.dll,ImageView_Fullscreen " +
fileName.TrimEnd (null));
try
{
// Pass the ProcessStartInfo object to the Start function.
System.Diagnostics.Process.Start (f);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine (ex.ToString ());
}
I have a C++/CLI GUI application and I want to display an image as a visual aid for the user to see what step in a procedure they're at. This image will need to be changed each time the user selects the new step.
Currently I'm using a picture box and have an image loaded from the disk at run time. So there are a few things I need to know here:
Is a picture box the best thing to use for this purpose or is there another control that would better suit?
How do embed the images in the executable and load them from there instead of a file that exists on disk.
How do I load a new image (I'm guessing that this will be fairly obvois if I can crack point 2)?
I've seen a few answers which relate to C# but I've not seen anything which looks like it translates to doing things in a C++/CLI app. Any suggestions would be very welcome.
Well it may not be the best solution, but the following works.
Create a new Windows Forms Application
Add these libraries to your linker settings (Project Proerties -> Link -> Input -> Additional Dependencies):
User32.lib Gdi32.lib
Add these headers:
#include <windows.h>
#include "resource.h"
Add these namespaces:
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
Add a pair of bitmaps to your resources and call them IDB_BITMAP1 and IDB_BITMAP2.
Add a picture box called m_pictureBox1.
Add a button and double-click the button to add an on-click handler:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// Remove any previously stored images
if(m_pictureBox1->Image != nullptr)
{
delete m_pictureBox1->Image;
}
// Pick a new bitmap
static int resource = IDB_BITMAP1;
if( resource == IDB_BITMAP2)
{
resource = IDB_BITMAP1;
}
else
{
resource = IDB_BITMAP2;
}
// Get the primary module
Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];
// Get the instance handle
IntPtr hinst = Marshal::GetHINSTANCE(mod);
// Get the bitmap as unmanaged
HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR);
// import the unmanaged bitmap into the managed side
Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));
// insert the bitmap into the picture box
m_pictureBox1->Image = bi;
// Free up the unmanaged bitmap
DeleteObject(hbi);
// Free up the instance and module
delete hinst;
delete mod;
}
..et voila the bitmaps are stored neatly in you app and each time you click the button the images will swap.