Rémi
Rémi
CC#
Created by Rémi on 1/1/2024 in #help
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;
}
}
54 replies
CC#
Created by Rémi on 12/24/2023 in #help
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?)
42 replies
CC#
Created by Rémi on 8/22/2023 in #help
❔ Find last element in dictionnary, using a list as path
Hey, I have a dictionnary:
c#
Dictionary<string, object> tree => new()
{
{ "show", new Dictionary<string, object>() { { "10", null } } },
{ "clear", null },
{ "true", null },
{ "false", null}
};
c#
Dictionary<string, object> tree => new()
{
{ "show", new Dictionary<string, object>() { { "10", null } } },
{ "clear", null },
{ "true", null },
{ "false", null}
};
like this, and i wanna make a function that return me the next/end word. Like if I input 'sh' i wanna have 'ow', and if I input 'show' I wanna have 10, but if i input 'show 10' then only return a null value.
10 replies
CC#
Created by Rémi on 7/3/2023 in #help
❔ Interactive both in y and x axis, in cmd
Hello :), I am trying to do an interactive menu in a command line application that use both the x and y axis, you navigate using keyboard arrows. I did a well working one that is only using the x axis, but with both this is way more complicated. I wrote a code that kind of works but for any reason the Console.Clear is not doing what i was expecting :
public static void Editing()
{
int x = 0;
int y = 0;
List<Option> x_options = new List<Option>();
List<string> y_options = GetPrompt();
List<string> special = new List<string>() { "%path", "%user", "%os", "%workingset", "%time", "%date", "%newline" };
List<string> colors = new List<string>() { "%fore:", "%back:", "%style:" };
while (true)
{
if (special.Contains(y_options[y]) || colors.Contains(y_options[y]))
{
x_options.Add(new Option(CustomCMD.GetValue("tips"), () => { }));
}
x_options.Add(new Option(CustomCMD.GetValue("edit"), () => { }));
x_options.Add(new Option(CustomCMD.GetValue("add_right"), () => { }));
x_options.Add(new Option(CustomCMD.GetValue("add_left"), () => { }));
x_options.Add(new Option(CustomCMD.GetValue("delete"), () => { }));

WriteComplexMenu(x_options, y_options, x_options[x], y_options[y]);
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.RightArrow)
{
y--;
if (y < 0) y = y_options.Count - 1;
}
else if (key.Key == ConsoleKey.LeftArrow)
{
y++;
if (y >= y_options.Count) y = 0;
}
else if (key.Key == ConsoleKey.UpArrow)
{
x--;
if (x < 0) x = x_options.Count - 1;
}
else if (key.Key == ConsoleKey.DownArrow)
{
x++;
if (x >= x_options.Count) x = 0;
}
else if (key.Key == ConsoleKey.Enter)
{
}
}
}

private static void WriteComplexMenu(List<Option> x_options, List<string> y_options, Option x_selected, string y_selected, string title="", string end="")
{
Console.Clear();
if (title != "") Console.WriteLine(title);
foreach (Option option in x_options)
{
if (option == x_selected)
{
Console.Write(" > ");
}
else
{
Console.Write(" ");
}
Console.WriteLine(option.Name);
}
Console.WriteLine(end);
int tab = 0;
bool found = false;
foreach (string option in y_options)
{
if (option == y_selected)
{
found = true;
tab += Math.Abs(option.Length / 2);
}
else if (!found)
{
tab += option.Length + 1;
}
Console.Write(option + " ");
}
Console.WriteLine();
for (int i = 0; i < tab; i++)
{
Console.Write(" ");
}
Console.Write("^");
}
class Option
{
public string Name { get; }
public Action Selected { get; }

public Option(string name, Action selected)
{
Name = name;
Selected = selected;
}
}
public static void Editing()
{
int x = 0;
int y = 0;
List<Option> x_options = new List<Option>();
List<string> y_options = GetPrompt();
List<string> special = new List<string>() { "%path", "%user", "%os", "%workingset", "%time", "%date", "%newline" };
List<string> colors = new List<string>() { "%fore:", "%back:", "%style:" };
while (true)
{
if (special.Contains(y_options[y]) || colors.Contains(y_options[y]))
{
x_options.Add(new Option(CustomCMD.GetValue("tips"), () => { }));
}
x_options.Add(new Option(CustomCMD.GetValue("edit"), () => { }));
x_options.Add(new Option(CustomCMD.GetValue("add_right"), () => { }));
x_options.Add(new Option(CustomCMD.GetValue("add_left"), () => { }));
x_options.Add(new Option(CustomCMD.GetValue("delete"), () => { }));

WriteComplexMenu(x_options, y_options, x_options[x], y_options[y]);
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.RightArrow)
{
y--;
if (y < 0) y = y_options.Count - 1;
}
else if (key.Key == ConsoleKey.LeftArrow)
{
y++;
if (y >= y_options.Count) y = 0;
}
else if (key.Key == ConsoleKey.UpArrow)
{
x--;
if (x < 0) x = x_options.Count - 1;
}
else if (key.Key == ConsoleKey.DownArrow)
{
x++;
if (x >= x_options.Count) x = 0;
}
else if (key.Key == ConsoleKey.Enter)
{
}
}
}

private static void WriteComplexMenu(List<Option> x_options, List<string> y_options, Option x_selected, string y_selected, string title="", string end="")
{
Console.Clear();
if (title != "") Console.WriteLine(title);
foreach (Option option in x_options)
{
if (option == x_selected)
{
Console.Write(" > ");
}
else
{
Console.Write(" ");
}
Console.WriteLine(option.Name);
}
Console.WriteLine(end);
int tab = 0;
bool found = false;
foreach (string option in y_options)
{
if (option == y_selected)
{
found = true;
tab += Math.Abs(option.Length / 2);
}
else if (!found)
{
tab += option.Length + 1;
}
Console.Write(option + " ");
}
Console.WriteLine();
for (int i = 0; i < tab; i++)
{
Console.Write(" ");
}
Console.Write("^");
}
class Option
{
public string Name { get; }
public Action Selected { get; }

public Option(string name, Action selected)
{
Name = name;
Selected = selected;
}
}
Editing is the main code that run the menu and handle the user input
29 replies
CC#
Created by Rémi on 5/18/2023 in #help
❔ Playing a video using avalonia ui
Hey, i would like to play a video in a part of my window in avalonia, I found this https://github.com/jpmikkers/LibVLCSharp.Avalonia.Unofficial but i don't understand how it works
6 replies