WinForm Update during dragging using ReleaseCapture - c#

I want to update some graphical rendering based on the form's position as I drag the form with the mouse.
Since the form is border-less, I am using ReleaseCapture and SendMessage to move it.
However, I am not able to do my calls during the dragging operation.
I suppose it is possible to spawn a thread that runs until SendMessage returns a value, however I wanted to ask if there is a native method for this particular problem?
public void MoveWindow()
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
....
}
Best regards

You can use events of form which raise at start, during and at the end of movement:
Use Move event of form to do something during form movement.
Use ResizeBegin event of the form to do a job at start of movement.
Use ResizeEnd event of the form to do a job at the end of movement.
Example
private void Form1_Move(object sender, EventArgs e)
{
this.BackColor = this.Left/10 % 2 == 0 ? Color.Red : Color.Blue;
}
private void Form1_ResizeBegin(object sender, EventArgs e)
{
this.Text = this.Location.ToString();
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Text = this.Location.ToString();
}

Related

Getting Scrollbar position from Mouse wheel event

I have a panel in winform. I want to capture both scroll and mouse wheel event for the panel. For both scenario I want to check the scroll bar position.
When scroll bar is at bottom (at the end of scrolling...) the control should fire the event.
I have done this for Panel.Scroll like this:
private void Panel1_Scroll(object sender, ScrollEventArgs e)
{
if (e.NewValue == Panel1.VerticalScroll.Maximum - Panel1.VerticalScroll.LargeChange+1)
{
//do some operation
}
}
But for MouseEventArgs there is no value (e.newvalue) to indicate scrollbar position.
How can I get the Scrollbar position from mouse wheel event ?
Also as per my requirement both event call have same logic implementation, so I want to write the logic once.
How can I achieve this ?
Well upon further analysis I checked out that panel1.VerticalScroll.Value
is equivalent to e.NewValue of ScrollEventArgs.
So for code re-usability below can be used:
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1_scrollcheck(e.NewValue);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
panel1_scrollcheck(panel1.VerticalScroll.Value);
}
private void panel1_scrollcheck(int currPos)
{
if (currPos == panel1.VerticalScroll.Maximum - panel1.VerticalScroll.LargeChange+1)
{
//Put the logic here
}
}
Try to you VerticalScroll property of Panel.
void MouseWheel(object sender, MouseEventArgs e)
{
if (_panel.VerticalScroll.Value > _panel.VerticalScroll.Maximum - _panel.VerticalScroll.LargeChange)
MessageBox.Show("Bottom");
}

C# mousedown event continously press key

okay so what I want to do, it when the mouse is HELD DOWN I want it to continously press a key. it should continously press this key until I let off.
Imagine if you will, a left and right button on a windows form,
then by clicking and holding the right button, the letter "R" displays on a textbox continously until you release the button. What I am doing has very little to do with that scenario, but you get what I'm trying to do here.
What exactly do I put in the mouse down to keep the sendkeys going forever without locking up the application?
I hope my question makes sense. lol.
Thanks
RT
private void pictureBoxKeyboard_MouseDown(object sender, MouseEventArgs e)
{
//something goes here
}
This is worth a read...
http://msdn.microsoft.com/en-us/library/ms171548.aspx
SendKeys.Send("r")
This might just fire once, you may want to attach a timer that starts on the MouseDown event and stops on the MouseUp event. Then you could put the SendKeys in the Timer.Tick event.
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 500;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
this.Text = "moose-Up";
}
private void button1_MouseDown(object sender, EventArgs e)
{
timer1.Start();
this.Text = "moose-Down";
this.richTextBox1.Select();
}
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send("r");
Debug.Print("tickling");
}
Select the control that you wish to receive the SendKeys value...

Mouse events not fired

I'm making a C# WinForms application. The MouseMove and MouseClick events of the form aren't getting fired for some reason. (I'm probably going to feel like an idiot when I find out why.)
It is a transparent form (TransparencyKey is set to the background colour) with a semi-transparent animated gif in a Picture Box. I am making a screensaver.
Any suggestions?
EDIT:
MainScreensaver.cs
Random randGen = new Random();
public MainScreensaver(Rectangle bounds)
{
InitializeComponent();
this.Bounds = Bounds;
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle screen = Screen.PrimaryScreen.Bounds;
Point position = new Point(randGen.Next(0,screen.Width-this.Width)+screen.Left,randGen.Next(0,screen.Height-this.Height)+screen.Top);
this.Location = position;
}
private void MainScreensaver_Load(object sender, EventArgs e)
{
Cursor.Hide();
TopMost = true;
}
private Point mouseLocation;
private void MainScreensaver_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseLocation.IsEmpty)
{
// Terminate if mouse is moved a significant distance
if (Math.Abs(mouseLocation.X - e.X) > 5 ||
Math.Abs(mouseLocation.Y - e.Y) > 5)
Application.Exit();
}
// Update current mouse location
mouseLocation = e.Location;
}
private void MainScreensaver_KeyPress(object sender, KeyPressEventArgs e)
{
Application.Exit();
}
private void MainScreensaver_Deactive(object sender, EventArgs e)
{
Application.Exit();
}
private void MainScreensaver_MouseClick(object sender, MouseEventArgs e)
{
Application.Exit();
}
Excerpt from MainScreensaver.Designer.cs InitialiseComponent()
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseClick);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseMove);
This isn't an answer to you question, but I'm leaving this answer in case anyone else stumbles upon this question while trying to debug this same issue (which is how I got here)
In my case, I had a class that was derived from Form.
I was also using TransparencyKey.
Some things I noticed
The events will not fire on the transparent parts of the form.
The events will not fire if the mouse cursor is over another control on the form.
The events will not fire if you override WndProc and set the result of a WM_NCHITTEST message. Windows doesn't even send out the corresponding mouse messages that would cause the .NET events.
My Solution
In my constructor, I had forgotten to call InitializeComponent().
Which was where the event handlers were being bound to my controls.
Events were not firing because the handlers were not being bound.
Are you sure that your form has focus? If your form does not have focus, the mouse events will not be fired.

C# Fade-in and fade-out transparency when form is being moved

I have the code to make the form partially transparent when is being moved, but I want to know if it's possible to add fade-in and fade-out effects when I start moving and stop moving the form.
EDIT
The code I am using to add transparency to the form is:
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.7;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
You should use a Timer control, set the opacity in timer's tick event. until the form stops moving (define a variable like isMoving and set it to true/false based on form's status).
You can find an example of setting opacity in timer's tick event in my article about a fading label. Use Google translator to read it.
Hope this helps.
You could take a Timer control, you could then start the timer when form starts moving and set the transparency of the form to some value, and on each tick of the Timer, make the transparency to decrease and on some value make it to increase. If you want to have the fadein fadeout effect when form stops moving, you can do the same when the form has moved.

Creating A Continuous Action During A Windows Form Mouse Event

When I put a button on a form in C#, Visual Studio 2005, and have an action triggered by a button event, such as MouseHover or MouseDown, then the event triggers a single call to the function which defines the action despite the fact that I may continue to hover or keep the left button down. In this case I am trying to move a graphical object by rotating or translating it. I don't want to continue to click the mouse in order to get a repeated call to the transforming function, just keep the mouse hovering or hold the button down. What maintains the action until I cease my own action?
Set a flag on MouseEnter and keep doing the action while the flag remains true. Set the flag to false on MouseLeave.
In your case you need to use a combination of the events MouseDown, MouseMove and MouseUp.
Here a small simplified example to start:
private void OnMouseDown(object sender, EventArgs e)
{
//hit test to check if the mouse pointer is on a graphical object
_myHitObject = the_selected_object
}
private void OnMouseMove(object sender, EventArgs e)
{
if(_myHitObject != null)
//do your action relative to the mouse movements.
}
private void OnMouseUp(object sender, EventArgs e)
{
_myHitObject = null;
}
The solution is to use DoEvents() which allows for the MouseLeave event to be noted and the class variable "more" to be changed:
private void MouseEnter_ZoomIn(object sender, EventArgs e)
{
more = true;
while (more == true)
{
c1Chart3D1.ChartArea.View.ViewportScale *= ZoomMultiple;
Application.DoEvents();
}
} // MOUSEENTER_ZOOMIN()
//-------------------------------------
private void MouseLeave_Stop(object sender, EventArgs e)
{
more = false;
}

Categories