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.AutoScro llPosition.X;
int stringY = 10 + panel1.AutoScro llPosition.Y;
grPaint.DrawStr ing("This is a demo -- draw a grid.And expect that it works
well whenever.", new Font("Arial",12 ), new SolidBrush(Colo r.Black),
stringX, stringY);
and
int startX = 5*_RECTSIZE_ + panel1.AutoScro llPosition.X;
int startY = 3*_RECTSIZE_ + panel1.AutoScro llPosition.Y;
for(int i=0;i<iCols;i++ )
{
for(int j=0;j<iRows;j++ )
{
grPaint.DrawRec tangle(blackpen , startX + i*_RECTSIZE_, startY +
j*_RECTSIZE_, _RECTSIZE_, _RECTSIZE_);
}
}
And here is the entire code for it
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace Sample
{
public class frmDemo : System.Windows. Forms.Form
{
private Sample.Panel panel1;
private System.Componen tModel.IContain er 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()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Initialize components
private void InitializeCompo nent()
{
this.components = new System.Componen tModel.Containe r();
this.panel1 = new Sample.Panel ();
this.imageList1 = new System.Windows. Forms.ImageList (this.component s);
this.label1 = new System.Windows. Forms.Label();
this.panel1.Sus pendLayout();
this.SuspendLay out();
//
// panel1
//
this.panel1.Aut oScroll = true;
this.panel1.Con trols.Add(this. label1);
this.panel1.Loc ation = new System.Drawing. Point(0, 0);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(344, 328);
this.panel1.Tab Index = 0;
this.panel1.Pai nt += new
System.Windows. Forms.PaintEven tHandler(this.p anel1_OnPaint);
//
// imageList1
//
this.imageList1 .ImageSize = new System.Drawing. Size(16, 16);
this.imageList1 .TransparentCol or = System.Drawing. Color.Transpare nt;
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(256, 304);
this.label1.Nam e = "label1";
this.label1.Tab Index = 0;
this.label1.Tex t = "label1";
//
// frmDemo
//
this.AutoScaleB aseSize = new System.Drawing. Size(6, 14);
this.ClientSize = new System.Drawing. Size(376, 349);
this.Controls.A dd(this.panel1) ;
this.Name = "frmDemo";
this.Text = "Draw a grid";
this.Load += new System.EventHan dler(this.frmDe mo_Load);
this.panel1.Res umeLayout(false );
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new frmDemo());
}
private void panel1_OnPaint( object sender,
System.Windows. Forms.PaintEven tArgs e)
{
Graphics grPaint = e.Graphics;
grPaint.Flush(S ystem.Drawing.D rawing2D.FlushI ntention.Sync);
SolidBrush brushWhite = new SolidBrush(Colo r.White);
Pen blackpen = new Pen(Color.Black ,2);
// Clear the screen
grPaint.FillRec tangle(brushWhi te, e.ClipRectangle );
// Draw the grid
// You need to adjust top left by adding the negative autoscrollposit ion
int stringX = _RECTSIZE_ + panel1.AutoScro llPosition.X;
int stringY = 10 + panel1.AutoScro llPosition.Y;
grPaint.DrawStr ing("This is a demo -- draw a grid.And expect that it
works well whenever.", new Font("Arial",12 ), new SolidBrush(Colo r.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.AutoScro llPosition.X;
int startY = 3*_RECTSIZE_ + panel1.AutoScro llPosition.Y;
for(int i=0;i<iCols;i++ )
{
for(int j=0;j<iRows;j++ )
{
grPaint.DrawRec tangle(blackpen , startX + i*_RECTSIZE_, startY +
j*_RECTSIZE_, _RECTSIZE_, _RECTSIZE_);
}
}
brushWhite.Disp ose();
blackpen.Dispos e();
}
private void frmDemo_Load(ob ject sender, System.EventArg s e)
{
}
}
class Panel :System.Windows .Forms.Panel
{
[System.Security .Permissions.Pe rmissionSet(Sys tem.Security.Pe rmissions.Secur ityAction.Deman d,
Name="FullTrust ")]
protected override void OnKeyDown(KeyEv entArgs 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(re f m);
}
}
}
--
Using M2, Opera's revolutionary e-mail client:
http://www.opera.com/m2/