C
C#13mo ago
Rémi

I can't use notifyIcon on my c# console app

I am trying to make a notifyIcon by i have this: The type or namespace name 'NotifyIcon' could not be found (are you missing a using directive or an assembly reference?) and when i try to fix it using: using System.Windows.Forms; i have this error : The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
23 Replies
Rémi
RémiOP13mo ago
I tryed dotnet add package System.Windows.Forms but it didn't work i am using vscode and dotnet 8
Pobiega
Pobiega13mo ago
Yeah iirc it's not trivial to get s notification icon with a console app, as you need a window handle to.. handle clicks on the icon.
Rémi
RémiOP13mo ago
actually i just want a tray icon not even a console app, or any window
Pobiega
Pobiega13mo ago
You can't "just" have a tray icon. They are not standalone things. You need an application that owns the icon and handles interactions with the icon
Rémi
RémiOP13mo ago
I don't any window if you prefer this is why i chose console app
Pobiega
Pobiega13mo ago
A reasonable assumption, but a console app doesn't by default come with a window handle. And you need a window handle to have a tray icon. If you Google ".net tray icon console app" I'm sure you'll find plenty of ways to solve this
Rémi
RémiOP13mo ago
they are using System.Windows.Forms but i can't manage to make it works
Pobiega
Pobiega13mo ago
Are they somewhat recent? Like .net 5 or newer? A guide using .net framework will likely not work
Rémi
RémiOP13mo ago
no I can't find something for .net 8
Pobiega
Pobiega13mo ago
.net 8 is one month old bruh
Rémi
RémiOP13mo ago
do you have any idea how can add windw.Forms package to this project ?
Pobiega
Pobiega13mo ago
You need to add a property to your csproj to include the winforms stuff, but I'm not sure that's enough here
Rémi
RémiOP13mo ago
How would u recommend me to do this little project ? I wanna change the tray icon if cap is activated
Pobiega
Pobiega13mo ago
Cap?
Rémi
RémiOP13mo ago
when you write in CAPITALIZE or not
Pobiega
Pobiega13mo ago
Caps lock
Rémi
RémiOP13mo ago
and shift
Pobiega
Pobiega13mo ago
Idk, without any research what so ever... I'd probably just make a winforms app and hide the window That gives you a window handle and it's then trivial to implement the icon
Rémi
RémiOP13mo ago
I lookedd at that but didn't find how to hide the window
Pobiega
Pobiega13mo ago
Isn't it just setting Visible to false on the form?
Rémi
RémiOP13mo ago
wdym ?
Pobiega
Pobiega13mo ago
I mean exactly that :p a Form object has a Visible property, that can be set to false. this hides the window at least, thats what I remember. Maybe thats for controls in general and forms need to use .Hide(); not sure winforms isnt exactly relevant these days
Rémi
RémiOP12mo ago
I finaly mange to make it, but I have an issue
using System;
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 SW_SHOW = 5;
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("C:\\Users\\SCD UM\\Desktop\\pro\\Caps\\sign_way_direction_arrow_up_icon_256189.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;
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 SW_SHOW = 5;
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("C:\\Users\\SCD UM\\Desktop\\pro\\Caps\\sign_way_direction_arrow_up_icon_256189.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;
}
}
This works, but I can't hide the console app I tryed to set the output from console app to windows applcation but when I do that i don't have a tray icon So i try to hide the windw but it just minimised it
Want results from more Discord servers?
Add your server