This question already has answers here:
.Net Console Application in System tray
(6 answers)
Closed 5 years ago.
I´m using C#, I want to hide the console application but I want to show the icon in "hidden taskbar" (I don´t know if it is the correct name), something similar like Thunderbird, Team Viewer....
I´ve tried this:
[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_SHOWMINIMIZED = 2;
const int SW_SHOW = 5;
const int SW_MINIMIZE = 6;
private const string AppGuid = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9";
// ==============================
// ==============================
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
But If I hide the console it is not shown in the "Hidden Task Bar". Is it posible to hide the console and show in the "Hidden Task Bar"?
the keyword which you must search for is "notification icon", then you will find something like How to show a message with icon in the notification area
i can not explain it more precise because i don't know whether you are using wpf or a console application
Related
I have written a program that change the windows theme but after changing the theme personalization window remains open and I want to close it. I tried using process.kill() with familiar process name but it didn't work. Thank you.
The code for what I am doing is as below:
ProcessStartInfo theinfo = new ProcessStartInfo(themepath + "aero.theme");
theinfo.CreateNoWindow = true;
Process thepr = new Process();
thepr.StartInfo = theinfo;
thepr.Start();
where "themepath" is String location to aero.theme.
I have even enabled CreateNoWindow to true then also it opens up Personalization to change theme but didn't close it automatically.
First use find window to get the window from their name by Using FindWindow..
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);
It returns you the handle of the window you want now you can use send message to close it..
[DllImport("User32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
private void closeWindow()
{
// retrieve the handler of the window
int iHandle = FindWindow("CabinetWClass", "Personalization");
if (iHandle > 0)
{
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
You need to obtain the window handle by it's name and then send it a close message. This prevents having to kill any processes. See this article for information on obtaining the windows. See this one for closing windows from the handle.
After seeing the code and doing a little digging, you can accomplish this with two registry edits. You should read this article and just have your program edit the two registry keys in question.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I'm currently developing an app that uses System.Diagnostics.Process to get the Window Title of the Current Active App or the App on the ForeGround. Now, this is fine and working with the current code I have until I came across with this issue. This NullReferenceException occurs if I switch my app using the task bar icon instead of the minimized button of the app window.
Edit:
It maybe confusing if I don't raise this up. So basically NullReferenceException is caused by (title.Equals(System.Diagnostics.Process.GetCurrentProcess().MainWindowTitle))
title variable is null due to
String title = GetActiveWindowTitle();
GetActiveWindowTitle() returns null. And this uses Window.GetForegroundWindow(); And for some reason using taskbar icons to switch up, this Window.GetForegroundWindow(); method returns nothing.
Originally I used this
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
To get what I need. But I'm still having the same issue when switching app using the task bar icon. The only difference with using this code is that the exception is this
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
My operating system is Windows 8.1.
Any inputs is greatly appreciated.
Update
The answer from #KcDoD was somewhat correct. And since the NullReferenceException was actually part of my question I will mark it as the Answer. And it also guides me a lot to figure out what's going on.
Basically, on my application I call this method GetActiveWindowTitle() within WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime). WinEventProc() is a delegate method that triggers when you switch window. And it turns out that by calling GetForegroundWindow(); or GetActiveWindowTitle() the process is not active yet that's why when it goes to get the Active application or Process it gives/return null.
Best regards
Your Problem is related to the String you are trying to Equal with MainWindowTitle . That means the title variable/field.
You should check tite for null before trying to use Equal
Additionally : Here is a program using answer from here
static void Main(string[] args)
{
String answ;
while (true)
{
answ = GetActiveWindowTitle();
if (answ == null)
{
Console.WriteLine("NO active program");
}
else {
Console.WriteLine(answ);
}
Thread.Sleep(1000);
}
}
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
static private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
Answer : If there is no process selected then this will return null. I think that's the answer for your question.
I have a WPF app that starts another application, I'd like for my application to change the Icon of this second app. I am able to use GetWindowText and SetWindowText to change the title. Is it possible to do this for the Icon as well?
update
I have no control of the second app.
To change the window title of another application:
Definitions of Win32 API functions and constants:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hwnd, int message, int wParam, IntPtr lParam);
private const int WM_SETICON = 0x80;
private const int ICON_SMALL = 0;
private const int ICON_BIG = 1;
Usage:
Process process = Process.Start("notepad");
// If you have just started a process and want to use its main window handle,
// consider using the WaitForInputIdle method to allow the process to finish starting,
// ensuring that the main window handle has been created.
// Otherwise, an exception will be thrown.
process.WaitForInputIdle();
SetWindowText(process.MainWindowHandle, "Hello!");
Icon icon = new Icon(#"C:\Icon\File\Path.ico");
SendMessage(process.MainWindowHandle, WM_SETICON, ICON_BIG, icon.Handle);
In Windows Forms you would use
Icon ico = Icon.ExtractAssociatedIcon(#"C:\WINDOWS\system32\notepad.exe");
this.Icon = ico;
So im guessing for WPF it would be similar.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to make the Close button disabled in a windows Form using C# coding
I want to disable Close button in a form can any one help me in this.
The following should help:
[DllImport("user32.dll")]
private static extern int GetSystemMenu(int hwnd, int revert);
[DllImport("user32.dll")]
private static extern int GetMenuItemCount(int menu);
[DllImport("user32.dll")]
private static extern int RemoveMenu(int menu, int position, int flags);
private void DisableCloseButton()
{
int menu = GetSystemMenu(Handle.ToInt32(), 0);
int count = GetMenuItemCount(menu);
RemoveMenu(menu, count - 1, MF_DISABLED | MF_BYPOSITION);
}
This question already has answers here:
Simulating Key Press C#
(8 answers)
Closed 4 years ago.
I have an application with combobox that contains names of currently running applications. As I understood from msdn library, SendKeys method can send keys only to active application. Is it somehow possible in .NET, to send keys also to inactive app? Or at least in WinAPI ?
You can use the SendMessage() API function to send keystrokes to an inactive window.
With C# <3 is everthing possible :D
No need to be active window, as u wished.
Also here a usefull list of Virtual Key Codes
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
const int WM_SYSKEYDOWN = 0x0104;
const int VK_KEY_A = 0x41;
IntPtr WindowToFind = FindWindow(null, "Window Name");
//In ur case u have to write a code that translates the combobox into Virtual Key Codes. Will take time but it shouls be easy
PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_KEY_A, 0);
//PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0);
}