I wrote a windows form application which can change my GateWay automatically by executing two Batch files.
Problem is that I want to show "check.ico" icon beside the item in ContextMenu list when I click on it.
Here is my code :
public Form1()
{
InitializeComponent();
}
Image MenuStripImage = Image.FromFile(#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\Check.ico");
NotifyIcon TrayIcon;
ContextMenuStrip TrayMenu;
private void Form1_Load(object sender, EventArgs e)
{
Image MenuStripExit = Image.FromFile(#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\exit.ico");
this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = false;
this.Hide();
TrayIcon = new NotifyIcon();
TrayMenu = new ContextMenuStrip();
TrayMenu.Items.Add("GreenPAcket").Click += new EventHandler(GreenpacketSelect);
//if (DLinkSelectcliced == true && GreenPacketSelected == false)
//{
TrayMenu.Items.Add(MenuStripImage);
//}
TrayMenu.Items.Add("D-Link DSL 2890AL 5GHz").Click += new EventHandler(DLinkSelect);
//if (GreenPacketSelected == true && DLinkSelectcliced == false)
//{
TrayMenu.Items.Add(MenuStripImage);
//}
TrayMenu.Items.Add("Exit", MenuStripExit).Click += new EventHandler(Exit);
TrayIcon.ContextMenuStrip = TrayMenu;
TrayIcon.Visible = true;
TrayIcon.Icon = new Icon(#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\WindowsFormsApplication2\router.ico");
}
private void Exit(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
// bool DLinkSelectcliced = false;
private void DLinkSelect(object sender, EventArgs e)
{
// DLinkSelectcliced = true;
Process Proc = new Process();
Proc.StartInfo.FileName = (#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\Dlink.lnk");
Proc.Start();
TrayMenu.Items.Add(MenuStripImage);
// GreenPacketSelected = false;
}
// bool GreenPacketSelected = false;
private void GreenpacketSelect(object sender, EventArgs e)
{
// GreenPacketSelected = true;
Process Proc = new Process();
Proc.StartInfo.FileName = (#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\GreenPacket.lnk");
Proc.Start();
TrayMenu.Items.Add(MenuStripImage);
// DLinkSelectcliced = false;
}
If check.ico is a check mark, you don't need it for ContextMenuItem because it has option to show check mark, just enable CheckOnClick property for that item.
But if you want to show your custom icon instead, you must first convert it to image
You can do it yourself or use this method below to let C# do it for you
Use method here to convert icon to bmp image:
http://www.dotnetfunda.com/codes/show/967/icon-to-bitmap-in-csharp-winforms
Or simply create .png/.bmp version of this icon instead.
But first you should add this icons as resources to your project because if this icons will not exist in the location you gave it won't work (so you won't be able to use it on another PC).
To do it open solution explorer, right click on project and select properties, then go to resources, select icons, click Add Resource and add all icons.
Then to load icons from resources you simply type:
notifyIcon1.Icon = Properties.Resources.iconName
To show icon as image in your ContextMenuItem method in the link like this:
menu1ToolStripMenuItem.Image = ConvertFromIconToBitmap(Properties.Resources.iconName, new Size(16,16))
Btw, I glanced at your code and I think there is a lot of problems there...
Related
I am using AxMSTSCLib to develop a Windows App for creating RDP connections.
The steps listed below caused my remote desktop display disappear:
Start an RDP connection with full-screen mode
Click "restore down" button from connection bar
Click "maximize" button again to re-enter full-screen mode
Click "minimize" button
Then my app disappears, I can't see it in taskbar (but still be listed / running in taskmanager)
If I skip steps 2 & 3, it won't disappear when I click "minimize" button from connection bar, it's really weird.
I post some part of my code here, hope anyone can help me to find out the problem.
public partial class RdpShowForm : Form
{
public AxMSTSCLib.AxMsRdpClient9NotSafeForScripting oRemote;
public RdpShowForm()
{
InitializeComponent();
oRemote = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(this.oRemote)).BeginInit();
oRemote.Dock = System.Windows.Forms.DockStyle.Fill;
oRemote.Enabled = true;
oRemote.Name = "WindowsVM";
this.Controls.Add(oRemote); // Controls contains 'panel1' and 'oRemote'
((System.ComponentModel.ISupportInitialize)(this.oRemote)).EndInit();
oRemote.CreateControl();
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
}
private void RdpShowForm_Load(object sender, EventArgs e)
{
NewRdpConnectionInstance();
}
private void NewRdpConnectionInstance()
{
oRemote.Server = 'xxx';
...
oRemote.FullScreen = true;
oRemote.AdvancedSettings7.DisplayConnectionBar = true;
oRemote.AdvancedSettings7.ConnectionBarShowMinimizeButton = true;
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = true;
oRemote.OnConnected += rdpClient_OnConnected;
oRemote.OnLeaveFullScreenMode += rdpClient_OnLeaveFullScreenMode;
oRemote.Connect();
oRemote.Show();
oRemote.Focus();
}
private void rdpClient_OnConnected(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.Visible = false;
}
private void rdpClient_OnLeaveFullScreenMode(object sender, EventArgs e)
{
this.Visible = true;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == MAXIMIZE)
{
this.Visible = false;
oRemote.FullScreen = true;
oRemote.Show();
}
}
base.WndProc(ref m);
}
}
I've tried some methods, but none of them worked.
oRemote.Bounds = Screen.PrimaryScreen.WorkingArea;
oRemote.IsAccessible = true;
this.ShowInTaskbar = true;
or
this.Visible = ture; // do not hide the main form
or
if (m.WParam.ToInt32() == MAXIMIZE)
{
oRemote.FullScreen = true;
oRemote.Show();
oRemote.Update();
oRemote.Refresh();
}
So far, the only solution that came into my mind is to disable the restore button, like this:
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = false;
This way isn't fix the problem, just avoid the issue to be triggered.
I would appreciate any help.
Since AxMSTSCLib has a restore down issue, I turn to use a form to handle its fullscreen mode. In this way, I can get extensive control over full-screen mode behavior, write code to tell the container what to do when the restore down button is called.
// enable container-handled full-screen mode
oRemote.AdvancedSettings7.ContainerHandledFullScreen = 1;
Then, customize OnRequestGoFullScreen, OnRequestLeaveFullScreen, OnRequestContainerMinimize.
oRemote.OnRequestGoFullScreen += corey_OnRequestGoFullScreen;
oRemote.OnRequestLeaveFullScreen += corey_OnRequestLeaveFullScreen;
oRemote.OnRequestContainerMinimize += corey_OnRequestContainerMinimize;
private void corey_OnRequestGoFullScreen(object sender, EventArgs e)
{
// set the form to fullscreen
}
private void corey_OnRequestLeaveFullScreen(object sender, EventArgs e)
{
// set the form back to non-fullscreen mode
}
private void corey_OnRequestContainerMinimize(object sender, EventArgs e)
{
// minimize the form
}
The method I posted here can be a workaround to this issue.
I have a question!
I'm currently making a notepad app and everything was going well until now.
The way it's set up:
There's a panel with the menu buttons (File, Edit, Themes, & Format) and a panel at the bottom with all of the options under each tab. Both the menu button and sub-buttons are labels.
When you click a menu button, the bottom panel will show the buttons categorized under that section by making all labels there invisible, and then making the appropriate ones visible.
Everything seems to work fine except for the 2 buttons under Format (Font & Font Color). They show up, but do not do anything when clicked. Below is the code for it.
private void FontButton_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
if(fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fd.Font;
}
}
private void FontColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if(cd.ShowDialog() == DialogResult.OK)
{
richTextBox1.ForeColor = cd.Color;
}
}
private void Label4_Click(object sender, EventArgs e)
{
Config();
FontButton.Visible = true;
FontColor.Visible = true;
}
private void Config()
{
New.Visible = false;
Open.Visible = false;
Save.Visible = false;
Light.Visible = false;
Dark.Visible = false;
FontButton.Visible = false;
FontColor.Visible = false;
Undo.Visible = false;
Cut.Visible = false;
Copy.Visible = false;
Paste.Visible = false;
}
Is the code wrong? Is it because the labels are stacked?
If someone could lend a helping hand, that'd be great! Thanks in advance.
i was following the instruction on page
but then, there's no icon attached for the application, so after the form is hidden, i cannot reshow the form, since there's no icon on the system tray,
how do i resolve this ?
here is my code
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
cmd.cetakSukses(ident.judul_App + " Diperkecil ke dalam System Tray");
notifyIcon1.BalloonTipText = ident.judul_App + " Diperkecil ke dalam System Tray";
notifyIcon1.BalloonTipTitle = ident.judul_App;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else
{
}
}
update :
i have attached the icon, and the icon still not showing on the system tray
and i figured how to make the form showing, i need to add the following code to notifyicon
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
You can set the notify icon at design using the Properties sheet:
Or you can add/change the icon property at runtime using the following code:
notifyIcon1.Icon = new Icon("appicon.ico");
This is how i implemented through code behind for a WPF app.
System.Windows.Forms.NotifyIcon m_NotifyIcon;
public StartWindow()
{
InitializeComponent();
m_NotifyIcon = new System.Windows.Forms.NotifyIcon();
m_NotifyIcon.Icon = new System.Drawing.Icon(IconPath);
m_NotifyIcon.Visible = true;
m_NotifyIcon.BalloonTipTitle = "Tip here";
m_NotifyIcon.Text = "Text here";
m_NotifyIcon.DoubleClick += delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
try
{
if (m_NotifyIcon != null)
m_NotifyIcon.Dispose();
}
catch { }
base.OnClosing(e);
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
You need to give you application an icon either by using visual studio or programatically.
You can do it in VS by going to the project properties and selecting the application tab
Or you can set it at runtime if you have icon files attached to your project already.
private NotifyIcon appIcon = new NotifyIcon();
appIcon.Icon = new System.Drawing.Icon("myApp.ico");
I have an application that goes to system tray when I minimize it. I created a notify icon to handle some secondary options for the app using right click from mouse using that notify icon.
But I would like the application not to disappear from task bar when I minimize it and mantain the notify icon on system tray.
Is there any way to acomplish thtat?
EDIT: When I minimize the application, I use the Hide() command to use the NotifyIcon. But I want it to remain on task bar.
See code here:
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized) return;
Hide();
ShowInTaskbar = true;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(2000);
}
Note: This NotifyIcon is embeded on a WPF container programatically like this:
DrawNotifyIcon();
private void DrawNotifyIcon()
{
try
{
string source = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MainWindow)).CodeBase);
string tmpSource = source + #"\Resources\mainico.ico";
tmpSource = tmpSource.Replace(#"file:\", "");
// notify Icon
notifyIcon = new NotifyIcon
{
BalloonTipTitle = Cultures.Resources.Title,
BalloonTipText = Cultures.Resources.NotifyIconExecuting,
BalloonTipIcon = ToolTipIcon.Info,
Icon = new System.Drawing.Icon(tmpSource)
};
notifyIcon.DoubleClick += notifyIcon_DoubleClick;
notifyIcon.Click += notifyIcon_Click;
notifyIcon.MouseUp += notifyIcon_MouseUp;
// Create ContextMenu
contextMenu = new ContextMenuStrip();
contextMenu.Closing += contextMenu_Closing;
// Exit item
menuItemExit = new ToolStripMenuItem
{
Text = Cultures.Resources.Exit,
Image = Cultures.Resources.close
};
menuItemExit.Click += menuItemExit_Click;
// Restore item
menuItemRestore = new ToolStripMenuItem
{
Text = Cultures.Resources.Restore,
Image = Cultures.Resources.restore1
};
menuItemRestore.Click += menuItemRestore_Click;
// Active or inactive log
menuItemActive = new ToolStripMenuItem
{
Text = Cultures.Resources.On,
Image = Cultures.Resources.green,
Checked = true
};
menuItemActive.Click += menuItemActive_Click;
menuItemActive.CheckStateChanged += menuItemActive_CheckStateChanged;
// Order of appearance of ContextMenu items
contextMenu.Items.Add(menuItemActive);
contextMenu.Items.Add("-");
contextMenu.Items.Add(menuItemRestore);
contextMenu.Items.Add(menuItemExit);
notifyIcon.ContextMenuStrip = contextMenu;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Any idea on how to keep both icons for WPF?
Well, Its not possible to show forms in taskbar which is in hidden state. Still you can forcefully minimized the form. try below modified code :
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized) return;
this.ShowInTaskbar = true;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(2000);
this.WindowState = FormWindowState.Minimized;
}
I have an app that minimizes to the system tray by clicking on "close" button and I want to save it's state (position, all elements (comboboxes, textboxes) with their values, etc).
Now I wrote this code, but it creates a new window from tray (instead of recovering the old one, with its parameters):
# app.xaml.cs:
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
// create a system tray icon
var ni = new System.Windows.Forms.NotifyIcon();
ni.Visible = true;
ni.Icon = QuickTranslator.Properties.Resources.MainIcon;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
var wnd = new MainWindow();
wnd.Visibility = Visibility.Visible;
};
// set the context menu
ni.ContextMenu = new System.Windows.Forms.ContextMenu(new[]
{
new System.Windows.Forms.MenuItem("About", delegate
{
var uri = new Uri("AboutWindow.xaml", UriKind.Relative);
var wnd = Application.LoadComponent(uri) as Window;
wnd.Visibility = Visibility.Visible;
}),
new System.Windows.Forms.MenuItem("Exit", delegate
{
ni.Visible = false;
this.Shutdown();
})
});
How I can modify this code for my problem?
When you hold a reference to your `MainWindow´ then you can simple call Show() again after closing it. Closing the Window will simply hide it and calling Show again will restore it.
private Window m_MainWindow;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
if(m_MainWindow == null)
m_MainWindow = new MainWindow();
m_MainWindow.Show();
};
If you´re sure that the MainWidnow is your Applications primary Window then you can also use this:
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
Application.MainWindow.Show();
};
I would prefer the first variant since it´s explicit.