精彩控件件源码(3)-ImageListPopup, a C# class which pops up a window to select an image from an image list
2008-12-18 13:28
736 查看
http://www.codeproject.com/KB/selection/imagelistpopup.aspx
Download demo project - 49 Kb
Custom colors for the grid, background color and selection color
Adjustable horizontal and vertical spaces between images
Add on the top of your source code the directive using CustomUIControls;
add a member variable in your class:
Collapse
Copy Code
In your constructor add the following lines:
Collapse
Copy Code
Collapse
Copy Code
These properties allow us to customize the different colors of the ImageList Popup (see the picture below for details):
The properties must be set before calling Init(), otherwise they won't be taken into account.
Collapse
Copy Code
This property allows to Enable Drag'n'Drop support (default is false). You can drag a bitmap from the popup to any Drop target. A Bitmap object and a string containing the Id of the dropped image in the imagelist are available to the target.
Collapse
Copy Code
This line sets the image list, the horizontal and vertical pixel spaces between 2 images, and the rows and columns number (must be present).
Collapse
Copy Code
This is the Event that is fired when an image is selected. The delegate should look like this:
Collapse
Copy Code
When you want to show the ImageListPopup, you just have to call:
Collapse
Copy Code
Collapse
Copy Code
In the PaintBackground method, we blit the previously generated bitmap onto a hidden bitmap, then we draw the selection rectangle, and finally we blit everything to the screen with a basic double buffering technique.
Collapse
Copy Code
25/02/2003 : Added Keyboard support, Added Drag'n'Drop Support (disabled by default) - The selected Image and its Id are available to the drop target
Download demo project - 49 Kb
Introduction
The ImageListPopup is a quite easy to use Class that inherits fromSystem.Windows.Forms.Formand allows to show a popup displaying the content of a specified Image List. When you click over an image, an event is fired and you get the index of the clicked image in the imagelist.
Features
The ImageListPopup class supports:Custom colors for the grid, background color and selection color
Adjustable horizontal and vertical spaces between images
Compatibility
This class is stand alone and doesn't need any particular Libraries except .NET default ones. It runs in managed code and hence should be portable.How to use the Class
First of all, copy ImageListPopup.cs in your project directory.Add on the top of your source code the directive using CustomUIControls;
add a member variable in your class:
Collapse
Copy Code
ImageListPopup imageListPopup;
In your constructor add the following lines:
Collapse
Copy Code
imageListPopup = new ImageListPopup(); // facultative properties imageListPopup.BackgroundColor = Color.FromArgb(241,241,241); imageListPopup.BackgroundOverColor = Color.FromArgb(102,154,204); imageListPopup.HLinesColor = Color.FromArgb(182,189,210); imageListPopup.VLinesColor = Color.FromArgb(182,189,210); imageListPopup.BorderColor = Color.FromArgb(0,0,0); imageListPopup.EnableDragDrop = true; imageListPopup.Init(imageList,8,8,5,4); imageListPopup.ItemClick += new ImageListPopupEventHandler(OnItemClicked);
Details
Collapse
Copy Code
imageListPopup.BackgroundColor = Color.FromArgb(241,241,241); imageListPopup.BackgroundOverColor = Color.FromArgb(102,154,204); imageListPopup.HLinesColor = Color.FromArgb(182,189,210); imageListPopup.VLinesColor = Color.FromArgb(182,189,210); imageListPopup.BorderColor = Color.FromArgb(0,0,0);
These properties allow us to customize the different colors of the ImageList Popup (see the picture below for details):
The properties must be set before calling Init(), otherwise they won't be taken into account.
Collapse
Copy Code
imageListPopup.EnableDragDrop = true;
This property allows to Enable Drag'n'Drop support (default is false). You can drag a bitmap from the popup to any Drop target. A Bitmap object and a string containing the Id of the dropped image in the imagelist are available to the target.
Collapse
Copy Code
imageListPopup.Init(imageList,8,8,5,4);
This line sets the image list, the horizontal and vertical pixel spaces between 2 images, and the rows and columns number (must be present).
Collapse
Copy Code
imageListPopup.ItemClick += new ImageListPopupEventHandler(OnItemClicked);
This is the Event that is fired when an image is selected. The delegate should look like this:
Collapse
Copy Code
private void OnItemClicked(object sender, ImageListPopupEventArgs e) { label1.Text = "Selected Image: " + e.SelectedItem; }
When you want to show the ImageListPopup, you just have to call:
Collapse
Copy Code
imageListPopup.Show(x,y);
Technical issues
When Init is called, a pre-calculated bitmap is generated with all the static drawing (the background Color, the grid, and all the images)Collapse
Copy Code
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; }
In the PaintBackground method, we blit the previously generated bitmap onto a hidden bitmap, then we draw the selection rectangle, and finally we blit everything to the screen with a basic double buffering technique.
Collapse
Copy Code
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); }
Conclusion
I hope this code will be useful to you. If you have suggestions to enhance this class functionalities, please post a comment.Updates
24/02/2003 : Initial Release25/02/2003 : Added Keyboard support, Added Drag'n'Drop Support (disabled by default) - The selected Image and its Id are available to the drop target
License
相关文章推荐
- Multi select elements which sums upto a certain number from an array(II)
- Multi select elements which sums upto a certain number from an array(II)
- Multi select elements which sums upto a certain number from an array
- Multi select elements which sums upto a certain number from an array
- To get the tree list of the objects on which an object depends
- Q2.1 Write code to remove duplicates from an unsorted linked list.
- C#调用函数传参数IEnumerable<ClassA> classlist.ToList(),不会改变classlist
- An association from the table emps refers to an unmapped class: Dept 错误解析
- (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
- [GDI+] C# ImageClass帮助类教程与源码下载 (转载)
- Converting an RGB image to 1 bit-per-pixel monochrome(源码)
- 关于运行期异常:Failed to instantiate [java.util.List]: Specified class is an interface
- How to Pop Open an image window that resizes to fit the image in ASP.net 1.x and 2.0 Beta 1
- An association from the table refers to an unmapped class
- SpringMvc报错解决:Failed to instantiate [java.util.List]: Specified class is an interface
- Serialization from an Object to XML Document in C#
- C# WINFROM中threeview中imagelist图片有时候不显示透明的
- Cannot refer to a non-final variable image inside an inner class 关于android 的final
- 关于运行期异常:Failed to instantiate [java.util.List]: Specified class is an interface
- Android中出现:Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 5 columns异常