http://www.codeproject.com/cs/media/flickerFreeDrawing.asp

解决方案 »

  1.   

    http://www.codeproject.com/cs/media/flickerFreeDrawing.asp
      

  2.   

    如下是我以前自己写的:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Threading;namespace MyAlbum
    {
    /// <summary>
    /// Summary description for frmFullScreen.
    /// </summary>
    public class frmFullScreen : System.Windows.Forms.Form
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; private Bitmap bitCurrent=null;
    private Point ptCurrent;
    private Point ptStart;
    private Size sShowing;
    private Point ptMouseDown; private const int MOVESIZE=20;
    private Cursor curApp=null;
    private Cursor curImage=null; public frmFullScreen(ref Bitmap bitShow)
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    bitCurrent=bitShow; ptStart=new Point(0,0);
    sShowing=new Size(0,0);
    if(bitCurrent.Width<Screen.PrimaryScreen.Bounds.Width)
    {
    ptStart.X=(Screen.PrimaryScreen.Bounds.Width-bitCurrent.Width)/2;
    sShowing.Width=bitCurrent.Width;
    }
    else
    {
    ptStart.X=0;
    sShowing.Width=Screen.PrimaryScreen.Bounds.Width;
    } if(bitCurrent.Height<Screen.PrimaryScreen.Bounds.Height)
    {
    ptStart.Y=(Screen.PrimaryScreen.Bounds.Height-bitCurrent.Height)/2;
    sShowing.Height=bitCurrent.Height;
    }
    else
    {
    ptStart.Y=0;
    sShowing.Height=Screen.PrimaryScreen.Bounds.Height;
    } ptCurrent=new Point(0,0);
    ptMouseDown=new Point(0,0);
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } private void frmFullScreen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if(e.KeyCode==Keys.Escape)
    this.Close(); if(e.KeyCode==Keys.Up)
    {
    if(sShowing.Height<Screen.PrimaryScreen.Bounds.Height)
    return;
    else if(ptCurrent.Y==0)
    return;
                    
    if((ptCurrent.Y-MOVESIZE)<0)
    ptCurrent.Y=0;
    else
    ptCurrent.Y=ptCurrent.Y-MOVESIZE; //Then redraw
    this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
    } if(e.KeyCode==Keys.Down)
    {
    if(sShowing.Height<Screen.PrimaryScreen.Bounds.Height)
    return;
    else if((ptCurrent.Y+sShowing.Height)==bitCurrent.Height)
    return; if((ptCurrent.Y+MOVESIZE+sShowing.Height)<bitCurrent.Height)
    ptCurrent.Y+=MOVESIZE;
    else
    ptCurrent.Y=bitCurrent.Height-sShowing.Height; //Then redraw
    this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
    } if(e.KeyCode==Keys.Left)
    {
    if(sShowing.Width<Screen.PrimaryScreen.Bounds.Width)
    return;
    else if(ptCurrent.X==0)
    return;
                    
    if((ptCurrent.X-MOVESIZE)<0)
    ptCurrent.X=0;
    else
    ptCurrent.X-=MOVESIZE; //Then redraw
    this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
    } if(e.KeyCode==Keys.Right)
    {
    if(sShowing.Width<Screen.PrimaryScreen.Bounds.Width)
    return;
    else if((ptCurrent.X+sShowing.Width)==bitCurrent.Width)
    return; if((ptCurrent.X+MOVESIZE+sShowing.Width)<bitCurrent.Width)
    ptCurrent.X+=MOVESIZE;
    else
    ptCurrent.X=bitCurrent.Width-sShowing.Width; //Then redraw
    this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
    }
    } protected override void OnPaint(PaintEventArgs e)
    {
    e.Graphics.DrawImage(bitCurrent,
    new Rectangle(ptStart,sShowing),
    new Rectangle(ptCurrent,sShowing),
    GraphicsUnit.Pixel);
    } private void frmFullScreen_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if((e.X>ptStart.X)&&(e.Y>ptStart.Y)&&
    (e.X<(ptStart.X+sShowing.Width))&&(e.Y<(ptStart.Y+sShowing.Height)))
    {
    this.Cursor=curImage;
    }
    else
    {
    this.Cursor=curApp;
    return;
    } if(e.Button==MouseButtons.Left)
    {
    bool blnXChanged=false;
    bool blnYChanged=false;
    if(e.Y>ptMouseDown.Y)
    {
    if((sShowing.Height>=Screen.PrimaryScreen.Bounds.Height)
    &&(ptCurrent.Y!=0))
    {
    blnYChanged=true;
    if((ptCurrent.Y-(e.Y-ptMouseDown.Y))<0)
    ptCurrent.Y=0;
    else
    ptCurrent.Y-=(e.Y-ptMouseDown.Y);
    }
    } if(e.Y<ptMouseDown.Y)
    {
    if((sShowing.Height>=Screen.PrimaryScreen.Bounds.Height)
    &&((ptCurrent.Y+sShowing.Height)!=bitCurrent.Height))
    {
    blnYChanged=true;
    if((ptCurrent.Y+(ptMouseDown.Y-e.Y)+sShowing.Height)<bitCurrent.Height)
    ptCurrent.Y+=(ptMouseDown.Y-e.Y);
    else
    ptCurrent.Y=bitCurrent.Height-sShowing.Height;
    }
    } if(e.X>ptMouseDown.X)
    {
    if((sShowing.Width>=Screen.PrimaryScreen.Bounds.Width)
    &&(ptCurrent.X!=0))
    {
    blnXChanged=true;
    if((ptCurrent.X-(e.X-ptMouseDown.X))<0)
    ptCurrent.X=0;
    else
    ptCurrent.X-=(e.X-ptMouseDown.X);
    }
    } if(e.X<ptMouseDown.X)
    {
    if((sShowing.Width>=Screen.PrimaryScreen.Bounds.Width)
    &&((ptCurrent.X+sShowing.Width)!=bitCurrent.Width))
    {
    if((ptCurrent.X+(ptMouseDown.X-e.X)+sShowing.Width)<bitCurrent.Width)
    ptCurrent.X+=(ptMouseDown.X-e.X);
    else
    ptCurrent.X=bitCurrent.Width-sShowing.Width;
    }
    } //Then redraw
    if(blnXChanged||blnYChanged)
    this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
    ptMouseDown.X=e.X;
    ptMouseDown.Y=e.Y;
    //Thread.Sleep(50);
    }
    } private void frmFullScreen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(e.Button==MouseButtons.Left)
    {
    ptMouseDown.X=e.X;
    ptMouseDown.Y=e.Y;
    }
    }
    }
    }