C
C#11mo ago
Rémi

Hide console app but still show tray Icon

Hey, i'm making a tray icon only app, the tray icon works fine,but not when I set the output to Windows Application to hide the console.
using System.Runtime.InteropServices;

class Program
{
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern bool Shell_NotifyIcon(int dwMessage, NOTIFYICONDATA lpData);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int NIM_ADD = 0x00000000;
const int NIM_DELETE = 0x00000002;
const int NIF_MESSAGE = 0x00000001;
const int NIF_ICON = 0x00000002;
const int NIF_TIP = 0x00000004;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
}

static void Main()
{
NOTIFYICONDATA nid = new NOTIFYICONDATA();
nid.cbSize = Marshal.SizeOf(nid);
nid.hWnd = GetConsoleWindow();
nid.uID = 1;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = 0x8000;

// Specify the path to your custom icon
nid.hIcon = LoadIcon("ico.ico");
nid.szTip = "";

Shell_NotifyIcon(NIM_ADD, nid);
ShowWindow(GetConsoleWindow(), SW_HIDE);
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

Shell_NotifyIcon(NIM_DELETE, nid);
Thread.Sleep(5000);
}

// Load icon from file
private static IntPtr LoadIcon(string path)
{
IntPtr hIcon = IntPtr.Zero;
hIcon = new System.Drawing.Icon(path).Handle;

return hIcon;
}
}
using System.Runtime.InteropServices;

class Program
{
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern bool Shell_NotifyIcon(int dwMessage, NOTIFYICONDATA lpData);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int NIM_ADD = 0x00000000;
const int NIM_DELETE = 0x00000002;
const int NIF_MESSAGE = 0x00000001;
const int NIF_ICON = 0x00000002;
const int NIF_TIP = 0x00000004;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
}

static void Main()
{
NOTIFYICONDATA nid = new NOTIFYICONDATA();
nid.cbSize = Marshal.SizeOf(nid);
nid.hWnd = GetConsoleWindow();
nid.uID = 1;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = 0x8000;

// Specify the path to your custom icon
nid.hIcon = LoadIcon("ico.ico");
nid.szTip = "";

Shell_NotifyIcon(NIM_ADD, nid);
ShowWindow(GetConsoleWindow(), SW_HIDE);
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

Shell_NotifyIcon(NIM_DELETE, nid);
Thread.Sleep(5000);
}

// Load icon from file
private static IntPtr LoadIcon(string path)
{
IntPtr hIcon = IntPtr.Zero;
hIcon = new System.Drawing.Icon(path).Handle;

return hIcon;
}
}
33 Replies
Rémi
RémiOP11mo ago
Right now, I minimise the console but it is not great I see it on start, for half a sec, and I see it in the taskbar
Jester
Jester11mo ago
you could maybe try using the get function to get the ex_style of the console window. then add the WSEX TOOLWINDOW bit to it and then set it using https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongw that should hide the window from the taskbar
SetWindowLongW function (winuser.h) - Win32 apps
Changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory. (Unicode)
Jester
Jester11mo ago
or else i suggest making a window application without a console and making your own message only window. that way you would only see the tray icon https://learn.microsoft.com/en-us/windows/win32/winmsg/window-features#message-only-windows
Window Features - Win32 apps
This overview discusses features of windows such as window types, states, size, and position.
Jester
Jester11mo ago
note its not that easy to make your own window
Rémi
RémiOP11mo ago
😮 i'll try to understand that
Jester
Jester11mo ago
not the best written out explenation
var hwnd = GetConsoleWindow();
var styles = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
SetWindowLongPtr(hwnd, GWL_EXSTYLE, styles | WS_EX_TOOLWINDOW);
ShowWindow(hwnd, SW_HIDE);
var hwnd = GetConsoleWindow();
var styles = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
SetWindowLongPtr(hwnd, GWL_EXSTYLE, styles | WS_EX_TOOLWINDOW);
ShowWindow(hwnd, SW_HIDE);
Rémi
RémiOP11mo ago
I got an error, SetWindowLongPtr need one more argument dwNewLong
Jester
Jester11mo ago
whoops i fixed it thats just what i could remember
Rémi
RémiOP11mo ago
the doc is not very expresive about what this does
No description
Jester
Jester11mo ago
this gets the extra window styles the console window has and then sets them again + the toolwindow style
Rémi
RémiOP11mo ago
the result is the exact same as before
Jester
Jester11mo ago
can you see the console window still in the taskbar?
Rémi
RémiOP11mo ago
Yes
No description
Rémi
RémiOP11mo ago
but at least the tray icon is there
Jester
Jester11mo ago
well i guess thats not possible then console windows are special
Rémi
RémiOP11mo ago
maybe if I use the original cmd, not windows terminal
Jester
Jester11mo ago
yeah then maybe or you have to make your own window but thats not easy
Rémi
RémiOP11mo ago
that's it hmm well that might do it but now i need to do something to stop the app from running x)
Jester
Jester11mo ago
whats the app supposed to do?
Rémi
RémiOP11mo ago
change a tray icon if i'm in cap mode or not So if sift is pressed, or CapsLock clicked
Jester
Jester11mo ago
oh ur writing an entire app for that some keyboards like the one of my laptop has a light in the caps lock key so i can see if its caps locked do you have a way to know if the key is locked yet?
Rémi
RémiOP11mo ago
i used to have a python app that does is using while true and vkstate but is was making me lag so rn i don't really know
Jester
Jester11mo ago
yeah dont keep calling that in a loop
Jester
Jester11mo ago
GitHub
Keylogging/samples/Console/Program.cs at main · QubitTooLate/Keylog...
Useful wrapper for Hot Keys and Raw Input in C#. Contribute to QubitTooLate/Keylogging development by creating an account on GitHub.
Jester
Jester11mo ago
you can keep track if the caps lock key has been pressed or not i also have a message only window in here so acrually this could be perfect for you just add the tray icon to the code
Rémi
RémiOP11mo ago
with kind like an event ? when a key is pressed
Jester
Jester11mo ago
yeah this uses RAW INPUT which gives events when keys get pressed
Rémi
RémiOP11mo ago
nice, i have to see how I will handle shift
Jester
Jester11mo ago
so it doesnt have to loop a function and only runs on an event, very efficient
Rémi
RémiOP11mo ago
cause it is supposed to be kept pressed
Jester
Jester11mo ago
Virtual-Key Codes (Winuser.h) - Win32 apps
The following table shows the symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order.
Rémi
RémiOP11mo ago
this was terrible, but working x) thank you to use it my project, i have to put the source code of this in the same folder ?
Jester
Jester11mo ago
yeah i havent made it the easiest thing to use. yeah put the source code in one of your folders and then reference the library project of the solution in your project so you can compile and use it
Want results from more Discord servers?
Add your server