I'm making a custom control slider so I can adjust the appearance myself.
However, I can't find a way to get the cursor position relative to the control.
It would propably be easy to code for each control after plopping the controls into my program. But I'd like to have the full functionality inside the custom control project and only having to worry about getting a value from the slider once it's inside my program.
So I need to get the cursor tracking done inside the custom control project.
I've tried using the event here:
private void CustomSlider_MouseDown(object sender, MouseEventArgs e)
{
}
But the only position I'm able to get is the 'global' screen location of the cursor, which will not help me unless I know the position of the control.
I hope my question is clear, thanks.
To obtain the cursor position within the control's event-handler or corresponding virtual method, use the MouseEventArgs.Location(MSDN) property:
class CustomControl : Control {
protected override void OnMouseDown(MouseEventArgs e) {
Point cursorPos = e.Location;
}
}
To obtain the cursor position outside the control, use the Control.MousePosition(MSDN) static property and the PointToClient()(MSDN) method:
CustomControl ctrl = ...
Point cursorPos = ctrl.PointToClient(Control.MousePosition);
Related
Like the RichTextBox, I want to be able to handle events for when the vertical scrollbar has been adjusted (through slider dragging, the mouse wheel or otherwise), and for when the caret / text cursor has been moved. However, these events appear to be missing from Scintilla. How can I achieve the same result?
Both of those are available under the UpdateUI event via the UpdateChange structure. Example:
private void scintilla1_UpdateUI(object sender, ScintillaNET.UpdateUIEventArgs e)
{
if (e.Change == ScintillaNET.UpdateChange.VScroll)
{
...
}
}
I have two event handlers in an application that provide a 'Drag' type operation (not an actual Drag Event). For this, I have event handlers to handle the MouseDown and MouseMove events.
private void OnMouseDown(object sender,MouseButtonEventArgs e)
{
//The following CORRECTLY gives the relative position
//Note the MouseButtonEventArgs type
Point position = e.GetPosition(this.myCanvas);
Point relativePosition = e.GetPosition(this.myUsercontrol);
//Do something with position and relative position
}
private void OnMouseMove(object sender,MouseEventArgs e)
{
//The following INCORRECTLY gives the relative position.
//Note the MouseEventArgs type
Point position = e.GetPosition(this.myCanvas);
Point relativePosition = e.GetPosition(this.myUsercontrol);
//Do something with position and relative position
}
The problem I am having is I need to utilize the relative position of the mouse during the MouseMove event. However, it is returning (0,0) when I call GetPosition(...) on MouseEventArgs, but is working perfectly fine when invoked on MouseButtonEventArgs. Why wou
The problem with Mouse events is that they don't exactly only happen in the context you are looking at, the same event happens everywhere. And if some other element sets MouseEventArgs.Handled = true... well weird things start happening. I was getting the same problem, e.GetPosition() was returning a constant {-25;-170}, but only when the window was in normal mode, when maximized everything worked just fine. The culprit was window level event handler setting e.Handled = true;
So check where else your application is using mouse events
I am new to c# Windows Form Application Development.
I created a form with a panel in which the user can draw image on it. How do I check if the image is clicked?
In designer mode, right-click on the panel, go to Properties.
In the Properties window, choose EVENTS (Lightning icon).
Double click on Click, then this code will be generated:
private void panel1_Click(object sender, EventArgs e)
{
//--what to do when user clicks on panel--
MessageBox.Show("Clicked");
}
Just double-click on the form's image-panel (or whatever object you wish to detect click events on) and Visual Studio will automatically generate a OnClick() event. Needless to say, I'm talking about the forms designer, not the actual form you will see when testing your code.
Alternatively, you can set which events you want to implement through the object's properties. That way you can also implement OnKeyDown() or OnFocus() or any other kind of events.
Edit: If the image doesn't cover the entire panel, you'll have to check if the mouse position is within the image's dimension. Assuming the image is drawn at position (imgOriginX, imgOriginY) and has the size (imgWidth, imgHeight):
// Fires, when user clicks on panel
private void panel_Click(object sender, EventArgs e)
{
// Cast to MouseEventArgs
MouseEventArgs mouse = (MouseEventArgs)e;
// If mouse is within image
if (mouse.X >= imgOriginX && mouse.Y >= imgOriginY && mouse.X < imgOriginX + imgWidth && mouse.Y < imgOriginY + imgHeight)
{
// do something here
}
}
I have WebBrowser class and image loaded in it. After mouse click on the browser, I need to get mouse position - what is the best way to do it?
this is actually pretty simple if your just looking for the screen coordinates.
// this probably should be in your form initialization
this.MouseClick += new MouseEventHandler(MouseClickEvent);
void MouseClickEvent(object sender, MouseEventArgs e)
{
// do whatever you need with e.Location
}
if your strictly looking for the point in the browser, you need to consider the functions
browser.PointToClient();
browser.PointToScreen();
I am trying to create this simple application in c#: when the user double clicks on specific location in the form, a little circle will be drawn. By one click, if the current location is marked by a circle - the circle will be removed.
I am trying to do this by simply register the MouseDoubleClick and MouseClick events, and to draw the circle from a .bmp file the following way:
private void MouseDoubleClick (object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
Bitmap myImage = (Bitmap)Bitmap.FromFile("Circle.bmp");
g.DrawImage(myImage, e.X, e.Y);
}
My problem is that I dont know how to make the circle unvisible when the user clicks its location: I know how to check if the selected location contains a circle (by managing a list of all the locations containig circles...), but I dont know how exactly to delete it.
Another question: should I call the method this.CreateGraphics() everytime the user double-clicks a location, as I wrote in my code snippet, or should I call it once on initialization?
My personal preference is to put my images in instances of the Picturebox class. Reason being, I can simply call each Picturebox's Hide() function (or set 'Visible` to false).
What you're doing is drawing directly onto the window's client area, which technically isn't wrong but normally should be done in the form's Paint handler. If at some point you decide you don't want your circle to be visible anymore, you can call the form's Invalidate() method which triggers the Paint event. There, you explicitly do not draw your circle, and so to the user, the circle disappears.
The nice thing about a Picturebox is that it's persistent - you put your image into it and optionally draw on that image, but you only need to draw once. If you use the Paint handler technique, your drawing code gets called each time the form needs to redraw itself.
Edit:
Here's some code that illustrates my Paint handler information:
private void Form_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(); // clear any and all circles being drawn
if (CircleIsVisible)
{
e.Graphics.DrawEllipse( ... ); // OR, DrawImage( ) as in your example
}
}
private void MouseDoubleClick (object sender, MouseEventArgs e)
{
CircleIsVisible = true;
Invalidate(); // triggers Paint event
}
If you're drawing bitmaps, I would load the bitmap once and store it as a class variable. This way you don't need to hit the hard drive each time you want to draw. Dispose of the bitmap when you dispose of your class (in this case, your window).
I thinks you should clear all of the image you draw before your next double click.
Such as Graphics.Clear().
On the other hand, you should not to create Graphics object or dispose it every time.
If you have simple background color you could use Graphics.DrawEllipse to draw Circles and then just change circle color to the background color. Also you need to have a Collection of all circles you draw so you can access any circle that you've drawn.