To draw both a horizontal line and a vertical line (a "+" shape at the center of the screen) alongside the circle, I will make minimal changes while keeping the structure exactly the same. Here's the updated OnPaint method with the added functionality to draw the lines in addition to the circle.
Updated Code:
namespace YourNamespace // Replace with your actual project namespace
{
public class CrosshairForm : Form
{
private const int WS_EX_TRANSPARENT = 0x20;
private const int WS_EX_LAYERED = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public CrosshairForm()
{
// Configure the form properties for the overlay.
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.LimeGreen; // Use LimeGreen for transparency.
this.TransparencyKey = this.BackColor;
this.TopMost = true; // Always keep this overlay on top.
this.WindowState = FormWindowState.Maximized;
this.Bounds = Screen.PrimaryScreen.Bounds;
this.Opacity = 0.90; // Slight transparency makes the overlay smoother.
// Attach the load event.
this.Load += CrosshairForm_Load;
}
private void CrosshairForm_Load(object sender, EventArgs e)
{
// Make the form transparent and non-obstructive to mouse clicks.
int currentStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, currentStyle | WS_EX_TRANSPARENT | WS_EX_LAYERED);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Center of the screen.
int centerX = Screen.PrimaryScreen.Bounds.Width / 2;
int centerY = Screen.PrimaryScreen.Bounds.Height / 2;
// Circle properties.
int circleDiameter = 5; // Diameter of the circle.
int circleThickness = 1; // Thickness of the circle outline.
Color circleColor = Color.Purple; // Color of the circle.
// Dot properties (in case specifics are required).
int dotDiameter = circleDiameter;
// Line properties.
int lineThickness = 1; // Thickness of the lines.
Color lineColor = Color.White; // Color of the lines.
// Draw the circle crosshair.
using (Pen pen = new Pen(circleColor, circleThickness))
{
e.Graphics.DrawEllipse(
pen,
centerX - circleDiameter / 2,
centerY - circleDiameter / 2,
circleDiameter,
circleDiameter
);
}
// Draw lines (horizontal and vertical).
using (Pen pen = new Pen(lineColor, lineThickness))
{
// Horizontal line.
e.Graphics.DrawLine(
pen,
centerX - 20, // Start point X (20 pixels to the left of center).
centerY, // Start point Y (at center).
centerX + 20, // End point X (20 pixels to the right of center).
centerY // End point Y (at center, horizontal line).
);
// Vertical line.
e.Graphics.DrawLine(
pen,
centerX, // Start point X (at center).
centerY - 20, // Start point Y (20 pixels above center).
centerX, // End point X (at center).
centerY + 20 // End point Y (20 pixels below center).
);
}
// Draw the filled dot in the center.
using (SolidBrush brush = new SolidBrush(circleColor))
{
e.Graphics.FillEllipse(
brush,
centerX - dotDiameter / 2,
centerY - dotDiameter / 2,
dotDiameter,
dotDiameter
);
}
}
}
}
Explanation of Changes:
-
Added Horizontal and Vertical Lines:
- I added logic to draw a horizontal line and a vertical line using
e.Graphics.DrawLine(...).
- The lines are centered on the screen (
centerX, centerY) and span 40 pixels in total (20 pixels in each direction from the center).
-
Circle Logic is Still Present:
- The
DrawEllipse and FillEllipse remain intact to preserve your circle crosshair functionality.
-
No New Structure:
- I adhered strictly to your original code structure. Only relevant logic for drawing the lines was added.
-
Minimal Adjustments:
- Variables like
lineThickness and lineColor were defined to match your preferences.
How It Will Look:
The result will display:
- A centered circle crosshair filled with purple at the center of the screen,
- A horizontal line going 20 pixels left and right of the center,
- A vertical line going 20 pixels above and below the center,
creating a "+" shape with the circle exactly at the intersection.
Let me know if you need additional tweaks!