I was building a simple form with one simple effect- opacity reduced when mouse is not over the form, and form becomes opaque when mouse is over it. I am currently encountering couple of difficulties:-
Firstly, I did this-
this.MouseHover += new EventHandler(Form1_MouseHover);
this.MouseLeave += new EventHandler(Form1_MouseLeave);
But I had 1 richtextbox in form too, and as mouse went over it, the form lost opacity again. I had to add this too:-
richTextBox1.MouseHover+=new EventHandler(Form1_MouseHover);
richTextBox1.MouseLeave+=new EventHandler(Form1_MouseLeave);
wondering if there was any better way,because there is still some gap between richtextbox and form boundaries, and form is losing opacity when mouse cursor goes there.
If the mouse is NOT over the form (suppose initially), the form is less opaque. Now, I want form to become opaque as soon as mouse goes over it, but it only happens when mouse movement over form stops completely. If I keep moving mouse over the form, it does not become opaque. Is this a problem with the way events are stored in message queue and all that or will I be able to do something, because I have seen applications with the effect I am trying to implement.
The MouseEnter/Leave events are too unreliable to do this. Best thing to do is just use a Timer that checks if the mouse is still inside the window. Drop a Timer on the form and make the code look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Opacity = 0.99;
timer1.Interval = 200;
timer1.Enabled = true;
timer1.Tick += timer1_Tick;
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
timer1_Tick(this, e);
}
private void timer1_Tick(object sender, EventArgs e) {
this.Opacity = this.Bounds.Contains(this.PointToClient(Cursor.Position)) ? 0.99 : 0.20;
}
}
Btw: avoid increasing the Opacity to 1.0, that forces the native window to be recreated and that can have a lot of side-effects. Using 0.99 is best.
I might be wrong, but why would you use MouseHover event? MouseHover detects when the mouse stop moving on the form and is usually used to show Tooltips.
The event you are looking for is MouseEnter which is the opposite of MouseLeave and detects when the mouse enters the client rect of a window.
In the Leave event, just check if the cursor position is in the window client rect to know if it did actually leave the form or if it is just on top of child control.
Ofc if you use a region, you'll have to adapt the code.
private void Form1_MouseEnter(object sender, EventArgs e)
{
this.Opacity = 1;
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
{
this.Opacity = 0.5;
}
}
Add a timer control then use below in timer's tick event. Above answers won't work if you have custom/user controls in your form. So have to use ClientRectangle
this.Opacity = this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)) ? 0.99 : 0.20;
private void Form1_MouseEnter(object sender, EventArgs e)
{
this.Opacity = 1.0;
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Opacity = 0.8;
}
Related
How do I drag borderless Form? I tried almost everything but most of the solutions are pretty confusing as I don't have decent experience in C#.
so i find this approach. feel free to delete if its duplicate but for me it looks the most simple.
in order to drag window we need some sort of loop that runs while we have mouse button pressed, im doing it with timer.
also we need variable that will store initial mouse press location.
using System.Timers;
using Timer = System.Timers.Timer;
public partial class Form1 : Form
{
private static Timer moveTimer;
private static Point pnt;
public Form1()
{
InitializeComponent();
moveTimer = new Timer();
moveTimer.Interval = 0.1;
moveTimer.Enabled = false;
moveTimer.Elapsed += moveTimerEvent;
moveTimer.AutoReset = true;
}
next we just add MouseDown and MouseUp Events to every control we want to be able to move window. usually i have this event on menustrip like most browsers do nowadays but you can use any control
private void AddHub_MouseDown(object sender, MouseEventArgs e)
{
pnt = new Point(e.Location.X+(sender as Control).Location.X, e.Location.Y + (sender as Control).Location.Y);
moveTimer.Enabled = true;
}
private void AddHub_MouseUp(object sender, MouseEventArgs e)
{
moveTimer.Enabled = false;
}
we are getting mouse location with respect to control with e.location and then controls location with respect to form with (sender as Control).Location
so mouse location with respect to form is mouse location with respect to control + controls location with respect to form
and finally we use timer event moveTimerEvent to actually drag a window
private void moveTimerEvent(Object source, ElapsedEventArgs e)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { Location = new Point(Cursor.Position.X - pnt.X, Cursor.Position.Y - pnt.Y); }));
}
}
we need to invoke a method as timer moveTimerEvent is running in another thread and cant directly affect Form1.
this is a pseudo code to calculate Form1 location from mouse location: Form1.location = Cursor.Position - pnt and this location is with respect to screen.
I have project with several hundreds of buttons, created dynamically within for-loop. I also have timer to update toolstripstatuslabel (labelClock) with current time every second:
static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
...
timer.Tick += new System.EventHandler(timer_Tick);
...
private void timer_Tick(object sender, EventArgs e)
{
labelClock.Text = DateTime.Now.ToString();
}
Here's the problem: last clicked button will be focused, of course. So if I scroll page down, everytime timer ticks page scrolls up (or down), so the focused button becomes visible.
How can I prevent that?
Stupid solution (won't work, if you need to save focus of button):
private void timer_Tick(object sender, EventArgs e)
{
justAnotherButton.Focus();
labelClock.Text = DateTime.Now.ToString();
}
You need to pass focus to another focusable control, it may be another Button, TextBox, etc. You may use button with zero Width and Height, so user won't see it.
If you are not concerned with keeping the same button focussed, set the focus to the parent form. Although this sounds like a bit of a nightmare to deal with, and i'd look for a way not not needing hundreds of buttons!
private void timer_Tick(object sender, EventArgs e)
{
formMain.Focus()
labelClock.Text = DateTime.Now.ToString();
}
You could create a container control that extends System.Windows.Forms.ContainerControl and override the ScrollToControl to return the current scroll position. Then add all your buttons to this control.
class MyContainer : ContainerControl
{
protected override System.Drawing.Point ScrollToControl(Control activeControl)
{
return base.AutoScrollPosition;
}
}
I have the following problem: I have a panel which has a specific color, say red.
When the user presses his mouse, the color of this panel gets stored in a variable. Then the user moves, his mouse still pressed, over to another panel. When he releases the mouse there, this panel should get the background color of the first that had been stored in the variable. My code looks something like this:
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
I need to identify the sender first because there are many possible panels that can trigger this event. The first MouseDown method works totally fine, the color is stored nicely in the variable. The secon one however doesn't even get triggered when the user does what I described above. When the ser clicks on the second panel, it works (there is an MouseUp part in a click aswell I guess).
What's wrong here? Why is the event not triggered when the user holds the mouse key down before?
(This answer assumes you are using Windows Forms.)
It could be that you need to capture the mouse by setting this.Capture = true in the MouseDown of the source control. (See Control.Capture)
If you did that, the source window would get the MouseUp event, and it would be the source window that had to determine the destination window under the mouse coords. You can do that using Control.GetChildAtPoint() (see this answer on Stack Overflow).
Use Windows Forms Drag and Drop Support Instead! <- Click for more info
I'm going to suggest you bite the bullet and use the .Net Drag and Drop methods to do this. It requires some reading up, but it will be much better to use it.
You start a drag in response to a MouseDown event by calling Control.DoDragDrop().
Then you need to handle the Control.DragDrop event in the drop target control.
There's a few more things you might need to do to set it up; see the Control.DoDragDrop() documentation for an example.
(For WPF drag and drop support, see here.)
when your mouse enter the target control , mouse down triggerd ang get target BackColor! you need add an boolean flag to your code :
public Color currentColor;
bool flag=false;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
if(flag==false)
{
flag=true
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
}
//assume mouse up for panles
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
if(flag==true)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
flag=flase;
}
}
and also you need change your flag in mouseMove( if )
As I mentioned in my comment Mouse Events are captured by the originating control, You would probably be better off using the DragDrop functionality built into Windows Forms. Something like this should work for you. I assigned common event handlers, so they can be assigned to all of your panels and just work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
((Control)sender).DoDragDrop(((Control)sender).BackColor,DragDropEffects.All);
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Control)sender).BackColor = (Color)e.Data.GetData(BackColor.GetType());
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I know it's an old question but I had the same issue and none of the above answers worked for me. In my case I had to handle the MouseMove event in the target control and check for the mouse to be released. I did set 'BringToFront' on my target panel just in case that helped at all.
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void panelTarget_MouseMove(object sender, MouseEventArgs e)
{
//the mouse button is released
if (SortMouseLocation == Point.Empty)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
}
I have an application. If the application is not being used for a certain amount if time, it should hide. When application is hidden and we mouse-over the icon, it should be restored.
How can I do this? Thanks in advance.
You have to define a timer in your application that will count the time when mouse is not over the form/window. Then just hide your application.
Download WPF NotifyIcon
And handle MouseOver event, that will show Form/Window
EDIT:
If you do not need to minimize application to tray and hide window keeping it on desktop -> use the same algorithm, but do not hide the window, just set transparency to 0% or 10%. When mouse is over - set transparency to 100%.
Like JesseJames said, use a timer to measure the inactive time of the application and hide it after an amount of time. Re-activate it when the mouse is hovered over the NotifyIcon. Here's a sample WindowsForms solution that does the job:
private Timer _timer;
private int _ticks;
public Form1()
{
_timer = new Timer { Interval = 1000, Enabled = true };
_timer.Tick += TimerTick;
Activated += Form1_Activated;
MouseMove += Form1_MouseMove;
//notifyIcon1 is an icon set through the designer
notifyIcon1.MouseMove += NotifyIcon1MouseMove;
}
protected void TimerTick(object sender, EventArgs e)
{
//After 5 seconds the app will be hidden
if (_ticks++ == 5)
{
WindowState = FormWindowState.Minimized;
Hide();
_timer.Stop();
_ticks = 0;
}
}
protected void NotifyIcon1MouseMove(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
Show();
_ticks = 0;
_timer.Start();
}
protected void Form1_MouseMove(object sender, MouseEventArgs e)
{
_ticks = 0;
}
Perhaps there might exist a cleaner solution, I don't know, but it gets you on the way. Same principle will go for WPF, only the code will be slightly different. Hope this helps!
To see if the user has made any input you can use a similar approach like this one. To get your application visible again you need a way to get the global mouse and maybe keyboard input, to do this you can use hooks, you can find one solution for that here. And if the hook is triggered it really just depends on what kind of UI you are using, but calling specific hide or show methods should be suffice.
Purpose is to have the opacity event trigger when the form loses focus. The form has a setting for STAY ON TOP. The visual effect would be to click on a possibly overlapping window, and yet the form when not focused on would stay on top, but in the corner slightly transparent, keeping it within easy access, but providing visibility to the stuff underneath.
I 've googled and googled, and can't figure out how to get this event to properly fire when form loses focus, and then when form gains focus back to restore opacity to 100% or the level determined elsewhere.
Tips?
// under designer.cs
//
// CollectionToolForm
//
//other code....
this.LostFocus += new System.EventHandler(goTransparent);
//method
private void goTransparent(object sender, EventArgs e)
{
if (transparentCheck.Checked == true)
{
this.Opacity = 0.50;
}
else
{
this.Opacity = 1;
}
}
It sounds as if you are looking for the Activated and Deactivate events.
Update
In response to the comment about LostFocus event, it could be of interest to clarify how it works. The LostFocus event of the Form is inherited from Control. It is raised when a controls loses focus; either because the form as such is being deactivated (focus moves to another application for instance), or because focus moves to another control within the same form.
If you hook up an event handler for the LostFocus event of a form that contains only at least one control that can receive focus, you will find that the LostFocus event of the form is raised immediately after the form is displayed. This is because focus moves from the form (which is a Control) to the first focusable control on the form.
So, the form being active and the form being focused are two separate behaviours.
You tried doing it with mouse enter/leave events?
public Form1()
{
this.MouseEnter += new System.EventHandler(this.Form1_MouseEnter);
this.MouseLeave += new System.EventHandler(this.Form1_MouseLeave);
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Opacity = 0.5;
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
this.Opacity = 1;
}