2009-03-01 89 views
0

我正在開發和C#應用程序Windows Mobile。我有一個自定義控件,用OnPaint覆蓋來繪製用戶隨指針移動的圖像。我自己的OnPaint方法是這樣的:顯示圖像的自定義控件:移動圖像

 

protected override void OnPaint(PaintEventArgs e) 
{ 
    Graphics gxOff; //Offscreen graphics 
    Brush backBrush; 

    if (m_bmpOffscreen == null) //Bitmap for doublebuffering 
    { 
     m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height); 
    } 

    gxOff = Graphics.FromImage(m_bmpOffscreen); 

    gxOff.Clear(Color.White); 

    backBrush = new SolidBrush(Color.White); 
    gxOff.FillRectangle(backBrush, this.ClientRectangle); 

    //Draw some bitmap 
    gxOff.DrawImage(imageToShow, 0, 0, rectImageToShow, GraphicsUnit.Pixel); 

    //Draw from the memory bitmap 
    e.Graphics.DrawImage(m_bmpOffscreen, this.Left, this.Top); 

    base.OnPaint(e); 
} 
 

imageToShow它的圖像。

rectImageToShow它以這種方式對事件onResize受到初始化:

 
rectImageToShow = 
    new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height); 
 

this.Topthis.Left是的左上角繪製自定義控件內的圖像。

我認爲它可以正常工作,但是當我移動圖像時,它永遠不會清除所有的控件。我總是看到以前繪圖的一部分。

我在做什麼錯了?

謝謝!

回答

2

我想你還沒有清除控件的圖像緩衝區。您只清除了後臺緩衝區。在2個DrawImage調用之間試試:

e.Graphics.Clear(Color.White); 

這應該先清除任何剩餘的圖像。


或者,你可以把它改寫所以一切都畫上到後臺緩衝區,然後回緩衝區繪製到屏幕上正好(0,0),所以任何問題,是因爲後臺緩衝區繪圖邏輯而不是介於兩者之間。

事情是這樣的:

Graphics gxOff; //Offscreen graphics 
Brush backBrush; 

if (m_bmpOffscreen == null) //Bitmap for doublebuffering 
{ 
    m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height); 
} 

// draw back buffer 
gxOff = Graphics.FromImage(m_bmpOffscreen); 

gxOff.Clear(Color.White); 

backBrush = new SolidBrush(Color.White); 

gxOff.FillRectangle(backBrush, this.Left, this.Top, 
    this.ClientRectangle.Width, 
    this.ClientRectangle.Height); 

//Draw some bitmap 
gxOff.DrawImage(imageToShow, this.Left, this.Top, rectImageToShow, GraphicsUnit.Pixel); 

//Draw from the memory bitmap 
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0); 

base.OnPaint(e); 

不知道這是正確的,但你應該明白我的意思。

+0

它適用於: e.Graphics.Clear(Color.White); – VansFannel 2009-03-01 09:17:28