T
Tony Johansson
Hello!
This program draw an x-axis and a y-axes and also add values to the interval
on the x and y axes.
The program also move the Page coordinates to the bottom left corner which
will be the origo.
All this works good.
I also want a cross to be drawn in the following way one horizontal dashed
line all over the panel called pnlDiagram and one vertical dashed line from
bottom to top of the panel control. So as a summary it should be a cross
from left to right and from top to bottom of the Panel control and the cross
should be in sync with the mouse pointer. If I move the mouse pointer the
cross should also be moved. For example if a position the mouse pointer at
the page origo(0,0) the cross should also be there.
The vertical dashed line works perfect and is in sync with the mouse
pointer.
The horizontal dashed line is not in sync with the mouse pointer and I can't
figure out where I should change to make it work. ?
When I position the mouse pointer at the upper horizontal line of the Panel
my dashed horizontal line is located at the x-axis and when I move the mouse
poiner down the dashed horizontal line is going up.
It's this drawing of the horizontal dashed line that is wrong because it's
not in sync with the mouse pointer
e.Graphics.DrawLine(pen,
new PointF(pnlDiagram.Width, -originalMousePosition.Y),
new PointF(0, -originalMousePosition.Y));
Below is all the code that has to do with my problem.
public partial class MainForm : Form
{
private PointF currPos;
private PointF newOriginOffset;
private PointF originalMousePosition;
private DiagramData data = new DiagramData();
private DrawDiagram drawDiagram = new DrawDiagram();
public MainForm() //C-tor
{
InitializeComponent();
newOriginOffset = new Point(pnlDiagram.Width / 10,
pnlDiagram.Height / 10);
InitializeNewRun();
}
private void InitializeNewRun()
{
grpStart.Enabled = false;
groupSettings.Enabled = true;
pnlDiagram.Invalidate();
}
private void pnlDiagram_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TranslateTransform(pnlDiagram.Width * 1 / 10,
pnlDiagram.Height * 9 / 10);
drawDiagram.Draw(g, data.Points, pnlDiagram.Height,
pnlDiagram.Width,
int.Parse(txtXIntervals.Text),
int.Parse(txtXIntervalValue.Text),
int.Parse(txtYIntervals.Text),
int.Parse(txtYIntervalValue.Text));
Pen pen = new Pen(Brushes.Black);
pen.DashStyle = DashStyle.Dash;
e.Graphics.DrawLine(pen,
new PointF(currPos.X, -pnlDiagram.Height),
new PointF(currPos.X, 0));
e.Graphics.DrawLine(pen, new
PointF(pnlDiagram.Width, -originalMousePosition.Y),
new
PointF(0, -originalMousePosition.Y));
}
private void pnlDiagram_MouseMove(object sender, MouseEventArgs e)
{
originalMousePosition = new Point(e.X, e.Y);
currPos.X = e.X - newOriginOffset.X;
currPos.Y = e.Y - newOriginOffset.Y - e.Y;
pnlDiagram.Invalidate();
}
}
//Tony
This program draw an x-axis and a y-axes and also add values to the interval
on the x and y axes.
The program also move the Page coordinates to the bottom left corner which
will be the origo.
All this works good.
I also want a cross to be drawn in the following way one horizontal dashed
line all over the panel called pnlDiagram and one vertical dashed line from
bottom to top of the panel control. So as a summary it should be a cross
from left to right and from top to bottom of the Panel control and the cross
should be in sync with the mouse pointer. If I move the mouse pointer the
cross should also be moved. For example if a position the mouse pointer at
the page origo(0,0) the cross should also be there.
The vertical dashed line works perfect and is in sync with the mouse
pointer.
The horizontal dashed line is not in sync with the mouse pointer and I can't
figure out where I should change to make it work. ?
When I position the mouse pointer at the upper horizontal line of the Panel
my dashed horizontal line is located at the x-axis and when I move the mouse
poiner down the dashed horizontal line is going up.
It's this drawing of the horizontal dashed line that is wrong because it's
not in sync with the mouse pointer
e.Graphics.DrawLine(pen,
new PointF(pnlDiagram.Width, -originalMousePosition.Y),
new PointF(0, -originalMousePosition.Y));
Below is all the code that has to do with my problem.
public partial class MainForm : Form
{
private PointF currPos;
private PointF newOriginOffset;
private PointF originalMousePosition;
private DiagramData data = new DiagramData();
private DrawDiagram drawDiagram = new DrawDiagram();
public MainForm() //C-tor
{
InitializeComponent();
newOriginOffset = new Point(pnlDiagram.Width / 10,
pnlDiagram.Height / 10);
InitializeNewRun();
}
private void InitializeNewRun()
{
grpStart.Enabled = false;
groupSettings.Enabled = true;
pnlDiagram.Invalidate();
}
private void pnlDiagram_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TranslateTransform(pnlDiagram.Width * 1 / 10,
pnlDiagram.Height * 9 / 10);
drawDiagram.Draw(g, data.Points, pnlDiagram.Height,
pnlDiagram.Width,
int.Parse(txtXIntervals.Text),
int.Parse(txtXIntervalValue.Text),
int.Parse(txtYIntervals.Text),
int.Parse(txtYIntervalValue.Text));
Pen pen = new Pen(Brushes.Black);
pen.DashStyle = DashStyle.Dash;
e.Graphics.DrawLine(pen,
new PointF(currPos.X, -pnlDiagram.Height),
new PointF(currPos.X, 0));
e.Graphics.DrawLine(pen, new
PointF(pnlDiagram.Width, -originalMousePosition.Y),
new
PointF(0, -originalMousePosition.Y));
}
private void pnlDiagram_MouseMove(object sender, MouseEventArgs e)
{
originalMousePosition = new Point(e.X, e.Y);
currPos.X = e.X - newOriginOffset.X;
currPos.Y = e.Y - newOriginOffset.Y - e.Y;
pnlDiagram.Invalidate();
}
}
//Tony