Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want a way to draw on the WinForm with the same graphics object and send to it orders of drawing freely, I made this way below but it's hard to use.

What I have tried:

public FormTimerForDrawing()
        {
            InitializeComponent();

            Timer t = new Timer();
            t.Interval = 10;
            t.Tick += T_Tick; t.Start();

            lstDrawingOrders.Add(new drawingOrder
            {
                drawingOrderType = "RedLine",
                points = new Point[] 
                { new Point(20, 10), new Point(30, 50) }
            });
        }

        Graphics g;


        List<drawingOrder> lstDrawingOrders = new List<drawingOrder>();

        private void T_Tick(object sender, EventArgs e)
        {
            while (lstDrawingOrders.Count > 0)
            {
                switch (lstDrawingOrders[0].drawingOrderType)
                {
                    case "RedLine":
                        g.DrawLine(new Pen(Color.Red), lstDrawingOrders[0].points[0], lstDrawingOrders[0].points[1]);
                        break;
                }
                lstDrawingOrders.RemoveAt(0);
            }
        }

        class drawingOrder
        {
            public string drawingOrderType = "";
            public Point[] points;
        }

        private void FormTimerForDrwaing_Shown(object sender, EventArgs e)
        {
            g = CreateGraphics();
        }
Posted
Updated 26-May-21 21:42pm
v2

1 solution

"I made this way below but it's hard to use."
It is not clear what that means. However, drawing on Windows forms needs to be performed in the Form.OnPaint(PaintEventArgs) Method (System.Windows.Forms) | Microsoft Docs[^]. So while you can collect the parameters that need to be drawn in your timer, you should do the actual drawing in the correct place.
 
Share this answer
 
Comments
john1990_1 27-May-21 3:47am    
I want different methods to order maybe at the same time drawing of stuff on the form to add them, how?

I would work on them being drawn on OnPaint too but OnPaint doesn't happen every millisecond, should I use something other than WinForms? how to have many timers and backgroundworkers order drawing on the form with Graphics g object maybe at the same time?
Richard MacCutchan 27-May-21 4:09am    
You are likely to get things very confused like that. The design of the Windows system is such that any drawing done outside of the OnPaint event, can get erased, so if you do it the way you suggest you may end up with nothing. You should collect all your data in the background workers, but do all the paining in the one Form event handler. And remember if you do too much painting too fast you will end up with a flickering form that drives your users mad.
john1990_1 27-May-21 12:50pm    
Sometimes I need to draw on the form fire where the mouse is when the mouse is moving and remove the random points (pixels) near the mouse after 0.1 second (to remove the random points I would draw only the table squares affected by the fire pixels), would someone please help me in how to draw on a form simultaneously yet? OnPaint gets fired rarely and doesn't animate the form well, I would save the location of the fire pixels and draw them on OnPaint but I need a way to draw at the same time on the form please!
Richard MacCutchan 27-May-21 12:53pm    
Well you are still going to have problems. You can follow the rules and get it to work, or you can ignore the rules and continue to have problems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900