Is there a quick way to get the control that's under the mouse?
This code doesn't make a lot of sense, but it does avoid traversing the Controls collections:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pnt);
private void Form1_MouseMove(object sender, MouseEventArgs e) {
IntPtr hWnd = WindowFromPoint(Control.MousePosition);
if (hWnd != IntPtr.Zero) {
Control ctl = Control.FromHandle(hWnd);
if (ctl != null) label1.Text = ctl.Name;
}
}
private void button1_Click(object sender, EventArgs e) {
// Need to capture to see mouse move messages...
this.Capture = true;
}
How to get control under mouse cursor?
Have a look at GetChildAtPoint
. You will have to do some extra work if the controls are contained in a container, see Control.PointToClient
.
How to get the name of a control under the mouse pointer ( without making an event handler for each control )?
What you are looking for is a sender. The sender will tell which image was clicked and will allow you to get its name.
PictureBox picSender = (PictureBox)sender;
label1.Text = picSender.Name;
EDIT : You put that in the pic_Click event
C# Get control at cursor
Check out the GetChildAtPoint method. Combine it with a handler for the MouseMove or MouseHover event and you have your control under the cursor.
How do I get the Control that is under the cursor in Delphi?
I think FindVCLWindow
will meet your needs. Once you have the windowed control under the cursor you can walk the parent chain to find the form on which the window lives.
How to get a control that is underneath another control?
Set the Enabled
property of the PaintBox to False
. That will let the mouse messages go through.
Further:
In the
OnMouseDown
event for the PaintBox I create images at runtime and add them inside the scrollbox
Change that to the OnMouseDown
event on the ScrollBox. Adjust the coordinates by ScrollBox.[Horz/Vert]Scrollbar.Position
.
How to get the control under the mouse cursor in onMouseDown event?
TForm.ObjectAtPoint should do the trick.
How to control the mouse pointer outside our application
There are many ways of doing this, and you didn't mention any details of your application (system, target goal, etc...).
If your goal is menial automation, I'd recommend whipping together a quick AutoIt script on Windows. http://www.autoitscript.com/autoit3/index.shtml
If this isn't what you're looking for, please give more details.
Get the word under the mouse cursor in Windows
You could capture the GDI calls that output text to the display, and then figure out which word's bounding box the cursor falls in.
Related Topics
Combine Two Linq Lambda Expressions
How to Disable Aero Snap in an Application
Dynamically Cross-Join Multiple Different-Size Collections Together in Linq (C#)
How to Convert a Simple .Net Console Project a into Portable Exe with Mono and Mkbundle
Is This a Covariance Bug in C# 4
How to Apply Indenting Serialization Only to Some Properties
Capturing Process Output via Outputdatareceived Event
Equality Comparison Between Multiple Variables
Using Libtiff from C# (To Access Tiled Tiff Images)
Does "Foreach" Cause Repeated Linq Execution
Use "Real" Cultureinfo.Currentculture in Wpf Binding, Not Cultureinfo from Ietflanguagetag
Process Queue with Multithreading or Tasks
How to Configure Many to Many Relationship Using Entity Framework Fluent API
How to Use Xpath Function in a Xpathexpression Instance Programatically
Using Graphics.Drawimage() to Draw Image with Transparency/Alpha Channel