实例介绍
【实例简介】
【实例截图】
【核心代码】
// ImageListPopup by John O'Byrne // This class pops up window displaying the different images // contained in an ImageList and allows to select one. // History: 24/02/2003 : Initial Release // 25/02/2003 : Added Keyboard support (arrows space or enter to validate) // Added Drag'n'Drop Support (disabled by default) - The selected Image // and its Id are available to the drop target using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace CustomUIControls { public class ImageListPopupEventArgs : EventArgs { public int SelectedItem; public ImageListPopupEventArgs(int selectedItem) { SelectedItem = selectedItem; } } public delegate void ImageListPopupEventHandler(object sender, ImageListPopupEventArgs ilpea); public class ImageListPopup : System.Windows.Forms.Form { #region Protected Member Variables protected Bitmap _Bitmap = null; protected ImageList _imageList = null; protected int _nBitmapWidth = 0; protected int _nBitmapHeight = 0; protected int _nItemWidth = 0; protected int _nItemHeight = 0; protected int _nRows = 0; protected int _nColumns = 0; protected int _nHSpace = 0; protected int _nVSpace = 0; protected int _nCoordX = -1; protected int _nCoordY = -1; protected bool _bIsMouseDown = false; #endregion #region Public Properties public Color BackgroundColor = Color.FromArgb(255,255,255); public Color BackgroundOverColor = Color.FromArgb(241,238,231); public Color HLinesColor = Color.FromArgb(222,222,222); public Color VLinesColor = Color.FromArgb(165,182,222); public Color BorderColor = Color.FromArgb(0,16,123); public bool EnableDragDrop = false; #endregion #region Events public event ImageListPopupEventHandler ItemClick = null; #endregion #region Constructor public ImageListPopup() { // Window Style FormBorderStyle = FormBorderStyle.None; WindowState = FormWindowState.Minimized; base.Show(); base.Hide(); WindowState = FormWindowState.Normal; ShowInTaskbar = false; TopMost = true; MaximizeBox = false; MinimizeBox = false; ControlBox = false; } #endregion #region Public Methods public bool Init(ImageList imageList, int nHSpace, int nVSpace, int nColumns, int nRows) { _imageList = imageList; _nColumns = nColumns; _nRows = nRows; _nHSpace = nHSpace; _nVSpace = nVSpace; _nItemWidth = _imageList.ImageSize.Width nHSpace; _nItemHeight = _imageList.ImageSize.Height nVSpace; _nBitmapWidth = _nColumns * _nItemWidth 1; _nBitmapHeight = _nRows * _nItemHeight 1; this.Width = _nBitmapWidth; this.Height = _nBitmapHeight; _Bitmap = new Bitmap(_nBitmapWidth,_nBitmapHeight); Graphics grfx = Graphics.FromImage(_Bitmap); grfx.FillRectangle(new SolidBrush(BackgroundColor), 0, 0, _nBitmapWidth, _nBitmapHeight); for (int i=0;i<_nColumns;i ) grfx.DrawLine(new Pen(VLinesColor), i*_nItemWidth, 0, i*_nItemWidth, _nBitmapHeight-1); for (int i=0;i<_nRows;i ) grfx.DrawLine(new Pen(HLinesColor), 0, i*_nItemHeight, _nBitmapWidth-1, i*_nItemHeight); grfx.DrawRectangle(new Pen(BorderColor), 0 ,0 , _nBitmapWidth-1, _nBitmapHeight-1); for (int i=0;i<_nColumns;i ) for (int j=0;j<_nRows ;j ) if ((j*_nColumns i) < imageList.Images.Count) imageList.Draw(grfx, i*_nItemWidth _nHSpace/2, j*_nItemHeight nVSpace/2, imageList.ImageSize.Width, imageList.ImageSize.Height, j*_nColumns i); return true; } public void Show(int x, int y) { this.Left = x; this.Top = y; base.Show(); } #endregion #region Overrides protected override void OnMouseLeave(EventArgs ea) { // We repaint the popup if the mouse is no more over it base.OnMouseLeave(ea); _nCoordX = -1; _nCoordY = -1; Invalidate(); } protected override void OnDeactivate(EventArgs ea) { // If the form loses focus, we hide it this.Hide(); } protected override void OnKeyDown(KeyEventArgs kea) { if (_nCoordX==-1 || _nCoordY==-1) { _nCoordX = 0; _nCoordY = 0; Invalidate(); } else { switch(kea.KeyCode) { case Keys.Down: if (_nCoordY<_nRows-1) { _nCoordY ; Invalidate(); } break; case Keys.Up: if (_nCoordY>0) { _nCoordY--; Invalidate(); } break; case Keys.Right: if (_nCoordX<_nColumns-1) { _nCoordX ; Invalidate(); } break; case Keys.Left: if (_nCoordX>0) { _nCoordX--; Invalidate(); } break; case Keys.Enter: case Keys.Space: // We fire the event only when the mouse is released int nImageId = _nCoordY*_nColumns _nCoordX; if (ItemClick != null && nImageId>=0 && nImageId<_imageList.Images.Count) { ItemClick(this, new ImageListPopupEventArgs(nImageId)); _nCoordX = -1; _nCoordY = -1; Hide(); } break; case Keys.Escape: _nCoordX = -1; _nCoordY = -1; Hide(); break; } } } protected override void OnMouseMove(MouseEventArgs mea) { // Update the popup only if the image selection has changed if (ClientRectangle.Contains(new Point(mea.X,mea.Y))) { if (EnableDragDrop && _bIsMouseDown) { int nImage = _nCoordY*_nColumns _nCoordX; DataObject data = new DataObject(); data.SetData(DataFormats.Text,nImage.ToString()); data.SetData(DataFormats.Bitmap,_imageList.Images[nImage]); DragDropEffects dde = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Move); _bIsMouseDown = false; } if ( ((mea.X/_nItemWidth)!=_nCoordX) || ((mea.Y/_nItemHeight)!=_nCoordY) ) { _nCoordX = mea.X/_nItemWidth; _nCoordY = mea.Y/_nItemHeight; Invalidate(); } } else { _nCoordX = -1; _nCoordY = -1; Invalidate(); } base.OnMouseMove(mea); } protected override void OnMouseDown(MouseEventArgs mea) { base.OnMouseDown(mea); _bIsMouseDown = true; Invalidate(); } protected override void OnMouseUp(MouseEventArgs mea) { base.OnMouseDown(mea); _bIsMouseDown = false; // We fire the event only when the mouse is released int nImageId = _nCoordY*_nColumns _nCoordX; if (ItemClick != null && nImageId>=0 && nImageId<_imageList.Images.Count) { ItemClick(this, new ImageListPopupEventArgs(nImageId)); Hide(); } } protected override void OnPaintBackground(PaintEventArgs pea) { Graphics grfx = pea.Graphics; grfx.PageUnit = GraphicsUnit.Pixel; // Basic double buffering technique Bitmap offscreenBitmap = new Bitmap(_nBitmapWidth, _nBitmapHeight); Graphics offscreenGrfx = Graphics.FromImage(offscreenBitmap); // We blit the precalculated bitmap on the offscreen Graphics offscreenGrfx.DrawImage(_Bitmap, 0, 0); if (_nCoordX!=-1 && _nCoordY!=-1 && (_nCoordY*_nColumns _nCoordX)<_imageList.Images.Count) { // We draw the selection rectangle offscreenGrfx.FillRectangle(new SolidBrush(BackgroundOverColor), _nCoordX*_nItemWidth 1, _nCoordY*_nItemHeight 1, _nItemWidth-1, _nItemHeight-1); if (_bIsMouseDown) { // Mouse Down aspect for the image _imageList.Draw(offscreenGrfx, _nCoordX*_nItemWidth _nHSpace/2 1, _nCoordY*_nItemHeight _nVSpace/2 1, _imageList.ImageSize.Width, _imageList.ImageSize.Height, _nCoordY*_nColumns _nCoordX); } else { // Normal aspect for the image _imageList.Draw(offscreenGrfx, _nCoordX*_nItemWidth _nHSpace/2, _nCoordY*_nItemHeight _nVSpace/2, _imageList.ImageSize.Width, _imageList.ImageSize.Height, _nCoordY*_nColumns _nCoordX); } // Border selection Rectangle offscreenGrfx.DrawRectangle(new Pen(BorderColor), _nCoordX*_nItemWidth, _nCoordY*_nItemHeight, _nItemWidth, _nItemHeight); } // We blit the offscreen image on the screen grfx.DrawImage(offscreenBitmap, 0, 0); } #endregion } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)