I'm trying to access the variable "g" from a different method, it does not throw an error but doesn't write to the screen. Any way to use the graphics class from different methods? Paint Method:
public static Graphics g;
private void paintClass(object sender, PaintEventArgs e)
{
e.Graphics = g;
}
Method Im Trying To Access "g" From:
public void drawline(){
g.DrawLine(new Pen(Color.AliceBlue), 10, 10, 20, 20);
}
Note These Code Snippets Are from The Same Class
If this isn't possible, I am just trying to figure out a way to paint to the screen from a method that isn't the paint method.
CodePudding user response:
Instead of storing it, pass it directly from your event:
private void paintClass(object sender, PaintEventArgs e)
{
drawline(e.Graphics);
}
public void drawline(Graphics g){
g.DrawLine(new Pen(Color.AliceBlue), 10, 10, 20, 20);
}
This approach would make more sense if the "draw" method was part of a different class. For example, if you had a collection of custom class instances and you pass the graphics to each one so they can each draw themselves onto a surface (with the graphics provided from the paint event of that surface).
CodePudding user response:
I am guessing you could obtain what you want by using CreateGraphics()
.
var g = myButton.CreateGraphics();
g.DrawLine(Pens.Red, 0, 0, 20, 20);
Replace myButton with the Control
or Form
you want to draw on.
This may turn out to not be the right approach though. The drawing will disappear e.g. when the form is minimized, maximized, gets covered by another form etc.
The recommended pattern is to use the Paint
event - see
Line Button - Draw a line between two points.
private void buttonLine_Click(object sender, EventArgs e)
{
var offsetY = 15 * _testCountLine ;
_paintClass.Drawline(
GetNextTestColor(),
new PointF(0, 100 offsetY),
new PointF(ClientRectangle.Width, 100 offsetY));
}
Under the hood
The PaintClass
inherits List<PaintClassContext>
where:
class PaintClassContext
{
public PaintOp PaintOp { get; set; }
public Color Color { get; set; }
public PointF Start { get; set; }
public PointF End { get; set; }
public float OffsetX { get; set; }
public float OffsetY { get; set; }
}
enum PaintOp{ DrawLine, Clear, DrawDiagonal}
Example: Implementation of DrawDiagonal
Add a new context to the current list.
public void DrawDiagonal(Color color, float offsetX = 0, float offsetY = 0) =>
Add(new PaintClassContext
{
PaintOp = PaintOp.DrawDiagonal,
Color = color,
OffsetX = offsetX,
OffsetY = offsetY,
});
Every time a new action is added, the Refresh
event is fired and this repaints the canvas.
public new void Add(PaintClassContext context)
{
base.Add(context);
_modified = true;
Refresh?.Invoke(this, EventArgs.Empty);
}
The MainForm
will be subscribed to the PaintClass.Refresh
event and calls Refresh().
_paintClass.Refresh = (sender, e) =>
{
// Causes the control to repaint.
Refresh();
Text = CurrentTestColor.ToString();
};
This causes the control to redraw, where the PaintAll
will iterate its current items and paint them in a legitimate graphics context rather than trying to "finesse" one.
PaintClass _paintClass = new PaintClass();
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
_paintClass.PaintAll(e.Graphics, always: false);
}