如何实现像ACDSEE,PHOTOSHOP那样拖动图象时的平滑滚动? http://www.codeproject.com/cs/media/flickerFreeDrawing.asp 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.codeproject.com/cs/media/flickerFreeDrawing.asp 如下是我以前自己写的:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Threading;namespace MyAlbum{ /// <summary> /// Summary description for frmFullScreen. /// </summary> public class frmFullScreen : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private Bitmap bitCurrent=null; private Point ptCurrent; private Point ptStart; private Size sShowing; private Point ptMouseDown; private const int MOVESIZE=20; private Cursor curApp=null; private Cursor curImage=null; public frmFullScreen(ref Bitmap bitShow) { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // bitCurrent=bitShow; ptStart=new Point(0,0); sShowing=new Size(0,0); if(bitCurrent.Width<Screen.PrimaryScreen.Bounds.Width) { ptStart.X=(Screen.PrimaryScreen.Bounds.Width-bitCurrent.Width)/2; sShowing.Width=bitCurrent.Width; } else { ptStart.X=0; sShowing.Width=Screen.PrimaryScreen.Bounds.Width; } if(bitCurrent.Height<Screen.PrimaryScreen.Bounds.Height) { ptStart.Y=(Screen.PrimaryScreen.Bounds.Height-bitCurrent.Height)/2; sShowing.Height=bitCurrent.Height; } else { ptStart.Y=0; sShowing.Height=Screen.PrimaryScreen.Bounds.Height; } ptCurrent=new Point(0,0); ptMouseDown=new Point(0,0); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void frmFullScreen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.KeyCode==Keys.Escape) this.Close(); if(e.KeyCode==Keys.Up) { if(sShowing.Height<Screen.PrimaryScreen.Bounds.Height) return; else if(ptCurrent.Y==0) return; if((ptCurrent.Y-MOVESIZE)<0) ptCurrent.Y=0; else ptCurrent.Y=ptCurrent.Y-MOVESIZE; //Then redraw this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds)); } if(e.KeyCode==Keys.Down) { if(sShowing.Height<Screen.PrimaryScreen.Bounds.Height) return; else if((ptCurrent.Y+sShowing.Height)==bitCurrent.Height) return; if((ptCurrent.Y+MOVESIZE+sShowing.Height)<bitCurrent.Height) ptCurrent.Y+=MOVESIZE; else ptCurrent.Y=bitCurrent.Height-sShowing.Height; //Then redraw this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds)); } if(e.KeyCode==Keys.Left) { if(sShowing.Width<Screen.PrimaryScreen.Bounds.Width) return; else if(ptCurrent.X==0) return; if((ptCurrent.X-MOVESIZE)<0) ptCurrent.X=0; else ptCurrent.X-=MOVESIZE; //Then redraw this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds)); } if(e.KeyCode==Keys.Right) { if(sShowing.Width<Screen.PrimaryScreen.Bounds.Width) return; else if((ptCurrent.X+sShowing.Width)==bitCurrent.Width) return; if((ptCurrent.X+MOVESIZE+sShowing.Width)<bitCurrent.Width) ptCurrent.X+=MOVESIZE; else ptCurrent.X=bitCurrent.Width-sShowing.Width; //Then redraw this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds)); } } protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(bitCurrent, new Rectangle(ptStart,sShowing), new Rectangle(ptCurrent,sShowing), GraphicsUnit.Pixel); } private void frmFullScreen_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if((e.X>ptStart.X)&&(e.Y>ptStart.Y)&& (e.X<(ptStart.X+sShowing.Width))&&(e.Y<(ptStart.Y+sShowing.Height))) { this.Cursor=curImage; } else { this.Cursor=curApp; return; } if(e.Button==MouseButtons.Left) { bool blnXChanged=false; bool blnYChanged=false; if(e.Y>ptMouseDown.Y) { if((sShowing.Height>=Screen.PrimaryScreen.Bounds.Height) &&(ptCurrent.Y!=0)) { blnYChanged=true; if((ptCurrent.Y-(e.Y-ptMouseDown.Y))<0) ptCurrent.Y=0; else ptCurrent.Y-=(e.Y-ptMouseDown.Y); } } if(e.Y<ptMouseDown.Y) { if((sShowing.Height>=Screen.PrimaryScreen.Bounds.Height) &&((ptCurrent.Y+sShowing.Height)!=bitCurrent.Height)) { blnYChanged=true; if((ptCurrent.Y+(ptMouseDown.Y-e.Y)+sShowing.Height)<bitCurrent.Height) ptCurrent.Y+=(ptMouseDown.Y-e.Y); else ptCurrent.Y=bitCurrent.Height-sShowing.Height; } } if(e.X>ptMouseDown.X) { if((sShowing.Width>=Screen.PrimaryScreen.Bounds.Width) &&(ptCurrent.X!=0)) { blnXChanged=true; if((ptCurrent.X-(e.X-ptMouseDown.X))<0) ptCurrent.X=0; else ptCurrent.X-=(e.X-ptMouseDown.X); } } if(e.X<ptMouseDown.X) { if((sShowing.Width>=Screen.PrimaryScreen.Bounds.Width) &&((ptCurrent.X+sShowing.Width)!=bitCurrent.Width)) { if((ptCurrent.X+(ptMouseDown.X-e.X)+sShowing.Width)<bitCurrent.Width) ptCurrent.X+=(ptMouseDown.X-e.X); else ptCurrent.X=bitCurrent.Width-sShowing.Width; } } //Then redraw if(blnXChanged||blnYChanged) this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds)); ptMouseDown.X=e.X; ptMouseDown.Y=e.Y; //Thread.Sleep(50); } } private void frmFullScreen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button==MouseButtons.Left) { ptMouseDown.X=e.X; ptMouseDown.Y=e.Y; } } }} c# Winform用户控件如何在别的项目中使用 listview问题!! Viewstate 保存查询状态 如何获取当前窗口焦点所在的控件并进行操作 VS2008 WINDOWS应用程序的新建问题 [小梦涂鸦]--一个DataGrid的设计及实现全过程 咨询下 关于C#中的XML操作疑问? 我时WMI方面的新手,可是说刚刚接触,我需要这方面的书籍 C# frameset 在C#中如何设置(更改)系统时间? 介面設計?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;namespace MyAlbum
{
/// <summary>
/// Summary description for frmFullScreen.
/// </summary>
public class frmFullScreen : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; private Bitmap bitCurrent=null;
private Point ptCurrent;
private Point ptStart;
private Size sShowing;
private Point ptMouseDown; private const int MOVESIZE=20;
private Cursor curApp=null;
private Cursor curImage=null; public frmFullScreen(ref Bitmap bitShow)
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
bitCurrent=bitShow; ptStart=new Point(0,0);
sShowing=new Size(0,0);
if(bitCurrent.Width<Screen.PrimaryScreen.Bounds.Width)
{
ptStart.X=(Screen.PrimaryScreen.Bounds.Width-bitCurrent.Width)/2;
sShowing.Width=bitCurrent.Width;
}
else
{
ptStart.X=0;
sShowing.Width=Screen.PrimaryScreen.Bounds.Width;
} if(bitCurrent.Height<Screen.PrimaryScreen.Bounds.Height)
{
ptStart.Y=(Screen.PrimaryScreen.Bounds.Height-bitCurrent.Height)/2;
sShowing.Height=bitCurrent.Height;
}
else
{
ptStart.Y=0;
sShowing.Height=Screen.PrimaryScreen.Bounds.Height;
} ptCurrent=new Point(0,0);
ptMouseDown=new Point(0,0);
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} private void frmFullScreen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Escape)
this.Close(); if(e.KeyCode==Keys.Up)
{
if(sShowing.Height<Screen.PrimaryScreen.Bounds.Height)
return;
else if(ptCurrent.Y==0)
return;
if((ptCurrent.Y-MOVESIZE)<0)
ptCurrent.Y=0;
else
ptCurrent.Y=ptCurrent.Y-MOVESIZE; //Then redraw
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
} if(e.KeyCode==Keys.Down)
{
if(sShowing.Height<Screen.PrimaryScreen.Bounds.Height)
return;
else if((ptCurrent.Y+sShowing.Height)==bitCurrent.Height)
return; if((ptCurrent.Y+MOVESIZE+sShowing.Height)<bitCurrent.Height)
ptCurrent.Y+=MOVESIZE;
else
ptCurrent.Y=bitCurrent.Height-sShowing.Height; //Then redraw
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
} if(e.KeyCode==Keys.Left)
{
if(sShowing.Width<Screen.PrimaryScreen.Bounds.Width)
return;
else if(ptCurrent.X==0)
return;
if((ptCurrent.X-MOVESIZE)<0)
ptCurrent.X=0;
else
ptCurrent.X-=MOVESIZE; //Then redraw
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
} if(e.KeyCode==Keys.Right)
{
if(sShowing.Width<Screen.PrimaryScreen.Bounds.Width)
return;
else if((ptCurrent.X+sShowing.Width)==bitCurrent.Width)
return; if((ptCurrent.X+MOVESIZE+sShowing.Width)<bitCurrent.Width)
ptCurrent.X+=MOVESIZE;
else
ptCurrent.X=bitCurrent.Width-sShowing.Width; //Then redraw
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
}
} protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(bitCurrent,
new Rectangle(ptStart,sShowing),
new Rectangle(ptCurrent,sShowing),
GraphicsUnit.Pixel);
} private void frmFullScreen_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if((e.X>ptStart.X)&&(e.Y>ptStart.Y)&&
(e.X<(ptStart.X+sShowing.Width))&&(e.Y<(ptStart.Y+sShowing.Height)))
{
this.Cursor=curImage;
}
else
{
this.Cursor=curApp;
return;
} if(e.Button==MouseButtons.Left)
{
bool blnXChanged=false;
bool blnYChanged=false;
if(e.Y>ptMouseDown.Y)
{
if((sShowing.Height>=Screen.PrimaryScreen.Bounds.Height)
&&(ptCurrent.Y!=0))
{
blnYChanged=true;
if((ptCurrent.Y-(e.Y-ptMouseDown.Y))<0)
ptCurrent.Y=0;
else
ptCurrent.Y-=(e.Y-ptMouseDown.Y);
}
} if(e.Y<ptMouseDown.Y)
{
if((sShowing.Height>=Screen.PrimaryScreen.Bounds.Height)
&&((ptCurrent.Y+sShowing.Height)!=bitCurrent.Height))
{
blnYChanged=true;
if((ptCurrent.Y+(ptMouseDown.Y-e.Y)+sShowing.Height)<bitCurrent.Height)
ptCurrent.Y+=(ptMouseDown.Y-e.Y);
else
ptCurrent.Y=bitCurrent.Height-sShowing.Height;
}
} if(e.X>ptMouseDown.X)
{
if((sShowing.Width>=Screen.PrimaryScreen.Bounds.Width)
&&(ptCurrent.X!=0))
{
blnXChanged=true;
if((ptCurrent.X-(e.X-ptMouseDown.X))<0)
ptCurrent.X=0;
else
ptCurrent.X-=(e.X-ptMouseDown.X);
}
} if(e.X<ptMouseDown.X)
{
if((sShowing.Width>=Screen.PrimaryScreen.Bounds.Width)
&&((ptCurrent.X+sShowing.Width)!=bitCurrent.Width))
{
if((ptCurrent.X+(ptMouseDown.X-e.X)+sShowing.Width)<bitCurrent.Width)
ptCurrent.X+=(ptMouseDown.X-e.X);
else
ptCurrent.X=bitCurrent.Width-sShowing.Width;
}
} //Then redraw
if(blnXChanged||blnYChanged)
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.Bounds));
ptMouseDown.X=e.X;
ptMouseDown.Y=e.Y;
//Thread.Sleep(50);
}
} private void frmFullScreen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
ptMouseDown.X=e.X;
ptMouseDown.Y=e.Y;
}
}
}
}