WinForms cursor hidden only on one Form - c#

I have a C# application with 2 simultaneous visible forms, and I need to hide mouse cursor when it is over only on one of them. If I use Cursor.Hide() it applies the change for both of them.

You need to implement this logic by using the MouseEnter and MouseLeave events one each form something like:
private void frm1_MouseEnter(object sender, EventArgs e)
{
Cursor.Hide();
}
private void frm1_MouseLeave(object sender, EventArgs e)
{
Cursor.Show();
}
do the abobe on the form that should hide the cursor and add this to the form that should make the cursor visible:
private void frm2_MouseEnter(object sender, EventArgs e)
{
Cursor.Show();
}

You can make a "blank" cursor, and set myForm.Cursor = blankCursor; This will make that specific form show a specific cursor, which could be completely transparent.

Did you try this.Cursor = Cursors.None, instead of Cursor.Hide()?

You could use the Control.MouseEnter and Control.MouseLeave events to trigger hiding or displaying the cursor

If you're hiding the cursor so that the user can't do anything on the form, consider using this.UseWaitCursor = true; instead.

Related

WindowState and the Maximum/Minimize buttons Event are different

Before starting the question, I apologize for not using English well.
I don't want to use Border and Minimized/Maximized/Closed buttons in the existing form.
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Then, I created Custom Minimized/Maximized/Closed Button.
//Closed
private void imgLblCloseProgram_Click(object sender, EventArgs e) {
this.Close();
}
//Maximized
private void imgLblMaximumProgram_Click(object sender, EventArgs e) {
if(this.WindowState == FormWindowState.Maximized) {
this.WindowState = FormWindowState.Normal;
} else if(this.WindowState == FormWindowState.Normal) {
//this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; //★★★
this.WindowState = FormWindowState.Maximized; //
}
}
private void tbPnlTopBar_DoubleClick(object sender, EventArgs e) {
imgLblMaximumProgram_Click(imgLblMaximumProgram, e);
}
//Minimized
private void imgLblMinimumProgram_Click(object sender, EventArgs e) {
this.WindowState = FormWindowState.Minimized; //
}
When Clear the annotation, Maximizes without taking up space on the taskbar.
However, if more than one monitors are used, they do not work on screens other than the main screen.
In addition, for the Maximized/Minimized button, there seems to be a built-in animation provided by Windows.
When the Maximized/Minimized was changed via FormWindowState, I confirmed that the animation did not work.
The biggest reason why I want the Low-cost Maximized Button Event is that when I use more than one monitors, I have a issue.
Therefore, I need a way to maximize the form while displaying TaskBar on two or more monitors.
p.s. I want the animation of the maximized/minimized button as well, so the best way to run the maximized/minimized button's event is.
I solved this.
when I use more than one monitors, I have a issue(they do not work on screens other than the main screen).
If you still want to use FormWindowState.Maximized on more than one monitor, this is likely to be an appropriate solution.
this.MaximizedBounds = new Rectangle(new Point(Screen.PrimaryScreen.WorkingArea.X, Screen.PrimaryScreen.WorkingArea.Y), new Size(Screen.FromHandle(this.Handle).WorkingArea.Width, Screen.FromHandle(this.Handle).WorkingArea.Height));
However, there is an error on the screen with higher resolution than the main monitor.

C# Form - MouseClick will click the actual control after losing form focus

If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.

Using MouseHover event and ToolTip

To show the relevant information(in monopoly game, the property belongs which player, current market price etc.), I put a Label on the top of a panel, and used a ToolTip object to display the information. This is the image of my current setup.
Here are the steps I have done:
1.Added MouseHover event handler (The Label name is MEDITERANEAN)
this.MEDITERANEAN.MouseHover += new System.EventHandler(this.MEDITERANEAN_MouseHover);
2.Initialized Tooltip
private void InitializeToolTip()
{
toolTipLabel.ToolTipIcon = ToolTipIcon.Info;
toolTipLabel.IsBalloon = true;
toolTipLabel.ShowAlways = true;
}
3.Call setToolTip() in MouseHover call back function
private void MEDITERANEAN_MouseHover(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(MEDITERANEAN, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
But when I start application and move my cursor over the label, there is no text from toolTipLabel. What part did I make mistakes?
Interestingly, i made other function and it works.
private void panelBoard_MouseOver(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(panelBoard, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
I think you just need to bring your lable control in front of image. Try something like this .
MEDITERANEAN.BringToFront();
I found the solution, first I should set Panel's property "Enable" to true, then set label's property "visible" to true as well.

change cursor over a pictureBox

i was trying to implemet an image button in winforms application as i can ...easy when using asp.net
the problem seem to be(i suspect) that when the mouse is over the image inside the picturebox
it is not responding or not triggering the mouseEnter event
it looks like if i had a picture that is smaller than the pictureBox Size it will accept the reason to trigger the event but over the image within the pictureBox it would Not ?
the trick was to set pictureBox to sizeMode=zoom. then do 2 things when the mouse is over the "imageButton" : change the size of PictureBox a little larger + change cursor to hand
so i will get a kind of mouse over effect as i could with asp.net
did anyone have that problem ?
at first i tried mouseHover, then i thought enter would do better as it only requiers the mouse to pass the borders of the picture box... both enter and hover events did not work for me ...
Edit :
the event does trigger , i can see that if i initially set sizemode to CenterImage and inside the event
i ask for sizemode=zoom, so the effect dose occur ..but cursor.current=Cursors.Hand will not change.
This should work
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Cursor = Cursors.Hand;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Cursor = Cursors.Default;
}
seem like i should have known better how to use Cursors class .
cursor=Cursors.hand;
rather than
cursor.current=Cursors.hand;
that was embarrassing ..
only add MouseMove event on pictureBox and set a Cursor for this
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Cursor = Cursors.Hand;
}

Hiding form when are other controls focus

This question is related to this my question. Now I have form in my class and when user click on button I show (or hide) form. That´s ok. But I want to hide form when I move with origin form or when I click somewhere in origin form. The new form is behind that origin form. I was trying events like lostfocus and others but It didn´t help. So I think I need some trick that check from my control if there was click in parrent form (origin form) or some other hack. I know the best would be that I put code but I have many lines so I think that best way will be if you help me in general way and then I try to applicate to my app.
You can do it with a global mouse and keyboard hook. In fact, its been wrapped up into well documented, well structured .NET API over at CodePlex
Go over there and download it. Then, set up a global mouse hook:
_mouseListener = new MouseHookListener(new GlobalHooker());
_mouseListener.MouseMove += HandleGlobalHookMouseMove;
_mouseListener.Start();
The key here is that you will receive the MouseMove event ANY time the mouse moves ANYWHERE on the desktop, not just within the bounds of your window.
private void HandleAppHookMouseMove(object sender, MouseEventArgs e)
{
if (this.Bounds.Contains(e.Location))
{
HandleEnter();
}
else
{
HandleLeave();
}
}
You can also setup one for MouseClick. The combination of the two will enable you to determine any time the mouse moves over your origin form, or the mouse is clicked when its over it. Unlike the LostFocus and other events you tried, focus is irrelevant.
Does below help?
public partial class Form1 : Form
{
Form f2 = new Form2();
public Form1()
{
InitializeComponent();
f2.Show();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.ClientRectangle.Contains(e.Location) && f2.Visible) { f2.Hide(); }
}
private void button1_Click(object sender, EventArgs e)
{
f2.Visible = !f2.Visible ? true : false;
}
}

Categories