My Form has a problem with that GroupBbox MouseEvents.
I'm trying to make some GUI gadgetries (docking, opacity..).
Here is an example:
I've linked all (GUI)-Objects to these two functions.
private void MyMouseMove(object sender, MouseEventArgs e)
{
this.Opacity = 1;
}
private void MyMouseLeave(object sender, EventArgs e)
{
this.Opacity = 0.5;
}
..expect the group panels, because they don't have MouseMove and MouseLeave events. Can they be added? A standard Panel has them as well.
I really like the layout of that GroupPanels (with that border and text), that's why I would love to be able to solve that problem with GroupBox.
That gadgets I create will only be triggered, if the cursor is in- or outside the form. (doesn't matter if inactive or active). Maybe there is another way to trigger it, than MouseMove and MouseLeave.
Using a Timer is probably the simplest solution!
Thank you LarsTech for linking to this 'Winform - determine if mouse has left user control' question.
I'm able to continue my Project with this sample below.
public partial class Form1 : Form
{
private Timer timer1;
public Form1()
{
InitializeComponent();
this.Opacity = 0.5D;
timer1 = new Timer();
timer1.Interval = 200;
timer1.Tick += timer1_Tick;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.DesktopBounds.Contains(Cursor.Position))
this.Opacity = 1D;
else
this.Opacity = 0.5D;
}
}
credits goes to: Hans Passant
Related
I have an PictureBox that I want to move up on the y axis after a button click. The problem is that the PictureBox simply appears there after the button click. I want it to move to the new position, not teleport. What do I do?
public partial class Form1 : Form
{
Point stageplus1 = new Point(164, 325);
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox5.Location = stageplus1;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
}
}
Expanding on BJ Myers comment this is how you can implement that:
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
// calculate new position
this.pictureBox1.Top++;
this.pictureBox1.Left++;
// when to stop
if (this.pictureBox1.Top > (this.Height - (2 * this.pictureBox1.Height)))
{
this.timer1.Enabled = false;
}
}
I used the default Timer control that raises a Tick at an interval. The Tick event gets executed on the UI thread so you don't have to be bothered by cross thread errors.
If added to your form this is what you'll get:
If you need to animate more stuff you might want to look into using a Background worker and helper classes like I show in this answer of mine
I have a windows form application with a PictureBox control containing an image. I want to move the PictureBox control to the right in a slow movement. Here is my code:
Point currentPoint = pictureBox_Logo.Location;
for (int i = 0; i < 25; i++)
{
pictureBox_Logo.Location = new Point(pictureBox_Logo.Location.X + 1, pictureBox_Logo.Location.Y);
Thread.Sleep(30);
}
The problem here is that when the code executes instead of seeing the picture move, I see a white picture move and the moving stops until the picture appears.
What am I missing and what can I do about it?
Code:
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}
original thread is here Move images in C#
Try to use pictureBox_Logo.Refresh() after Thread.Sleep(30);
Or look for standard Timer control.
My code is good written, but what I did wrong was putting the code in an event:
private void Form1_Shown(object sender, EventArgs e);
But when I put my code in a button it works without any problems.
i want the mouse to freez (cant move) when mouse down
thanks
I used a tableLayoutPanel for your reference (Just remember to implement the code to the Control that is in the front):
OPTION1: Reset the mouse position:
Define two global variables:
bool mousemove = true;
Point currentp = new Point(0, 0);
Handel MouseDown Event to update mousemove :
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
int offsetX = (sender as Control).Location.X + this.Location.X;
int offsetY = (sender as Control).Location.Y + this.Location.Y;
mousemove = false;
currentp = new Point(e.X+offsetX, e.Y+offsetY); //or just use Cursor.Position
}
Handel MouseMove to disable/enable move:
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (!mousemove)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = currentp;
}
}
Reset mousemove while Mouseup
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
mousemove = true;
}
OPTION2: Limit mouse clipping rectangle:
Limit it while MouseDown:
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = new Rectangle(Cursor.Position, new Size(0, 0));
}
Release it after MouseUp:
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = Screen.PrimaryScreen.Bounds;
}
You can't.
Mouse acts in the OS Layer, not your app... even if you freeze your app, mouse will be able to run.
You can try to disconnect the mouse driver/port but you do need to ask the user what port the mouse is using as for the OS it's a Input device, just like a pen in a design board and you will not know the one to disconnect.
It's possible, Windows has a dedicated API for it, BlockInput(). Be sure to save all your work when you experiment with it, it is rather effective. You may need to reboot your machine, the thing your user will do when you use it in a program. Here's a sample Windows Forms form that uses it, it needs a button and a timer:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 3000;
timer1.Tick += new EventHandler(timer1_Tick);
button1.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e) {
timer1.Enabled = true;
BlockInput(true);
}
private void timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
BlockInput(false);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool BlockInput(bool block);
}
You can fake that behavior for your window in the following way:
Remember current cursor and its position.
Set
this.Cursor = Cursors.None;
Draw the remembered cursor at specified position and introduce canExecute flag for all your mouse handlers to disable them during "fake mouse freezing".
Can't you move the mouse pointer somewhere? You could reset its position when moving (which may look ugly).
Setup a low level mouse hook with SetWindowsHookEx and ignore all messages to the HOOKPROC delegate you specified (means not to call CallNextHookEx).
I have a fom which has one user control docked to fill.
this user control displays different images.each image has Id and I have a list of imageId vs imageDetail object dictionary.
Mouse Move event of this user control is captured and i am displaying current X and Y position of mouse in tool tip.
I also want to display image detail in tool tip when user keeps the mouse over image for some time.
I tried to do this with Mouse Hover event but it only raised when mouse enters in user control bound. after this if i move mouse within user control mouse hover event does not fire...
How can i display current X, Y position along image detail in tool tip.
is there any way to simulate Mouse Hover event within Mouse Move using some timer.
Is there any sample code..
I solved this problem by
public partial class Form1 : Form
{
Timer timer;
bool moveStart;
int count = 0;
Point prev;
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
this.timer.Stop();
this.moveStart = false;
this.toolTip1.SetToolTip(this, string.Format("Mouse Hover"));
this.textBox1.Text = (++count).ToString();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.prev.X == e.X && this.prev.Y == e.Y)
return;
if (moveStart)
{
this.prev = new Point(e.X, e.Y);
this.timer.Stop();
this.toolTip1.SetToolTip(this, string.Format("Mouse Move\nX : {0}\nY : {1}", e.X, e.Y));
this.timer.Start();
}
else
{
moveStart = true;
}
}
}
The simplest way would be to call the MouseOver subroutine from the MouseMove subroutine as such:
void MouseMove(object sender, MouseEventArgs e)
{
//Call the MouseHover event
MouseHover(sender, e);
}
void MouseHover(object sender, EventArgs e)
{
//MouseHover event code
}
If you want more control over when and how to display your tooltip, however, you'll need to do something similar to the following:
Declare a listening variable at class level.
Hook to the MouseHover event so the listening variable is turned on when the mouse enters.
Hook to the MouseLeave event so the listening variable is turned off when the mouse leaves.
Put your tooltip code in the MouseMove handler so it displays your tooltip if the listening variable is on.
Here's some code to demonstrate what's I outlined above.
class Form1
{
bool showPopup = false;
void MouseHover(object sender, EventArgs e)
{
showPopup = true;
}
void MouseLeave(object sender, EventArgs e)
{
showPopup = false;
toolTip.Hide(this);
}
void MouseMove(object sender, MouseEventArgs e)
{
if (showPopup)
{
toolTip.Show("X: " + e.Location.X + "\r\nY: " + e.Location.Y,
this, e.Location)
}
}
}
Of course, you'll have to add a ToolTip with the name toolTip and associate the various methods (subroutines) with the appropriate events of your control (Form, PictureBox, etc).
Is there any way to make the form semi-transparent while it is being moved and then become opaque when it's not being moved anymore? I have tried the Form_Move event with no luck.
I'm stuck, any help?
The reason the form loads as semi-transparent is because the form has to be moved into the starting position, which triggers the Move event. You can overcome that by basing whether the opacity is set, on whether the form has fully loaded.
The ResizeEnd event fires after a form has finished moving, so something like this should work:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.5;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
To do it properly I expect you'd need to override the message processing to respond to the title bar being held, etc. But you could cheat, and just use a timer so that you make it opaque for a little while when moved, so continuous movement works:
[STAThread]
static void Main()
{
using (Form form = new Form())
using (Timer tmr = new Timer())
{
tmr.Interval = 500;
bool first = true;
tmr.Tick += delegate
{
tmr.Stop();
form.Opacity = 1;
};
form.Move += delegate
{
if (first) { first = false; return; }
tmr.Stop();
tmr.Start();
form.Opacity = 0.3;
};
Application.Run(form);
}
}
Obviously you could tweak this to fade in/out, etc - this is just to show the overall concept.