473,508 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to draw a crosshair in a panel in windows forms

2 New Member
I have a 2 split containers on my C# .NET form dividing the form into 3 panels:

splitContainer1_Panel1
splitContainer2_Panel1
splitContainer1_Panel2

I want to be able to draw 2 lines - 1 vertical and 1 horizontal in this panel - making a crosshair.

I have tried using a new cursor like this:

Expand|Select|Wrap|Line Numbers
  1. private void splitContainer2_Panel1_MouseMove(object sender, MouseEventArgs e)
  2.         {
  3.             textBoxCoordenadas.Text = ("X = " + e.X.ToString() + "m ; Y = " + e.Y.ToString() + "m");
  4.  
  5.             if (CrossHaircheckBox1.Checked == true)
  6.             {
  7.                 splitContainer3.Panel1.Cursor = crossCursor(e);
  8.             }
  9.             else
  10.             {
  11.                 splitContainer3.Panel1.Cursor = Cursors.Arrow;
  12.             }
  13.  
  14. Thw function that defines the cursor is the following:
  15.  
  16.         private Cursor crossCursor(MouseEventArgs e)
  17.         {
  18.             //buscar a posição do cursor
  19.             //Panel panel = new System.Windows.Forms.Panel();
  20.             //Point point = panel.PointToClient(Cursor.Position);
  21.             //vai buscar o tamanho da picturebox
  22.             int[] tamanho =             
  23.             int x = splitContainer1.Panel1.ClientSize.Height;
  24.             int y = splitContainer1.Panel1.ClientSize.Height
  25.             //inicio da caneta tipo com a cor preta
  26.             Pen pen = new Pen(Color.Black);
  27.  
  28.             //Inicia a box de desenho
  29.             var pic = new Bitmap(x, y);
  30.             Graphics gr = Graphics.FromImage(pic);
  31.             //Desenha o crosshair (linhas principais)
  32.             var pathX = new GraphicsPath();
  33.             var pathY = new GraphicsPath();
  34.             pathX.AddLine(0, y / 2, x, y / 2);
  35.             pathY.AddLine(x / 2, 0, x / 2, y);
  36.             gr.DrawPath(pen, pathX);
  37.             gr.DrawPath(pen, pathY);
  38.             //Desenha o crosshair (quadrado central)
  39.             int tc = 5;//tamanho do crosshair
  40.             var pathX2 = new GraphicsPath();
  41.             pathX2.AddLine(x / 2 - tc, y / 2 + tc, x / 2 + tc, y / 2 + tc);
  42.             pathX2.AddLine(x / 2 + tc, y / 2 + tc, x / 2 + tc, y / 2 - tc);
  43.             pathX2.AddLine(x / 2 + tc, y / 2 - tc, x / 2 - tc, y / 2 - tc);
  44.             pathX2.AddLine(x / 2 - tc, y / 2 - tc, x / 2 - tc, y / 2 + tc);
  45.             gr.DrawPath(pen, pathX2);
  46.             //aplica o bitmap e cria a imagem (pointer)
  47.             IntPtr ptr = pic.GetHicon();
  48.             var c = new Cursor(ptr);
  49.             return c;
  50.         }
I have 2 problems in my aproach;

1 - The cursor doesnt update the lines so that when i move my mouse the lines dont end at the panel limits;
2 - The cursos removes performance. The way i mesure this is by printing the mouse coord to a textbox and i can see it's very slow.

Thoughts anyone?
Mar 20 '15 #1
1 3478
Miguel Mariz
2 New Member
Already solved the problema :)
Mar 21 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1280
by: Jan Kopcsek | last post by:
Hello, is there a way to draw Windows.Forms components into a specified surface (a D3DSurface for example)? i want to fill a texture with the graphics and render it to the screen. it looks like...
6
1852
by: Steve McLellan | last post by:
Hi, Does anyone know how Windows allocates memory for controls? For example, given a huge Panel, say, in a relatively much smaller Form with scrollbars, will Windows attempt to allocate a huge...
1
2448
by: Job Lot | last post by:
Is there anyway to draw table in Windows Forms? I want to draw Web Forms style (html) table? thanx
4
4754
by: Joey Powell | last post by:
Hello all. For a long time now I have been struggling with a .net Windows Forms problem. In several of my applications, I use the "Process" class from System.Diagnostics to accomplish various task...
4
1930
by: Joe Kasta | last post by:
Here's a tricky question..:) I have a a function in a webform that will draw a checkbox in a given area (note, this is an override of a PaintForeground function, not a drop / drag of a checkbox...
6
2312
by: | last post by:
Just a general question... When working with a form containing a treeview or similar control... if you need to show different form fields depending on what is selected in the treeview then what...
6
2187
by: Andre Ranieri | last post by:
Hello everyone, I'm wondering if I could get some advice on the best way to build the user interface depicted in this diagram: http://www.senske.com/images/winforms_layout.jpg The gray areas...
21
3287
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
9
1786
by: Scott Stark | last post by:
Hello, I'm *just* delving into Windows forms-based programming without the benefit of any books, etc. I have a background in light ASP.NET work, so forgive me if this is a really basic question...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.