How can i modify the background of the chart - c#

How can I remove the "-----Series1" text (top right, near button1)
How can I make the blue stepline thinker, and remove the background lines?
How can I draw color to the background, such as the area from y=0 to y=1 colored as grey.
How can I add mousewheel event, so that i can use Ctrl+mousewheel to zoom in and out the chart?
Thank you!

Below I have provided some example code to answer your questions. My answers to 3 and 4 are basic examples, and you will have to calculate the correct values for painting and zooming in your application.
private void button1_Click(object sender, EventArgs e)
{
setupChart();
}
private void setupChart()
{
// 1.
foreach (System.Windows.Forms.DataVisualization.Charting.Legend legend in chart1.Legends)
legend.Enabled = false;
// 2.
chart1.Series[0].BorderWidth = 5;
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
// 3.
chart1.Paint += new PaintEventHandler(chart1_Paint);
// 4.
chart1.MouseWheel += new MouseEventHandler(chart1_MouseWheel);
//chart must have focus for MouseWheel event to fire
chart1.Focus();
}
private void chart1_Paint(object sender, PaintEventArgs e)
{
//basic example of painting
//determine size of area to paint
Size areaSize = new Size(50, 50);
//determine location to paint
Point point = new Point(100, 450);
e.Graphics.FillRectangle(new SolidBrush(Color.Gray),
point.X, point.Y, areaSize.Width, areaSize.Height);
}
private void chart1_MouseWheel(object sender, MouseEventArgs e)
{
//basic example of zooming in and out
if (e.Delta > 0)
{
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(
chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum / 2,
chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum / 2);
chart1.ChartAreas[0].AxisY.ScaleView.Zoom(
chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum / 2,
chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum / 2);
}
else
{
if (chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum <
chart1.ChartAreas[0].AxisX.Maximum ||
chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum >
chart1.ChartAreas[0].AxisX.Minimum)
{
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(
chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum * 2,
chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum * 2);
chart1.ChartAreas[0].AxisY.ScaleView.Zoom(
chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum * 2,
chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum * 2);
}
else
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
}
}
}

Related

How can I click on drawn points and make a external window opening?

I want to click on points that I drew.
It would be also cool if a window would popup and I could do something with that. But the general thing i want to do is clicking on a drawn point. I want to make it work, that i can click on the map on points that I drew.
Example image:
public partial class Form1 : Form
{
Graphics g;
Pen p;
Point cursor;
int k = 0;
Point[] points = new Point[50];
public Form1()
{
InitializeComponent();
g = pbxkarte.CreateGraphics();
p = new Pen(Color.DeepSkyBlue, 3);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Pbxkarte_Click(object sender, EventArgs e)
{
if (drawmodecbx.Checked == true)
{
g.DrawEllipse(p, cursor.X - 10, cursor.Y - 10, 20, 20);
points[k++] = new Point(cursor.X, cursor.Y);
lbxDrawnPoints.Items.Add("X:" + cursor.X + "Y:" + cursor.Y);
}
}
private void Pbxkarte_MouseMove(object sender, MouseEventArgs e)
{
cursor = this.PointToClient(Cursor.Position);
xydisplay.Text = "X:" + cursor.X + "Y:" + cursor.Y;
}
}
}
Example code:
Two class level variables and a helper function:
List<Point> dots = new List<Point>();
int dotSize = 12;
Rectangle fromPoint(Point pt, int size)
{
return new Rectangle(pt.X - size/ 2, pt.Y - size / 2, size, size);
}
The mouseclick (as opposed to the click event) contains the location:
private void Pbxkarte_MouseClick(object sender, MouseEventArgs e)
{
if (!dots.Contains(e.Location))
{
dots.Add(e.Location);
Pbxkarte.Invalidate(); // show the dots
}
}
You could add code to remove dots or change the properties, esp. if you create a dot class. - If you want to avoid overlapping dots you can to use code like the one in the mousemove to detect this. But. Don't repeat the code! Instead factor out a boolOrPoint IsDotAt(Point) function you can use both times!!
In the mousemove I only show the hit state. You do your thing..
private void Pbxkarte_MouseMove(object sender, MouseEventArgs e)
{
bool hit = false;
foreach (var dot in dots)
{
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddEllipse(fromPoint(dot, dotSize));
if (gp.IsVisible(e.Location))
{
hit = true; break;
}
}
}
Cursor = hit ? Cursors.Hand : Cursors.Default;
}
All dot in the list must get shown every time anything changes, both in the list or in the system.:
private void Pbxkarte_Paint(object sender, PaintEventArgs e)
{
foreach (var dot in dots)
{
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddEllipse(fromPoint(dot, dotSize));
e.Graphics.FillPath(Brushes.Red, gp);
}
}
}
If you want more properties, like texts or colors do create a class dot and use a List<dot> !

Change ComboBox Border Color - Flash when SelectedIndex changed

I just wanted to know if in windows forms I can create a red line around the border of a combobox when its changed? Like just a flash of red and then gone again just to show that it was changed. Catch the user's eye or something. I will provide screens to represent what i would like.
If it is possible, please tell me where I can look it up to gain some information on it.
No border
Border flash on change
Border gone again after a second or two
Anytime the combobox changes, I want to flash a border to indicate it has changed.
The main idea is using a timer and drawing a border for some times. You can draw the border using different solutions. For example you can (1) draw the border on ComboBox or (2) you can draw border on Parent of ComboBox.
In the answer which I posed, I created a MyComboBox and added a FlashHotBorder method which can be called to flash border. Also I added a HotBorderColor property which can be used to set border color.
Flashing Border of ComboBox
To draw a border for ComboBox you can handle WM_Paint message of ComboBox and draw a border for control. Then to flash the border, you need to use a timer and turn on and turn off border for some times:
MyComboBox Code
I've created a FlashHotBorder method which you can call in SelectedIndexChanged event. Also if always you want to flash border when selected index changes, you can call it in OnSelectedIndexChanged. I prefer to call it in event handler. Here is the implementation:
using System.Drawing;
using System.Windows.Forms;
public class MyComboBox : ComboBox
{
int flash = 0;
private const int WM_PAINT = 0xF;
private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
public Color HotBorderColor { get; set; }
private bool DrawBorder { get; set; }
Timer timer;
public MyComboBox()
{
this.HotBorderColor = Color.Red;
timer = new Timer() { Interval = 100 };
timer.Tick += new System.EventHandler(timer_Tick);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT && this.DrawBorder)
using (var g = Graphics.FromHwnd(this.Handle))
using (var p = new Pen(this.HotBorderColor))
g.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
}
public void FlashHotBorder()
{
flash = 0;
timer.Start();
}
void timer_Tick(object sender, System.EventArgs e)
{
if (flash < 10)
{
flash++;
this.DrawBorder = !this.DrawBorder;
this.Invalidate();
}
else
{
timer.Stop();
flash = 0;
DrawBorder = false;
}
}
protected override void Dispose(bool disposing)
{
if (disposing) { timer.Dispose(); }
base.Dispose(disposing);
}
}
Then it's enough to use this event handler for SelectedIndexChanged event of eeach combo which you want to flash:
private void myComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var combo = sender as FlatCombo;
if (combo != null)
combo.FlashHotBorder();
}
You can create an outline/draw a border outside a comboBox or any other control using the DrawRectangle method.
The border will be drawn outside the comboBox if the SelectedIndex range condition satisfies else it'll revert to it's original state with no outline.
bool changed = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (changed)
{
Pen p = new Pen(Color.Red);
Graphics g = e.Graphics;
int diff = 1;
g.DrawRectangle(p, new Rectangle(comboBox1.Location.X - diff, comboBox1.Location.Y - diff, comboBox1.Width + diff, comboBox1.Height + diff));
}
}
And, I am calling the Form1_Paint event on SelectedIndexChanged event of the comboBox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 1 && comboBox1.SelectedIndex <= 9)
{
changed = true;
this.Refresh();
}
else
{
changed = false;
this.Refresh();
}
}
                    Outline                                         Without Outline
So I came up with this. It's the shortest and easiest way to do it I think. If you have any recommendation, feel free to post them or comment it. thanx for all the help :).
public partial class Form1 : Form
{
private int tick = 0;
public Form1()
{
InitializeComponent();
}
bool changed = false;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (changed == true)
{
changed = false;
this.Refresh();
}
else
{
if(tick<3)
{
timer1.Enabled = true;
timer1.Start();
}
changed = true;
this.Refresh();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (changed)
{
Graphics g1 = e.Graphics;
int diff = 1;
Rectangle rect2 = new Rectangle(comboBox1.Location.X - diff, comboBox1.Location.Y - diff, comboBox1.Width + diff, comboBox1.Height + diff);
using (LinearGradientBrush br = new LinearGradientBrush(rect2,Color.Red,Color.Blue,LinearGradientMode.Horizontal))
{
ColorBlend color_blend = new ColorBlend();
color_blend.Colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Lime, Color.Blue, Color.Indigo, Color.DarkViolet};
color_blend.Positions = new float[] { 0 / 6f, 1 / 6f, 2 / 6f, 3 / 6f, 4 / 6f, 5 / 6f, 6 / 6f };
br.InterpolationColors = color_blend;
Pen p = new Pen(br, 10);
e.Graphics.DrawRectangle(p, rect2);
}
}
else
{
Pen p = new Pen(Color.Transparent);
Graphics g = e.Graphics;
int diff = 1;
g.DrawRectangle(p, new Rectangle(comboBox1.Location.X - diff, comboBox1.Location.Y - diff, comboBox1.Width + diff, comboBox1.Height + diff));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if(tick<3)
{
comboBox1_SelectedIndexChanged(null, null);
tick++;
}
else
{
timer1.Stop();
tick = 0;
}
}
}

C# draws my image too large and regardless of mouse position

I'm currently working on a battleships game,but i've ran into an issue.So far i've managed to draw the grid.the purpose of the draw method is to draw an image(don't know how to/if i can color a certain surface) inside a square in the grid,when i left-click.
the problem here is that,even if the image's size is 25x25(the size of a square) it occupies like half the screen,and that's when it works.50% of the times when i run nothing happens,and the other 50% it draws a huge image in the middle of the screen,regardless of where the cursor is located or if i left-click.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(form1_paint);
}
private void form1_paint(object sender, PaintEventArgs e)
{
draw(e);
}
int x,y;
private void draw(PaintEventArgs e)
{
if (MouseButtons.Left != 0)
{
x = Cursor.Position.X;
y = Cursor.Position.Y;
Image poza = Image.FromFile("D://C//12E//c#//yellow4.jpg");
if (x < 301 && x > 24 && y < 301 && y > 24)
{
PointF coltz = new PointF(x / 25 * 25, y / 25 * 25);
e.Graphics.DrawImage(poza, coltz);
}
}
}
Does anyone know how i can solve this?or if someone has a better idea for a battleships grid, I am open to suggestions.Thanks!
Fisrt of all, this line of code: Cursor.Position.X gives you the global position of the cursor on the screen, not in the game window. I suggest you to handle MouseMove event to get the position relative to the content of your app.
The second thing is that you are loading the image from file on your computer. I think it's better to add the image to your app's resources, so you can load it easier just calling it by name, e.g.: AppName.Properties.Resources.ImageName - it returns Image object you can immediately use.
One more thing. This if (MouseButtons.Left != 0) won't check whether left mouse button is pressed or not. You have to check if MouseButtons property equals System.Windows.Forms.MouseButtons.Left.
Here's the full code that works for me:
public partial class Form1 : Form
{
private int x, y;
public Form1()
{
InitializeComponent();
Paint += Form1_Paint;
MouseMove += Form1_MouseMove;
MouseDown += Form1_MouseMove;
}
void Form1_Paint(object sender, PaintEventArgs e)
{
Draw(e);
}
private void Draw(PaintEventArgs e)
{
if (MouseButtons == System.Windows.Forms.MouseButtons.Left)
{
if (x < 301 && x > 24 && y < 301 && y > 24)
{
PointF coltz = new PointF(x / 25 * 25, y / 25 * 25);
e.Graphics.DrawImage(AppName.Properties.Resources.ImageName, coltz);
}
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
Invalidate();
}
}
And here's the result:
I also subscribed MouseDown event to show yellow rectangle when user clicks the button without moving the cursor.
the thing is,before adding your code,the draw worked fine.now,every time i move the mouse,the grid gets redrawn,and it looks like it's constantly refreshing,bleeping somehow.not sure how to phrase this
public Form1()
{
InitializeComponent();
Paint += Form1_Paint;
MouseMove += Form1_MouseMove;
MouseDown += Form1_MouseMove;
}
private int x,y;
void Form1_Paint(object sender, PaintEventArgs e)
{
Draw(e);
}
private void Draw(PaintEventArgs e)
{
if (MouseButtons == System.Windows.Forms.MouseButtons.Left)
{
if (x < 301 && x > 24 && y < 301 && y > 24)
{
PointF coltz = new PointF(x / 25 * 25, y / 25 * 25);
e.Graphics.DrawImage(battleships.Properties.Resources.yellow4, coltz);
}
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g;
g = e.Graphics;
Pen pen = new Pen(Color.Black);
pen.Width = 1;
for (int i = 25; i <= 300; i = i + 25)
{
g.DrawLine(pen, i, 25, i, 300);
g.DrawLine(pen, 25, i, 300, i);
}
}
}

Scrolling Label in C#

I am looking for an efficient way of scrolling text like a marquee in web terminology.
I managed to achieve this using a piece of code I found online:
private int xPos = 0, YPos = 0;
private void Form1_Load(object sender, EventArgs e)
{
//link label
lblText.Text = "Hello this is marquee text";
xPos = lblText.Location.X;
YPos = lblText.Location.Y;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (xPos == 0)
{
this.lblText.Location = new System.Drawing.Point(this.Width, YPos);
xPos = this.Width;
}
else
{
this.lblText.Location = new System.Drawing.Point(xPos, YPos);
xPos -= 2;
}
}
The code is very simple and it uses, a timer tick event.
It works great initially, but after scrolling 3 or 4 times, it does not reappear.
Is there anything I can adjust to make the scrolling infinite?
try:
private void timer1_Tick(object sender, EventArgs e)
{
if (xPos <= 0) xPos = this.Width;
this.lblText.Location = new System.Drawing.Point(xPos, YPos);
xPos -= 2;
}
You mentioned that it 'cuts off' if the string is longer than the form width. I assume you mean that the label jumps back to the right side of the form as soon as it hits the left side, which means you can't read the full text?
If so, you could set the 'minimum Left' of the Label to be the negative of it's width. This would allow the label to scroll fully off the form before resetting it:
private void timer1_Tick(object sender, EventArgs e)
{
// Let the label scroll all the way off the form
int minLeft = this.lblText.Width * -1;
if (xPos <= minLeft) xPos = this.Width;
this.lblText.Location = new Point(xPos, yPos);
xPos -= 2;
}
Or, you can set the 'minimum Left' to be the negative of the difference between the label width and the form width, so that it would not reset until the rightmost characters have been shown:
private void timer1_Tick(object sender, EventArgs e)
{
// Ensure that the label doesn't reset until you can read the whole thing:
int minLeft = (lblText.Width > this.Width) ? this.Width - lblText.Width : 0;
if (xPos <= minLeft) xPos = this.Width;
this.lblText.Location = new Point(xPos, yPos);
xPos -= 2;
}
Many other options, too. Like having multiple labels running back to back in rotation, so there is never any blank text!! You can figure out how many labels to generate dynamically (based on the difference between their width and the form's width) and handle their positions in the Timer event.
If you fill the Label by adding blanks til it has the length you want, this may look nicer:
private void timer1_Tick(object sender, EventArgs e)
{
lblText.Text=
lblText.Text.Substring(1) + lblText.Text.Substring(0,1);
}
Adding the right amount of blanks may be a bit of a challenge, though. You will need to do that on Form.Resize! Doing this right is a bit trickier, than one might think.
The perfect marquee would combine the pixelwise movement and the rollover effect, maybe by owner-drawing the label, maybe like this 'Real Marquee', so 80s ;-)
class Marquee : Label
{
public Timer MarqueeTimer { get; set; }
public int Speed { get; set; }
public int yOffset { get; set; }
public void Start() { MarqueeTimer.Start(); }
public void Stop() { MarqueeTimer.Stop(); }
private int offset;
SolidBrush backBrush ;
SolidBrush textBrush ;
public Marquee()
{
textBrush = new SolidBrush(this.ForeColor);
backBrush = new SolidBrush(this.BackColor);
yOffset = 0;
Speed = 1;
MarqueeTimer = new Timer();
MarqueeTimer.Interval = 25;
MarqueeTimer.Enabled = true;
MarqueeTimer.Tick += (aSender, eArgs) =>
{
offset = (offset - Speed);
if (offset < -this.ClientSize.Width) offset = 0;
this.Invalidate();
};
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(backBrush, e.ClipRectangle);
e.Graphics.DrawString(this.Text, this.Font, textBrush, offset, yOffset);
e.Graphics.DrawString(this.Text, this.Font, textBrush,
this.ClientSize.Width + offset, yOffset);
}
}
Looks really smooth and needs no outside code but Start, Stop and setting Speed and perhaps the MarqueeTimer Intervall. All regular Properties will work from the Designer, just set AutoSize=false and make it large enough to fill the area!

Click two new points and draw a line between those two points using mouse event

Any suggestions how to create a line by clicking two new points then draw a line between them?
I am trying to create a distance tool like the one in adobe acrobat.
Image Example
Problem Solved!
EDIT:
Here's the code:
private Point p1, p2;
List<Point> p1List = new List<Point>();
List<Point> p2List = new List<Point>();
private void Panel1_MouseDown(object sender, MouseEventArgs e)
{
if (p1.X == 0)
{
p1.X = e.X;
p1.Y = e.Y;
}
else
{
p2.X = e.X;
p2.Y = e.Y;
p1List.Add(p1);
p2List.Add(p2);
Invalidate();
p1.X = 0;
}
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
using(var p = new Pen(Color.Blue, 4))
{
for(int x = 0; x<p1List.Count; x++){
e.Graphics.DrawLine(p, p1List[x], p2List[x]);
}
}
}
You can handle the mouse click event on the panel (for example) and retrieve the location of the click (using the event args). Store this location in an attribute. Do that for as many points as you need.
In the panel paint event, call the parent paint, then draw the lines between your points.
Something like this should do it:
Point firstPoint;
Point seondPoint;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (this.firstPoint == null) {
this.firstPoint = e.Location;
}
if (this.secondPoint == null) {
this.secondPoint = e.Location;
}
panel1.Invalidate();
}
private void panel1_Paint_1(object sender, PaintEventArgs e)
{
Using (pn as new Pen(Color.Blue, 5))
{
e.Graphics.DrawLine(pn, firstPoint, secondPoint);
}
}
EDIT: You also dont need to do CreateGraphics to draw the line - in the Paint event you have a graphics object already.

Categories

Resources