• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

How do I put my image to the back so it wont keep on refreshing?

this is my code:
http://athena.csie.fju.edu.tw/~ie905162/test.cs

this is the compiled exe:
http://athena.csie.fju.edu.tw/~ie905162/test.rar

(Note: Left-click and click "Start" will start the program)


See how the screen refreshes all the time and how there is a brown line that runs up and down the screen?
My question is, how do I put my image (the pool table) to the back of the screen so that whenever my screen refreshes, it doesnt refresh with it and I dont get that annoying brown line everytime?

Im new to C#, and my book is vague and doesnt cover much about this subject.
Any help is appreciated, thanks! =)
0
MissJoJo
Asked:
MissJoJo
1 Solution
 
AgariciCommented:
take a look here: http://www.bobpowell.net/doublebuffer.htm

hth,
A.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also place a PictureBox on your form and Dock it filled so it takes up the whole form.  Then build a Bitmap and draw your Table into it.  Set your Bitmap as the PictureBox Image and then in the PictureBox Paint event all you need to do is Draw the Cue Ball.

Something like...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private int XX, YY;
        private int DX = 5, DY = 5;
        //private double F=10,A=0,Fr=0.02;
        private StatusBar statBar = new StatusBar();
        private StatusBarPanel statBarPanel = new StatusBarPanel();
        private ContextMenu popup;
        private Timer timer1 = new Timer();
        private Timer timer2 = new Timer();
        private PictureBox pb = new PictureBox();

        private void MainForm_Load(object sender, EventArgs e)
        {                    
            this.XX = 250;
            this.YY = 250;
            Height = 610;
            Width = 550;
            BackColor = Color.SaddleBrown;

            pb.Dock = DockStyle.Fill;
            this.Controls.Add(pb);

            Bitmap bmp = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bmp);
            Rectangle r = new Rectangle(125, 10, 275, 530);
            g.Clear(this.BackColor);          
            g.FillRectangle(Brushes.OliveDrab, r);
            g.FillPie(Brushes.Black, 100, -15, 50, 50, 0, 90);
            g.FillPie(Brushes.Black, 375, -15, 50, 50, 90, 90);
            g.FillPie(Brushes.Black, 100, 515, 50, 50, 270, 90);
            g.FillPie(Brushes.Black, 375, 515, 50, 50, 180, 90);
            g.FillPie(Brushes.Black, 107, 250, 35, 35, 270, 180);
            g.FillPie(Brushes.Black, 382, 250, 35, 35, 90, 180);
            g.Dispose();
            pb.Image = bmp;

            this.Text = "Billiards for Dummies";
            this.MaximizeBox = false;          
            CenterToScreen();

            /*A = Math.Atan(DY/DX);
        DX = (int)(F*Math.Cos(A));
        DY = (int)(F*Math.Sin(A));*/

            popup = new ContextMenu();
            popup.MenuItems.Add("Start", new EventHandler(popup_Click));
            popup.MenuItems.Add("Stop", new EventHandler(popup_Click));

            timer1.Interval = 33;
            timer2.Interval = 1000;
            timer1.Enabled = false;
            timer2.Enabled = false;

            BuildStatBar();

            pb.ContextMenu = popup;

            timer1.Tick += new EventHandler(timeTick);
            timer2.Tick += new EventHandler(statBarThing);
            pb.MouseUp += new MouseEventHandler(OnMouseUp);
            pb.Paint += new PaintEventHandler(MainForm_Paint);
        }

        public void OnMouseUp(object sender, MouseEventArgs e)
        {
            //statBarPanel.Text = "Clicked at: (" + e.X + ", " + e.Y + ")";
            this.XX = e.X;
            this.YY = e.Y;
            Invalidate();
        }


        // **************** Clicked at

        public void statBarThing(object sender, EventArgs e)
        {
            timer2.Enabled = true;

            statBarPanel.Text = "Current X: " + this.XX + " Y: " + this.YY + " DX: " + this.DX + " DY: " + this.DY;
            Invalidate();
        }

        // **************** Paint items on window

        public void MainForm_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillEllipse(Brushes.White, this.XX, this.YY, 15, 15);
        }



        // **************** Build the status bar, located below the window.

        public void BuildStatBar()
        {
            statBar.ShowPanels = true;
            statBar.Size = new Size(550, 20);
            statBar.Panels.AddRange(new StatusBarPanel[] { statBarPanel });
            statBarPanel.BorderStyle = StatusBarPanelBorderStyle.None;
            statBarPanel.AutoSize = StatusBarPanelAutoSize.Spring;

            this.Controls.Add(statBar);
        }

        // **************** Start/Stop

        private void popup_Click(object sender, EventArgs e)
        {
            MenuItem mClick = (MenuItem)sender;
            string item = mClick.Text;

            if (item == "Start")
            {
                timer1.Enabled = true;
                timer2.Enabled = true;
            }
            if (item == "Stop")
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
            }
            pb.Invalidate();
        }


        // **************** Making the ball move

        private void timeTick(object sender, EventArgs e)
        {
            this.XX = this.XX + DX;
            this.YY = this.YY + DY;
            //F -= Fr;

            if (this.XX > 382 || XX < 128)
            {
                DX = (-DX);
                //DX = -((int)(F*Math.Cos(A)));
                //DY = (int)(F*Math.Sin(A));
                //A = Math.PI - A;
                //A = Math.Atan(DY/DX);
                //A = Math.PI - (Math.Atan(DY/DX));
            }
            if (this.YY > 520 || YY < 15)
            {
                DY = (-DY);
                //DX = (int)(F*Math.Cos(A));
                //DY = -((int)(F*Math.Sin(A)));
                //A = -A;
            }


            //if ( F == 0 )
            //     timer1.Enabled = false;

            pb.Invalidate();
        }
    }
}
0
 
MissJoJoAuthor Commented:
Thanks a bunch Idle Mind!
That really helped me =)
Thanks again =)

PS. Sorry for the late reply.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now