This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(C#) How to detect when a windows form is being minimized
First of all my code:
private void Form1_Load(object sender, EventArgs e)
{
this.MinimumSizeChanged += new EventHandler(Form1_MinimumSizeChanged);
}
void Form1_MinimumSizeChanged(object sender, EventArgs e)
{
MessageBox.Show("1");
}
so, my program idea is, when the program get minized i will do a system tray, but this event never happnd. how can i know when the user do a minize screen(minize the program).
i have tryed everything. any ideas?
MinimumSizeChanged has nothing to do with the form getting minimized. MinimumSizeChanged has to deal with when the MinimumSize properties of the form are changed.
You want to check the Resize event of the form.
In order to minimize to the system tray, add a NotifyIcon control to your form. Now, override the OnResize method and check if your WindowState property is set to FormWindowState.Minimized. If it is, Hide() your forms and show your notify icon. Make sure to set the Icon property of the NotifyIcon as well. Now just reverse the process for the case when the form is restored.
Related
This question already has answers here:
Set global hotkeys using C#
(3 answers)
Closed 3 years ago.
I'm making an app that will control my rgb lights and I want it to handle event for clicking specific key, but i want to run it as a background process.
I have already tried using this:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F9)
{
Debug.WriteLine("F9 down");
}
}
It works, but only if form window is active and visible, so I'm not satisfied. I'm looking for a solution that will work even if the form is invisible.
When a keydown event happens and a form is not focused / active it will not detect the event. However, you can use Win API to create a key listener of sorts. See this link for more details of "Global Hotkeys".
This question already has answers here:
Show a Form without stealing focus?
(19 answers)
Closed 6 years ago.
I have a sort of a pop-up windows form. It pops up to show gathered data. Problem is it gains focus when it loads and interrupts work. I need it not to get focus when loaded. How to do that?
the Simplest thing you can do is, first load all the data which you gathered and when u feel its ready and you want to load the popup window, call the
window.show()
this will help you to use the data once its loaded. Also it would be better if you can share more details on the same.
In Your form class, override OnLostFocus(EventArgs e) and place a focusing call inside it. So it looks like this:
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
this.Focus();
}
Sometimes if that doesn't work try:
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.Focus();
}
This question already has answers here:
Detect event when user clicks the program on the taskbar?
(2 answers)
Closed 9 years ago.
Is there any event occurred when I click on an icon of an application that is running and is minimized on taskbar? I want to call my method when the icon is clicked.
The method is coded into the app's resource. Please view picture to get more information:
You can use the from Activated event.
public Form1()
{
InitializeComponent();
this.Activated += Form1_Activated;
}
private void Form1_Activated(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
//TODO: take required action here
}
}
P.S.:I am assuming that you are looking a solution for winform application.
If you're using WPF, you can use the Window.StateChanged Event. Definition of this event:
Occurs when the window's WindowState property changes.
For WindowsForms, there is no StateChanged event. You'll have to use the SizeChanged Event and check the WindowState yourself. Like this:
private void Form1_Resize(object sender, EventArgs e)
{
switch(this.WindowState)
{
case FormWindowState.Minimized: //Your minimized-event code here;
break;
case FormWindowState.Maximized: //Your maximized-event code here;
break;
default: //state is 'Normal':
}
}
Whenever a window is minimized, maximized, or restored, Window.StateChanged event is fired. You can trap this event and call your function.
If you want to do it for the same application in which your code is then you handle the Resize and SizeChanged event of the Form control for WinForms application and StateChanged event of Window control for WPF application. When user clicks on the taskbar icon of an application which is minimized to task bar, it is restored and the Resize(WinForms), SizeChanged(WindForms) and StateChanged(WPF) events are raised which you can handle and check for the WindowState whether it is maximized or normal.
If you want to do it for any other application then I think you will have to use SetWindowsHookEx Win32 API function ( as described here) so that you will get a collection of open windows in OS and will have to iterate through all such windows to identify and hook into their Resize event.
This question already has answers here:
Why does modal-form disappear completely when minimized?
(5 answers)
Closed 7 years ago.
I have problem with minimizing by program. It has main form and others, which I open by ShowDialog()
Problem is with minimizing these dialog forms. When my friend on Windows7 minimize this dialog, it minimize dialog and main form (dialog isn't in taskbar, I disabled it), but on my Windows7 only dialog is minimized like this
Is here any chance to minimize both forms? I found, that after form is minimized, Resize event is fired, so I made this
private void ShowCode_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
main.WindowState = FormWindowState.Minimized;
}
}
but when I click on form in taskbar, main form is showed and dialog is closed, I don't know why?
Is here any chance, to always minimize it to taskbar?
And why on my PC it works this way, and on friend's other way? I don't know that I changed something in Windows settings.
If I understand you correctly - I found a solution after asking a similar question.
Simply add a: Show();
At the end of Form2's event-handler.
See about it here (and in the other answers.)
This question already has answers here:
Can I choose a custom image for C# Windows Application Drag Drop functions?
(2 answers)
Closed 9 years ago.
We need to display a feedback to the user when he drags-in items into our application.
Our client prefers this feedback to be in form of a custom cursor.
This is already implemented for drag-out, using a custom cursor that is set in GiveFeedback event handler (raised by DoDragDrop when dragging items out of our app). The GiveFeedbackEventArgs allows us to specify UseDefaultCursors property - setting this to false allows us to override the cursor.
However, the DragOver event handler argument, which is the equivalent of GiveFeedback, does not have UseDefaultCursors property and changing the cursor from there does not have any effect.
Sample (this has no effect):
private void browser_DragOver(object sender, DragEventArgs e) {
Cursor.Current = Cursors.WaitCursor;
}
The drag operation originates from outside our app. (for in-app drag, it works using the GiveFeedback event.
How to change the cursor when receiving a drag? Is this even possible/feasible?
void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e) {
e.UseDefaultCursors = false;
}
Yes, you must implement a COM interfaces (IDragSourceHelper and IDropTargetHelper).
Take a look HERE.
Would this suffice?
This code will change the mouse pointer by adding a [+] sign next to it.
private void Form_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
We've addressed a whole range of drag-drop issues here:
Drag and Drop between Instances of the same Windows Forms Application
Hopefully this will point you in the right direction.