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.FlushIntent ion.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.S ecurity.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);
}
}
}
--
Using M2, Opera's revolutionary e-mail client:
http://www.opera.com/m2/