I have one class to register/unregister hotkey. It works perfectly when application start with Form Load event.
private Hotkey myHotKey;
private IntPtr thisWindow;
private void Form1_Load(object sender, EventArgs e)
{
thisWindow = FindWindow(null, "Form1");
myHotKey = new Hotkey(thisWindow);
myHotKey.RegisterHotKeys();
}
Now problem is I want to hide application in system tray at start but its not registering my host key , When I run below code it is displaying me successfully Notify() and other things except my hotkey have no effect.:
public Form1()
{
InitializeComponent();
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
notifyIcon1.Click += notifyIcon1_Click;
notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
openToolStripMenuItem.Click += openToolStripMenuItem_Click;
exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
Notify("Application Name", "Application Started...", 1000);
thisWindow = FindWindow(null, "Form1");
myHotKey = new Hotkey(thisWindow);
myHotKey.RegisterHotKeys();
}
Can you please guide me what I am doing wrong. Thank you
Thanks guys for help, I came across this example which explained everything in better way:
http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html
hotkey = new GlobalHotkeys();
hotkey.RegisterGlobalHotKey( (int) Keys.F11, GlobalHotkeys.MOD_CONTROL, this.Handle);
Related
Is it possible to create a form that always remains under other applications?
Because the following property exists:
this.TopMost = true;
but there is no property like:
this.BottomMost = true;
Each time the user clicks on the form, it is not positioned at the top level, as normally happens, but remains below other applications. However when the user presses show desktop or Win + D, the desktop is shown but with the form on top.
The form is shown as a kind of Windows Gadget, but it is not a gadget since in Windows 10 they are difficult to activate.
If the form doesn't need to be shown at all times, then just use this code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Activated += Form1_Activated;
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D && e.Control)
{
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
this.Show();
}
}
private void Form1_Activated(object sender, EventArgs e)
{
this.Hide();
}
}
However, you need to add a COM reference named "Microsoft Shell Controls And Automation"
Also, you will need to change the Form1_KeyDown to something out of this: Global keyboard capture in C# application
I am currently making a full-screen application in C# but I have a bit of a problem. I have a overview form which shows buttons. When you click a button it will open a new form. (Overview form and new form are both full-screen). The problem is when the new form opens it will show nothing for a second (It doesnt really look to good in a full screen application). So does anyone know how to fix this? You can find the code here:
Code for opening new form:
void NewForm(Form form)
{
form.FormClosing += new FormClosingEventHandler(form_Closed);
form.Show();
Hide();
}
void form_Closed(Object sender, EventArgs e)
{
Show();
}
One of the opened forms:
public AdminPanelForm()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
private void AdminPanelForm_Shown(object sender, EventArgs e)
{
List<User> users = User.GetAllUsers();
foreach(User user in users)
{
ListViewItem lvi = new ListViewItem(user.UserName);
lvi.SubItems.Add(user.Role);
listUsers.Items.Add(lvi);
}
}
private void btnBack_Click(object sender, EventArgs e)
{
Close();
}
Thanks in Advance!
After form.Show() add form.Update(). Otherwise, the real form redraw will be done with delay. It may be better to call Hide first.
I'm trying the get the inputted values from the user and save it on a char variable, but the problem is that nothing occurs, and I think the problem is with the Form Focus, this is the code, and when I run it no errors occurs, but also nothing happen. What I did wrong?
char keyPressed;
public FrmZigndSC()
{
InitializeComponent();
this.Focus();
}
private void FrmZigndSC_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
keyPressed = e.KeyChar;
LblResult.Text += Convert.ToString(keyPressed);
}
You can try with this code - Based on KeyPressEventHandler
public FrmZigndSC()
{
InitializeComponent();
this.Focus();
//Subscribe to event
this.KeyPress += new KeyPressEventHandler(FrmZigndSC_KeyPress);
}
private void FrmZigndSC_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
keyPressed = e.KeyChar;
LblResult.Text += Convert.ToString(keyPressed);
// Indicate the event is handled.
e.Handled = true;
}
If you want to recieve a key notification from the application'e mesage pipeline, relaying on focus of the element, in this case, make architecture fragile. You can not gurantee that from other forms in your app that one would be focused, or the form is nto covered by some control that absorbes that event. You can not forse to a form having a focus, cause it's completely bad UX design (not very sure even if this is possible to implement in 100% working way).
What you can do it, instead, is declare class derived from IMessageFilter:
public class MessageFilter : IMessageFilter
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
public bool PreFilterMessage(ref Message m)
{
// Intercept KEY down message
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;
if ((m.Msg == WM_KEYDOWN)
{
//get key pressed
return true;
}
else
{
return false;
}
}
}
and after register it within your application:
MessageFilter filter = new MessageFilter(); //init somewhere
Application.AddMessageFilter(filter ); // add
.....
//on application end don't forget to remove it
Application.RemoveMessageFilter(filter );
I tried to reproduce it in an empty little Windows Forms project. This code worked just fine without the Shown event handler:
public partial class FrmZigndSC : Form
{
public FrmZigndSC()
{
InitializeComponent();
this.KeyPress += (s, e) => this.LblResult.Text += e.KeyChar.ToString();
// this might be a solution, but i did not need it
this.Shown += (s, e) => this.Activate();
}
}
You could try to use this.Activate() anyway and see if it helps. If you got other input controls such as text boxes on your form, try setting the form's KeyPreview property to true.
Using the FocusManager instead of this.Focus() should do the trick!
http://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.aspx
I Think the problem is that when you have differenct controls on your Form that catch ketpressed. I had the same probmel with an DateTimePicker on my control.
Try to delete all of them and then try it, it will work. And from that point add the controls again to see which on is the problem.
The code you have given is working fine for me. Set the startup page as FrmZigndSC and try again.
I'm very new to programming C#, though I've scripted C# in unity3D for a few years.
I'm currently trying to make a WPF tray icon, all the sources I've found on the net tell me to use
System.Windows.Forms
However .Forms is not available in System.Windows for me, and I have no idea why not. Can anyone help me with this?
You need to add references to the System.Window.Forms and System.Drawing assemblies and then you use it like this. Suppose you try to minimize the Window to tray icon and show it again when user click that icon:
public partial class Window : System.Windows.Window
{
public Window()
{
InitializeComponent();
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("Main.ico");
ni.Visible = true;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
}
You need to add a reference to the System.Windows.Forms.dll and then use the NotifyIcon class.
http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx
I'm having two forms namely One and Two , and the title text of both are same as their names.
One is having a button and the click event of it contains,
Two l_objTwo = new Two();
l_objTwo.Show();
MessageBox.Show("Two Closed");
It opens the Two and after that the "Two Closed" message will be popped up.
My scenerio is, that Two should be opened in a Modeless way i.e i need the control of One and at the same time i need to execute some functionality after Two got closed. As of now I'm using like,
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(String sClassName, String sAppName);
Two l_objTwo = new Two();
l_objTwo.Show();
IntPtr l_objHandle = FindWindow(null, "Two");
while ((int)l_objHandle > 0)
{
l_objHandle = FindWindow(null, "Two");
Application.DoEvents();
}
MessageBox.Show("Two Closed");
Its working fine, but is this the only way to achieve this?
No, it's not the only way.
Your first form will attach a handler to either FormClosed or FormClosing public events of the second form.
When your second form closes it will raise the event and the first form will know that your second form is closed.
Just use this snippet:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.FormClosed += new FormClosedEventHandler(FormClosedHandler);
f.Show();
}
void FormClosedHandler(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Second form has closed.");
}
Use an event handler
var two = new Two();
two.Closed += OnTwoClosed;
two.Show();
private void OnTwoClosed(object sender, EventArgs e)
{
MessageBox.Show("Two Closed")
}
Haven't tested this code on a winforms Form but the principal is the same, and easier than using the Windows API with all its inherent pitfalls.
Another benefit is, the titles of the windows can be whatever you like.
After checking here it appears both Closed and FormClosed are valid events.
This looks all around messy. Not sure what you want to achieve here. Just open form two in One's event handler and hook two's onclose-event to your MessageBox.