//绘制网格 grPaint.DrawString(" testsdsdsadssadadafsewr3wqr32r",new Font(" Arial",12),new SolidBrush( Color.Black),_ RECTSIZE_,10); grPaint.DrawString(" testsdsdsadssadadafsewr3wqr32r",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(); } 类Panel:System.Windows.Forms.Panel { [System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.Secur ityAction.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) { 无效(); } 其他 { } base.Wn dProc(ref m); } } I draw a grid on a panel.And panel1'' property AutoScroll is set true.WhenHScroollBar is Scroolling,the grid is error.I have rewritten the mathod of WndProc.But the effect is unexpected.How can i do? Thx a lot.Main codes go here:class frmprivate MyApp.Panel panel1;this.panel1.Paint += newSystem.Windows.Forms.PaintEventHandler(this.OnPain t); private void 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 screengrPaint.FillRectangle(brushWhite, e.ClipRectangle); // Draw the gridgrPaint.DrawString("testsdsdsadssadadafsewr3wqr32r ", newFont("Arial",12), new SolidBrush(Color.Black),_RECTSIZE_,10);grPaint.DrawString("testsdsdsadssadadafsewr3wqr32r ", newFont("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{ [System.Security.Permissions.PermissionSet(System.S ecurity.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{ }base.WndProc(ref m);}} 推荐答案 I你会想要在 OnPaint代码中处理panel1.autoscrollposition,找出网格左上角的起点。小心 ,当你向右移动面板并且向上移动面板时,该位置将是负面的。当你向左和向下滚动时。 我不确定你尝试用WndProc做什么,但是如果它用 键滚动,你可以通过使用表单OnKeyDown事件和 使用panel1.AutoScrollPosition = //新位置或+ / - =增量(小心 边界错误) - 使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/ I think you will want to handle the panel1.autoscrollposition in yourOnPaint code to find out where upper left of the grid will start. Bewarethat the position will be negative as you move the panel "to the right andupwards" when scrolling left and down. I''m not sure what you try to do with WndProc, but if it is scrolling withkeys, you can do that much easier by using the forms OnKeyDown event anduse panel1.AutoScrollPosition = // new position or +/-= increment (bewareof boundaries errors) --Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/ 感谢Morten Wennevik。 我是.net的初学者。 我重写OnKeyDown方法,但它不起作用。我的代码在这里: public int iX = 0,iY = 0; protected override void OnKeyDown(KeyEventArgs e) { iX = this.AutoScrollPosition.X; iY = this.AutoScrollPosition.Y; base.OnKeyDown(e); MessageBox.Show(iX.ToString()); } Thank Morten Wennevik.I''m a beginner for .net.I rewirte OnKeyDown method,but it doen''t work. My code goes here: public int iX = 0 ,iY = 0 ;protected override void OnKeyDown(KeyEventArgs e){iX = this.AutoScrollPosition.X;iY = this.AutoScrollPosition.Y;base.OnKeyDown(e);MessageBox.Show(iX.ToString());} 你想要完成什么? 如果你想要箭头键滚动你可以使用这样的东西 protected override void OnKeyDown(KeyEventArgs e) { // xStep和yStep是每次向左/向上滚动的金额 关键点击 int xStep = 10; int yStep = 10; //检查所有按下的键 // AutoScrollPosition实际为负数或0 ,但是当你设置 autoscrollposition //它必须是正数,所以我们设置// AutoScrollPosition = new Point( - AutoScrollPosition.X,-AutoScrollPosition。 Y); 开关(e.KeyCode) { case Keys.Right: AutoScrollPosition = new Point(-AutoScrollPosition.X + xStep, - AutoScrollPosition.Y); break; case Keys.Left: AutoScrollPosition = new Point(-AutoScrollPosition.X - xStep, - AutoScrollPosition.Y); break; case Keys .Up: AutoScrollPosition = new Point(-AutoScrollPosition.X, - AutoScrollPosition.Y - yStep); break; case Keys.Down: AutoScrollPosition = new Point(-AutoScrollPosition.X, - AutoScrollPosition.Y + yStep); 休息; 默认: 休息; } //您可以使用标签控件来显示当前的自动滚动。 //每次滚动时设置顶部和左侧位置,或者标签将移动 ,窗口为 label1。 Top = 0; label1.Left = 0; label1.Text = AutoScrollPosit ion.ToString(); } - 使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/ What are you trying to accomplish? If you want the arrow keys to scroll you can use something like this protected override void OnKeyDown(KeyEventArgs e){// xStep and yStep is the amount to scroll left-right/up-down each timethe key is hitint xStep = 10;int yStep = 10;// Check all keys pressed// The AutoScrollPosition is actually negative or 0, but when you set theautoscrollposition// it has to be positive, so we set // AutoScrollPosition = new Point(-AutoScrollPosition.X, -AutoScrollPosition.Y);switch(e.KeyCode){case Keys.Right:AutoScrollPosition = new Point(-AutoScrollPosition.X + xStep, -AutoScrollPosition.Y);break;case Keys.Left:AutoScrollPosition = new Point(-AutoScrollPosition.X - xStep, -AutoScrollPosition.Y);break;case Keys.Up:AutoScrollPosition = new Point(-AutoScrollPosition.X, -AutoScrollPosition.Y - yStep);break;case Keys.Down:AutoScrollPosition = new Point(-AutoScrollPosition.X, -AutoScrollPosition.Y + yStep);break;default:break;}// You can use a label control to show the current autoscrollposition.// Set top and left position each time you scroll or the label will movewith the windowlabel1.Top = 0;label1.Left = 0;label1.Text = AutoScrollPosition.ToString();}--Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/ 这篇关于GDI?¢的OnPaint?¢自动滚屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 23:03