关键词:GDI、OnPaint、AutoScroll、滚动、重绘

NewSun99 2003-09-11 11:49:38
我在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);
}
}
...全文
59 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohedou 2003-09-16
  • 打赏
  • 举报
回复
look ^ study
dotnba 2003-09-16
  • 打赏
  • 举报
回复
Well, I can see one problem with it. You aren't changing the start
position of the drawing. You can see this by scrolling, then cover the
window with another window and move the other window away, the text and
grid will always be in the same spot starting at window coordinate
_RECTSIZE_ * 1, 3 or 5.

I've made a few ints indicating start of the x-and y-coordinate of the
string and the grid.

int stringX = _RECTSIZE_ + panel1.AutoScrollPosition.X;
int stringY = 10 + panel1.AutoScrollPosition.Y;
grPaint.DrawString("This is a demo -- draw a grid.And expect that it works
well whenever.", new Font("Arial",12), new SolidBrush(Color.Black),
stringX, stringY);

and

int startX = 5*_RECTSIZE_ + panel1.AutoScrollPosition.X;
int startY = 3*_RECTSIZE_ + panel1.AutoScrollPosition.Y;
for(int i=0;i<iCols;i++)
{
for(int j=0;j<iRows;j++)
{
grPaint.DrawRectangle(blackpen, startX + i*_RECTSIZE_, startY +
j*_RECTSIZE_, _RECTSIZE_, _RECTSIZE_);
}
}


And here is the entire code for it


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Sample
{
public class frmDemo : System.Windows.Forms.Form
{
private Sample.Panel panel1;
private System.ComponentModel.IContainer components;
private int iCols = 10 ,iRows = 6;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Label label1;
const int _RECTSIZE_ = 30;
public frmDemo()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Initialize components
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.panel1 = new Sample.Panel ();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(344, 328);
this.panel1.TabIndex = 0;
this.panel1.Paint += new
System.Windows.Forms.PaintEventHandler(this.panel1_OnPaint);
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// label1
//
this.label1.Location = new System.Drawing.Point(256, 304);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// frmDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 349);
this.Controls.Add(this.panel1);
this.Name = "frmDemo";
this.Text = "Draw a grid";
this.Load += new System.EventHandler(this.frmDemo_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmDemo());
}

private void panel1_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
// You need to adjust top left by adding the negative autoscrollposition
int stringX = _RECTSIZE_ + panel1.AutoScrollPosition.X;
int stringY = 10 + panel1.AutoScrollPosition.Y;
grPaint.DrawString("This is a demo -- draw a grid.And expect that it
works well whenever.", new Font("Arial",12), new SolidBrush(Color.Black),
stringX, stringY);
// Same with this, the starting point of the grid will change when you
scroll and can't be a fixed number.
int startX = 5*_RECTSIZE_ + panel1.AutoScrollPosition.X;
int startY = 3*_RECTSIZE_ + panel1.AutoScrollPosition.Y;
for(int i=0;i<iCols;i++)
{
for(int j=0;j<iRows;j++)
{
grPaint.DrawRectangle(blackpen, startX + i*_RECTSIZE_, startY +
j*_RECTSIZE_, _RECTSIZE_, _RECTSIZE_);
}
}
brushWhite.Dispose();
blackpen.Dispose();
}
private void frmDemo_Load(object sender, System.EventArgs e)
{
}
}
class Panel :System.Windows.Forms.Panel
{

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
MessageBox.Show("This method is never called.");
}
protected override void WndProc(ref Message m)
{
if(m.Msg == 0x0114)
{
}
else if(m.Msg == 0x0115)
{
}
else if(m.Msg == 0x020A)
{
}
else
{
}
base.WndProc(ref m);
}
}
}
dotnba 2003-09-15
  • 打赏
  • 举报
回复
I think you will want to handle the panel1.autoscrollposition in your
OnPaint code to find out where upper left of the grid will start. Beware
that the position will be negative as you move the panel "to the right and
upwards" when scrolling left and down.

I'm not sure what you try to do with WndProc, but if it is scrolling with
keys, you can do that much easier by using the forms OnKeyDown event and
use panel1.AutoScrollPosition = // new position or +/-= increment (beware
of boundaries errors)
NewSun99 2003-09-12
  • 打赏
  • 举报
回复
自己UP
dotnba 2003-09-11
  • 打赏
  • 举报
回复
大家中秋节快乐。

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧