关键词:GDI、OnPaint、AutoScroll、滚动、重绘
我在PANEL里画一个简单格子,但是当我的panel1的滚动设置成TRUE的时候,效果不是预期的效果,请问该如何处理。代码如下:
private MyApp.Panel panel1;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics grPaint = e.Graphics;
grPaint.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);
SolidBrush brushWhite = new SolidBrush(Color.White);
Pen blackpen = new Pen(Color.Black,2);
// Clear the screen
grPaint.FillRectangle(brushWhite, e.ClipRectangle);
// Draw the grid
grPaint.DrawString("哈哈", new Font("Arial",12), new SolidBrush(Color.Black),_RECTSIZE_,10);
grPaint.DrawString("西西", new Font("Arial",12), new SolidBrush(Color.Black),_RECTSIZE_,30);
for(int i=0;i<iCols;i++)
{
for(int j=0;j<iRows;j++)
{
grPaint.DrawRectangle(blackpen, 5 * _RECTSIZE_ + (i* _RECTSIZE_), 3 * _RECTSIZE_ + (j*_RECTSIZE_),_RECTSIZE_,_RECTSIZE_);
}
}
brushWhite.Dispose();
blackpen.Dispose();
}
class Panel :System.Windows.Forms.Panel
{
public static bool ibScroll;
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m)
{
if(m.Msg == 0x0114)
{
Invalidate();
}
else if(m.Msg == 0x0115)
{
Invalidate();
}
else if(m.Msg == 0x020A)
{
Invalidate();
}
else
{
ibScroll = false;
}
base.WndProc(ref m);
}
}