自定义控件中的GDI Onpaint问题/问题 [英] GDI Onpaint Problem/Question in Custom control

查看:87
本文介绍了自定义控件中的GDI Onpaint问题/问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我做了一个自定义控件,当鼠标按下时绘制一个矩形,鼠标启动时什么都不做。

我设置/重置一个标志在MouseDown / Mouse up中使用它在OnPaint中进行绘图。


当我按下鼠标时,重新绘制正确,但是当我释放鼠标时,背景不会恢复


我应该在Onpaint中做些什么来确保正确恢复背景(表格)?


这个问题已经让我忙碌了2天......


Johan


将此代码复制到新的c#app

中使用System;


使用System.Drawing;


使用System.Collections;


使用System.ComponentModel ;


使用System.Windows.Forms;


使用System.Data;


使用System.Drawing.Imaging;


使用System.Reflection;


名称空间WindowsApplication2


{


///< summary>


/// Form1的摘要说明。


///< / summary>


公共类Form1:System.Windows.Forms.Form

{


///< summary>


///所需的设计师变量。


///< / summary>


private System.ComponentModel.Container components = null;


private Hotspot现场;


公共表格1()


{


//


// Windows窗体设计器支持需要


//


InitializeComponent();
< br $>
//


// TODO:在InitializeComponent调用后添加任何构造函数代码


//


}


///< summary>


///清理正在使用的所有资源。


///< / summary>


protected override void Di spose(bool处理)


{


if(处理)


{


if(components!= null)


{


components.Dispose();


}


}


base.Dispose(disposing);


}


#region Windows窗体设计器生成的代码


///< summary>

/// Designer支持所需的方法 - 请勿使用代码编辑器修改


///此方法的内容。


///< / summary>


private void InitializeComponent()


{


//


// Form1


//


this.AutoScaleBaseSize = new System.Drawing.Size(5,13);


this.BackColor = System.Drawing.SystemColors.Desktop;


this .ClientSize = new System.Drawing.Size(152,141);


this.Name =" Form1&quo t ;;


this.Text =" Form1";


this.Load + = new System.EventHandler(this.Form1_Load) ;


}


#endregion


///< summary>


///申请的主要入口点。


///< / summary>


[STAThread]


静态无效主要()


{


Application.Run (新Form1());


}


private void Form1_Load(object sender,System.EventArgs e)


{


spot = new Hotspot();


spot.Size = new Size(50,50);


spot.Location = new Point(40,40);


spot.BringToFront();


spot.Visible = true;


this.Controls.Add(spot);

}


}


公共类热点:控制


{


/ /私人图片图片;


私人博ol bPushed;


私人位图m_bmpOffscreen;

刷GrayBrush;


公共热点()


{


bPushed = false;


//默认最小尺寸


this.Size = new Size(21,21);


GrayBrush =新的SolidBrush(Color.Gray);


}


protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)


{


Graphics gxOff; //屏幕外图形

if(m_bmpOffscreen == null)//双重缓冲位图


{


m_bmpOffscreen = new Bitmap(ClientSize.Width,ClientSize.Height);


}


gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(Color.Empty);

if(bPushed)


{


gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10,ClientSize.Height-10);


e.Graphics.DrawImage(m_bmpOffscreen,0,0); < br $>
}

base.OnPaint(e);


}


受保护的覆盖void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e)


{


}


protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)


{


bPushed = true;


Invalidate();


}


protected override void OnMouseUp(System.Windows.Forms.Mouse EventArgs e)


{


bPushed = false;


Invalidate();


}


}

}


I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up.
I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint .

The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored

What should i do in the Onpaint to make sure the background (the form) is restored correctly ?

This problem already keeps me busy for 2 days ...

Johan

copy this code in a new win c# app
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Imaging;

using System.Reflection;

namespace WindowsApplication2

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private Hotspot spot ;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackColor = System.Drawing.SystemColors.Desktop;

this.ClientSize = new System.Drawing.Size(152, 141);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

spot=new Hotspot() ;

spot.Size=new Size(50,50);

spot.Location=new Point(40,40);

spot.BringToFront();

spot.Visible=true;

this.Controls.Add(spot);
}

}


public class Hotspot : Control

{

//private Image image;

private bool bPushed;

private Bitmap m_bmpOffscreen;
Brush GrayBrush;

public Hotspot()

{

bPushed = false;

//default minimal size

this.Size = new Size(21, 21);

GrayBrush=new SolidBrush(Color.Gray);

}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )

{

Graphics gxOff; //Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering

{

m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);

}

gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(Color.Empty);
if (bPushed)

{

gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10, ClientSize.Height-10);

e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
}
base.OnPaint(e);

}

protected override void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e )

{

}

protected override void OnMouseDown ( System.Windows.Forms.MouseEventArgs e )

{

bPushed = true;

Invalidate();

}

protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs e )

{

bPushed = false;

Invalidate();

}

}
}

推荐答案

你是否在你的onpaint覆盖中调用基类绘画?

你是否在鼠标上调用Invalidate()?

你是否覆盖了背景涂料 - 我看到太多建议说要做到这一点,我认为在大多数情况下都没有意义。

-

Justin Weinberg


设计PrintDocument?绘制到表格?

www.mrgsoft.com

" Sagaert Johan" < RE ******************* @ hotmail.com>在消息新闻中写道:英国************** @ TK2MSFTNGP12.phx.gbl ...


我制作了一个自定义控件当鼠标按下时矩形,鼠标按下时什么也不做。

我在MouseDown / Mouse up中设置/重置一个标志并使用它在OnPaint中进行绘图。


当我按下鼠标时重新绘制正确,但是当我释放鼠标时背景没有恢复


我应该在Onpaint中做什么来制作确定背景(表格)是否正确恢复?


这个问题已经让我忙了2天......


Johan


将此代码复制到新的c#app

使用系统;


使用System.Drawing;


使用System.Collections;


使用System.ComponentModel;


使用System.Windows.Forms;


使用System.Data;


使用System.Drawing.Imaging;


使用Syst em.Reflection;


名称空间WindowsApplication2


{


///< summary>


/// Form1的摘要说明。


///< / summary>


public class Form1:System.Windows.Forms.Form


{


///< summary>


///所需的设计变量。


///< / summary>


private System.ComponentModel .Container components = null;


私密热点;


public Form1()


{$ / $

//


// Windows窗体设计器支持需要


//


InitializeComponent();


//


// TODO:之后添加任何构造函数代码InitializeComponent调用


//


}


///< summary>


///清理正在使用的所有资源。


///< / summary>


protected override void Dispose(bool disposing)


{


if(处理)


{


if(components!= null)


{


components.Dispose();


}


}


base.Dispose(disposing);


}


#region Windows窗体设计器生成的代码


///< summary>


///设计师支持所需的方法 - 不要修改


///使用代码编辑器的方法内容。


///< / summary>


private void InitializeComponent ()


{


//


// Form1


//


this.AutoScaleBaseSize = new System.Drawing.Size(5,13);


this。 BackColor = System.Drawing.SystemColors.Desktop;


this.ClientSize = new System.Dra wing.Size(152,141);


this.Name =" Form1";


this.Text =" Form1" ;


this.Load + = new System.EventHandler(this.Form1_Load);


}


#endregion


///< summary>


///应用程序的主要入口点。


///< / summary>


[STAThread]


static void Main()


{


Application.Run(新Form1());


}


private void Form1_Load(object sender,System.EventArgs e)


{


spot = new Hotspot ();


spot.Size = new尺寸(50,50);


spot.Location = new Point(40,40) ;


spot.BringToFront();


spot.Visible = true;


this .Controls.Add(现货);

}


}


公共类热点:控制


{


//私人图片图片;


private bool bPushed;


私有位图m_bmpOffscreen;

刷GrayBrush;


公共热点()


{


bPushed = false;


//默认最小尺寸

this.Size = new Size(21,21);


GrayBrush =新的SolidBrush(Color.Gray);


}


protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)


{


Graphics gxOff; //屏幕外图形

if(m_bmpOffscreen == null)//双重缓冲位图


{


m_bmpOffscreen = new Bitmap(ClientSize.Width,ClientSize.Height);


}


gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(Color.Empty);

if(bPushed)


{


gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10,ClientSize.Height-10);


e.Graphics.DrawImage(m_bmpOffscreen,0,0); < br $>
}

base.OnPaint(e);


}


受保护的覆盖void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e)


{


}


protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)


{


bPushed = true;


Invalidate();


}


protected override void OnMouseUp(System.Windows.Forms.Mouse EventArgs e)


{


bPushed = false;


Invalidate();


}


}

}
Are you calling the base class paint in your onpaint override?
Are you calling Invalidate() on mouse up?
Did you override background paint - I''ve seen too many suggestions saying to do this and I don''t think for most situations it makes sense.
--
Justin Weinberg

Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com
"Sagaert Johan" <RE*******************@hotmail.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl...

I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up.
I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint .

The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored

What should i do in the Onpaint to make sure the background (the form) is restored correctly ?

This problem already keeps me busy for 2 days ...

Johan

copy this code in a new win c# app
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Imaging;

using System.Reflection;

namespace WindowsApplication2

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private Hotspot spot ;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackColor = System.Drawing.SystemColors.Desktop;

this.ClientSize = new System.Drawing.Size(152, 141);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

spot=new Hotspot() ;

spot.Size=new Size(50,50);

spot.Location=new Point(40,40);

spot.BringToFront();

spot.Visible=true;

this.Controls.Add(spot);
}

}


public class Hotspot : Control

{

//private Image image;

private bool bPushed;

private Bitmap m_bmpOffscreen;
Brush GrayBrush;

public Hotspot()

{

bPushed = false;

//default minimal size

this.Size = new Size(21, 21);

GrayBrush=new SolidBrush(Color.Gray);

}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )

{

Graphics gxOff; //Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering

{

m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);

}

gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(Color.Empty);
if (bPushed)

{

gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10, ClientSize.Height-10);

e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
}
base.OnPaint(e);

}

protected override void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e )

{

}

protected override void OnMouseDown ( System.Windows.Forms.MouseEventArgs e )

{

bPushed = true;

Invalidate();

}

protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs e )

{

bPushed = false;

Invalidate();

}

}
}




一般来说,你的热点控件存在多种错误

(但是你会学到更多的东西)。然而

造成你所有问题的两个是一个非常小的逻辑错误

和颜色错误。


首先,你正在清理你的控件的表面,颜色为空,

这实际上根本就没有颜色(因此它没有清除任何东西,因为

你)和什么是在控制器坐下之前,在你包括随后的任何东西之后仍然是在那里。


第二个是你似乎意外地把你的绘图在''bPushed''if语句中显示
表面。这是一个微妙的但是很重要的错误。由于你正在绘制你的屏幕外位图,你需要绘制位图,无论你是否正在为你的压力或非压力状态做出



因此,你的绘制循环应如下所示:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e



{

图形gxOff; //屏外图形


if(m_bmpOffscreen == null)//双重缓冲的位图

m_bmpOffscreen = new Bitmap(ClientSize.Width,

ClientSize.Height);


gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(Color.White); //或者你想要的任何颜色

喜欢


if(bPushed)

gxOff.FillRectangle(GrayBrush,5,5, ClientSize.Width-10,

ClientSize.Height-10);


e.Graphics.DrawImage(m_bmpOffscreen,0,0);

gxOff.Dispose();


base.OnPaint(e);

}

将修复你的问题。

Allen Anderson
http://www.glacialcomponents。 com


On Thu,2003年10月9日19:52:23 +0200,Sagaert Johan

< RE*******************@hotmail.com>写道:

there are multiple things wrong in general with your hotspot control
(but they are things you''ll learn as you do more with them). However
the two that are causing all your problems is a very small logic error
and a color error.

First, you are clearing your control''s surface with the color Empty,
which in fact is no color at all (hence its not clearing anything for
you) and whatever was there before the control took a seat, is still
there after including anything you subsequently draw.

The second is that you seem to have accidently put your drawing to
surface inside your ''bPushed'' if statement. This is a subtle but
important error. Since you are drawing to your offscreen bitmap, you
ALWAYS need to draw the bitmap regardless of whether you are doing it
for your pressed or nonpressed state.

Therefore, your draw loop should look as follows,

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e
)
{
Graphics gxOff; //Offscreen graphics

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

gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(Color.White); // or whatever color you would
like

if (bPushed)
gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10,
ClientSize.Height-10);

e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
gxOff.Dispose();

base.OnPaint(e);
}
that will fix your problem.
Allen Anderson
http://www.glacialcomponents.com


On Thu, 9 Oct 2003 19:52:23 +0200, "Sagaert Johan"
<RE*******************@hotmail.com> wrote:

我做了一个自定义控件,当鼠标按下时绘制一个矩形,鼠标启动时什么都不做。
我设置/在MouseDown / Mouse up中重置一个标志并使用它在OnPaint中进行绘图。

当我按下鼠标时重新绘制正确,但是当我释放鼠标时,背景不会恢复
这个问题已经让我忙了2天......

约翰

将此代码复制到一个新的胜利c#app

使用系统;

使用System.Drawing;

使用System.Collections;

使用System.ComponentModel;

使用System.Windows.Forms;

使用System.Data;

使用System.Drawging;

使用System.Reflection;

命名空间WindowsApplication2

{

///< summary>

/// Summ Form1的ary描述。

///< / summary>

公共类Form1:System.Windows.Forms.Form

{

///< summary>

///所需的设计师变量。

///< / summary>
<私有System.ComponentModel.Container组件= null;

私密热点;

公共Form1()

{

//

// Windows窗体设计器支持需要

//

InitializeComponent();

//

// TODO:在InitializeComponent调用后添加任何构造函数代码

//

}

///< summary>

///清理正在使用的所有资源。

///< / summary>

受保护覆盖void Dispose(bool处理)


如果(处理)

{if / components!= null)

{

components.Dispose();

}

}
base.Dispose(disposing);

}

#region Windows窗体设计器生成的代码

///< summary>

///设计器支持所需的方法 - 不要修改<使用代码编辑器调用此方法的内容。

///< / summary>
私有void InitializeComponent()<

//

// Form1

//

这个.AutoScaleBaseSize = new System.Drawing.Size(5,13);

this.BackColor = System.Drawing.SystemColors.Desktop;

this.ClientSize = new System.Drawing。大小(152,141);

this.Name =" Form1";

this.Text =" Form1";

这个.Load + = new System.EventHandler(this.Form1_Load);

}
#endregion

///< summary>

///应用程序的主要入口点。

///< / summary>

[STAThread]

static void Main()


Application.Run(new Form1());

}
私void Form1_Load(object sender,System.EventArgs e)



spot = new Hotspot();

spot.Size = new Size( 50,50);

spot.Location = new Point(40,40);

spot.BringToFront();

spot.Visible = true;

this.Controls.Add(现货);

}

}


公共类热点:控制

//私人图像;

私人bool bPushed;

私人位图m_bmpOffscreen;

刷GrayBrush;

公共热点()



bPushed = false;

//默认最小尺寸

this.Size = new Size(21,21);

GrayBrush = new SolidBrush(Color.Gray);

}

受保护的覆盖void OnPaint(System.Windows.Forms.PaintEventArgs e)



图形gxOff; //屏幕外图形

if(m_bmpOffscreen == null)//双缓冲位图





m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);

}
gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(Color.Empty);

if(bPushed)



gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Widt h-10,ClientSize.Height-10) ;

e.Graphics.DrawImage(m_bmpOffscreen,0,0);

}

base.OnPaint(e);

}

protected override void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e)





protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)



bPushed = true;

Invalidate() ;



受保护的覆盖void OnMouseUp(System.Windows.Forms.MouseEventArgs e)



bPushed = false;

无效();

}

}

}

I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up.
I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint .

The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored

What should i do in the Onpaint to make sure the background (the form) is restored correctly ?

This problem already keeps me busy for 2 days ...

Johan

copy this code in a new win c# app
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Imaging;

using System.Reflection;

namespace WindowsApplication2

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private Hotspot spot ;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackColor = System.Drawing.SystemColors.Desktop;

this.ClientSize = new System.Drawing.Size(152, 141);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

spot=new Hotspot() ;

spot.Size=new Size(50,50);

spot.Location=new Point(40,40);

spot.BringToFront();

spot.Visible=true;

this.Controls.Add(spot);
}

}


public class Hotspot : Control

{

//private Image image;

private bool bPushed;

private Bitmap m_bmpOffscreen;
Brush GrayBrush;

public Hotspot()

{

bPushed = false;

//default minimal size

this.Size = new Size(21, 21);

GrayBrush=new SolidBrush(Color.Gray);

}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )

{

Graphics gxOff; //Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering

{

m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);

}

gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(Color.Empty);
if (bPushed)

{

gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Widt h-10, ClientSize.Height-10);

e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
}
base.OnPaint(e);

}

protected override void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e )

{

}

protected override void OnMouseDown ( System.Windows.Forms.MouseEventArgs e )

{

bPushed = true;

Invalidate();

}

protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs e )

{

bPushed = false;

Invalidate();

}

}
}






嗨Johan,


1.不要使用Color.Empty来清除位图。这不清楚。

你应该使用颜色。


类似于:

gxOff.Clear(这个.BackColor);


2.移动

e.Graphics.DrawImage(m_bmpOffscreen,0,0);

out of if(bPushed)块。仅当按钮为
down时才绘制控件。如果按钮已经启动,你就不会画(清除)任何东西。


除了我有一个建议。如果你想自己画出整个透明矩形

,请考虑设置控件样式:


this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |

ControlStyles.AllPaintingInWmPaint,true);


即使您有自己的屏幕外位图,也应按顺序设置

ControlStyles.DoubleBuffer样式为了避免闪烁。


HTH

B \ rgds


Hi Johan,

1. Dont use Color.Empty for clearing the bitmap. This won''t clear anything.
You should use color instead.

something like:
gxOff.Clear(this.BackColor);

2. Move
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
out of the if(bPushed) block. You paint the control only if the button is
down. And you don''t paint (clear ) anything if the button is up.

Beside I have one suggestion. If you want to paint the whole clent rectangle
by yourself consider setting control styles:

this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);

Even if you have your own offscreen bitmap you should set
ControlStyles.DoubleBuffer style in order to avoid flickering.

HTH
B\rgds
100


这篇关于自定义控件中的GDI Onpaint问题/问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆