📄 pimitemlistview.cs
字号:
this.ClientSize = new System.Drawing.Size(newWidth, newHeight);
//Refresh form
ResetGeometry();
this.Invalidate();
}
#endregion
#region Mouse Event Handler
/// <summary>
/// Capture the click event
/// </summary>
/// <param name="sender"> Sender </param>
/// <param name="e"> Information about the click</param>
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//Calculation to find out which pimItem was clicked
selectedCell = e.Y / cellHeight;
//Do highlighting if the pimItem is on display
if (selectedCell < pimItemsShowing)
{
UpdateDisplay();
PimItemListViewEventArgs args = new PimItemListViewEventArgs((PimItem)pimItemsCollection[scrollValue + selectedCell]);
PimItemSelected(sender, args);
}
}
#endregion
#region Paint Event Handler
/// <summary>
/// This paints the PimItemListView control
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="eventArgs">Event arguments</param>
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs eventArgs)
{
UpdateDisplay();
}
#endregion
#region Private Methods - Display PimItem Information
/// <summary>
/// Draw picture if requested and if picture exists
/// </summary>
/// <param name="picture">Picture to be drawn</param>
/// <param name="pimItemNo">The pimItem index indicating where the
/// picture should be drawn</param>
private void DrawPicture(System.Drawing.Image picture, int pimItemNo)
{
if (showPicture && picture != null)
{
//Rectangle where the picture should be drawn.
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(
this.ClientSize.Width - (vScrollBar1.Width + pictureBorderWidth + pictureWidth),
cellHeight * pimItemNo + pictureBorderWidth + 1 ,
pictureWidth,
pictureHeight);
//Select the full picture from the picture file
System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle(0, 0, picture.Size.Width, picture.Size.Height);
backGraphics.DrawImage(picture, destRect, srcRect, System.Drawing.GraphicsUnit.Pixel);
}
}
/// <summary>
/// Displays one cell in the PimItemViewer
/// </summary>
/// <param name="cellNumber">Number of cell to display.</param>
/// <param name="firstLine">String to display in the first label.</param>
/// <param name="lastLine">String to display in the second label.</param>
private void DisplayCell(int cellNumber, String firstLine, String lastLine)
{
int y1 = cellNumber * cellHeight;
int y2 = cellNumber * cellHeight + cellHeightDiv2;
int width = this.ClientSize.Width - vScrollBar1.Width - borderWidth;
int height = cellHeightDiv2 - borderWidth;
if (selectedCell != cellNumber)
{
backGraphics.DrawString(firstLine,line1Font, textBrush, new RectangleF(borderWidth, y1, width, height));
backGraphics.DrawString(lastLine, line2Font, textBrush, new RectangleF(borderWidth, y2, width, height));
}
else
{
backGraphics.DrawString(firstLine, line1Font, hightlightTextBrush, new RectangleF(borderWidth, y1, width, height));
backGraphics.DrawString(lastLine, line2Font, hightlightTextBrush, new RectangleF(borderWidth, y2, width, height));
}
}
#endregion
#region Protected Properties and Methods
/// <summary>
/// The collection of items to display in the ListView.
/// </summary>
protected PimItemCollection PimItems
{
set
{
pimItemsCollection = value;
ResetGeometry();
}
}
/// <summary>
/// Update the pimItem information on the pimItem viewer
/// </summary>
protected void UpdateDisplay()
{
scrollValue = vScrollBar1.Value;
int offset = scrollValue;
String firstLine;
String secondLine;
PimItem pimItem;
bool highlighted;
object pimProperty1;
object pimProperty2;
if (pimItemsShowing == 0)
{
formGraphics.FillRectangle(backgroundBrush, 0, 0,
this.Size.Width, this.Size.Height);
formGraphics.DrawString("There are no items in the collection", line1Font, textBrush, 0, 0);
return;
}
// Paint background
backGraphics.FillRectangle(backgroundBrush, 0, 0,
this.Size.Width, this.Size.Height);
// Highlight selected item.
backGraphics.FillRectangle(highlightBackgroundBrush, 0, selectedCell * cellHeight,
this.Size.Width - vScrollBar1.Width, cellHeight);
//Set the information on the labels according to the value of the scrollbar
for (int i = 0; i < pimItemsShowing && pimItemsCollection != null && offset < pimItemsCollection.Count; i++, offset++)
{
pimItem = pimItemsCollection[offset];
pimProperty1 = pimItem.Properties[property1];
pimProperty2 = pimItem.Properties[property2];
if (pimProperty1 != null)
{
firstLine = pimProperty1.ToString();
}
else
{
firstLine = "";
}
if (pimProperty2 != null)
{
secondLine = pimProperty2.ToString();
}
else
{
secondLine = "";
}
highlighted = (selectedCell == i);
DisplayCell(i, firstLine, secondLine);
if (showPicture)
{
DrawPicture(((Contact)pimItemsCollection[i + scrollValue]).Picture, i);
}
}
int yStart;
int xEnd = this.ClientSize.Width - vScrollBar1.Width;
//Draw the lines separating the pimItems
if (pimItemsShowing != 0)
{
for (int i = 0; i <= pimItemsShowing; i++)
{
yStart = cellHeight * i;
//Draw Line. yStart is the same as yEnd.
backGraphics.DrawLine(pen, 0, yStart, xEnd, yStart);
}
}
// Copy the backBuffer to the front buffer. Using the backbuffer
// makes changes in the display smoother.
formGraphics.DrawImage(backBuffer, 0, 0);
}
#endregion
#region Public Properties
/// <summary>
/// The font for the first line in a cell.
/// </summary>
public System.Drawing.Font Property1Font
{
get
{
return line1Font;
}
set
{
line1Font = value;
}
}
/// <summary>
/// The font for the second line in a cell.
/// </summary>
public System.Drawing.Font Property2Font
{
get
{
return line2Font;
}
set
{
line2Font = value;
}
}
/// <summary>
/// The height of one of the cells in pixels.
/// </summary>
public int CellHeight
{
get
{
return cellHeight;
}
set
{
cellHeight = value;
ResetGeometry();
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Disposes the PimItemViewer object.
/// </summary>
void IDisposable.Dispose()
{
if (!disposed)
{
disposed = true;
backgroundBrush.Dispose();
backgroundBrush.Dispose();
highlightBackgroundBrush.Dispose();
textBrush.Dispose();
hightlightTextBrush.Dispose();
pen.Dispose();
line1Font.Dispose();
line2Font.Dispose();
formGraphics.Dispose();
backGraphics.Dispose();
backBuffer.Dispose();
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Finalizer.
/// Dispose should be called by the parent object. We dispose just
/// in case it does not.
/// </summary>
~PimItemListView()
{
Dispose();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -