📄 renderers.cs
字号:
{
get { return !this.IsPrinting; }
}
/// <summary>
/// Cache whether or not our item is selected
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsItemSelected
{
get { return isItemSelected; }
set { isItemSelected = value; }
}
private bool isItemSelected;
/// <summary>
/// Is this renderer being used on a printer context?
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsPrinting
{
get { return isPrinting; }
set { isPrinting = value; }
}
private bool isPrinting;
/// <summary>
/// Get or set the listitem that this renderer will be drawing
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public OLVListItem ListItem
{
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Get/set the listview for which the drawing is to be done
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ObjectListView ListView
{
get { return objectListView; }
set { objectListView = value; }
}
private ObjectListView objectListView;
/// <summary>
/// Get the specialized OLVSubItem that this renderer is drawing
/// </summary>
/// <remarks>This returns null for column 0.</remarks>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public OLVListSubItem OLVSubItem
{
get { return listSubItem as OLVListSubItem; }
}
/// <summary>
/// Get or set the model object that this renderer should draw
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Object RowObject
{
get { return rowObject; }
set { rowObject = value; }
}
private Object rowObject;
/// <summary>
/// Get or set the list subitem that this renderer will be drawing
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ListViewItem.ListViewSubItem SubItem
{
get { return listSubItem; }
set { listSubItem = value; }
}
private ListViewItem.ListViewSubItem listSubItem;
/// <summary>
/// The brush that will be used to paint the text
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Brush TextBrush
{
get
{
if (textBrush == null)
return new SolidBrush(this.GetForegroundColor());
else
return this.textBrush;
}
set { textBrush = value; }
}
private Brush textBrush;
private void ClearState()
{
this.Event = null;
this.DrawItemEvent = null;
this.Aspect = null;
this.Font = null;
this.TextBrush = null;
}
#endregion
#region Utilities
/// <summary>
/// Align the second rectangle with the first rectangle,
/// according to the alignment of the column
/// </summary>
/// <param name="outer">The cell's bounds</param>
/// <param name="inner">The rectangle to be aligned within the bounds</param>
/// <returns>An aligned rectangle</returns>
protected virtual Rectangle AlignRectangle(Rectangle outer, Rectangle inner)
{
Rectangle r = new Rectangle(outer.Location, inner.Size);
// Centre horizontally depending on the column alignment
if (inner.Width < outer.Width) {
switch (this.Column.TextAlign) {
case HorizontalAlignment.Left:
r.X = outer.Left;
break;
case HorizontalAlignment.Center:
r.X = outer.Left + ((outer.Width - inner.Width) / 2);
break;
case HorizontalAlignment.Right:
r.X = outer.Right - inner.Width - 1;
break;
}
}
// Centre vertically too
if (inner.Height < outer.Height)
r.Y = outer.Top + ((outer.Height - inner.Height) / 2);
return r;
}
/// <summary>
/// Calculate the space that our rendering will occupy and then align that space
/// with the given rectangle, according to the Column alignment
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <returns></returns>
protected virtual Rectangle CalculateAlignedRectangle(Graphics g, Rectangle r)
{
if (this.Column.TextAlign == HorizontalAlignment.Left)
return r;
int width = this.CalculateCheckBoxWidth(g);
width += this.CalculateImageWidth(g, this.GetImageSelector());
width += this.CalculateTextWidth(g, this.GetText());
// If the combined width is greater than the whole cell,
// we just use the cell itself
if (width >= r.Width)
return r;
return this.AlignRectangle(r, new Rectangle(0, 0, width, r.Height));
}
/// <summary>
/// How much space will the check box for this cell occupy?
/// </summary>
/// <remarks>Only column 0 can have check boxes. Sub item checkboxes are
/// treated as images</remarks>
/// <param name="g"></param>
/// <returns></returns>
protected virtual int CalculateCheckBoxWidth(Graphics g)
{
if (this.ListView.CheckBoxes && this.Column.Index == 0)
return CheckBoxRenderer.GetGlyphSize(g, CheckBoxState.UncheckedNormal).Width + 6;
else
return 0;
}
/// <summary>
/// How much horizontal space will the image of this cell occupy?
/// </summary>
/// <param name="g"></param>
/// <param name="imageSelector"></param>
/// <returns></returns>
protected virtual int CalculateImageWidth(Graphics g, object imageSelector)
{
if (imageSelector == null || imageSelector == System.DBNull.Value)
return 0;
// Draw from the image list (most common case)
ImageList il = this.ListView.BaseSmallImageList;
if (il != null) {
int selectorAsInt = -1;
if (imageSelector is Int32)
selectorAsInt = (Int32)imageSelector;
else {
String selectorAsString = imageSelector as String;
if (selectorAsString != null)
selectorAsInt = il.Images.IndexOfKey(selectorAsString);
}
if (selectorAsInt >= 0)
return il.ImageSize.Width;
}
// Is the selector actually an image?
Image image = imageSelector as Image;
if (image != null)
return image.Width;
return 0;
}
/// <summary>
/// How much horizontal space will the text of this cell occupy?
/// </summary>
/// <param name="g"></param>
/// <param name="txt"></param>
/// <returns></returns>
protected virtual int CalculateTextWidth(Graphics g, string txt)
{
if (String.IsNullOrEmpty(txt))
return 0;
if (this.UseGdiTextRendering) {
Size proposedSize = new Size(int.MaxValue, int.MaxValue);
return TextRenderer.MeasureText(txt, this.Font, proposedSize, TextFormatFlags.EndEllipsis | TextFormatFlags.NoPrefix).Width;
} else {
StringFormat fmt = new StringFormat();
fmt.Trimming = StringTrimming.EllipsisCharacter;
return (int)g.MeasureString(txt, font, int.MaxValue, fmt).Width;
}
}
/// <summary>
/// Return the Color that is the background color for this item's cell
/// </summary>
/// <returns>The background color of the subitem</returns>
protected virtual Color GetBackgroundColor()
{
if (!this.ListView.Enabled)
return SystemColors.Control;
if (this.IsItemSelected && this.ListView.FullRowSelect) {
if (this.ListView.Focused)
return this.ListView.HighlightBackgroundColorOrDefault;
else
if (!this.ListView.HideSelection)
return SystemColors.Control; //TODO: What color should this be?
}
if (this.SubItem == null || this.ListItem.UseItemStyleForSubItems)
return this.ListItem.BackColor;
else
return this.SubItem.BackColor;
}
/// <summary>
/// Return the color to be used for text in this cell
/// </summary>
/// <returns>The text color of the subitem</returns>
protected virtual Color GetForegroundColor()
{
if (this.IsItemSelected && (this.Column.Index == 0 || this.ListView.FullRowSelect)) {
if (this.ListView.Focused)
return this.ListView.HighlightForegroundColorOrDefault;
else
if (!this.ListView.HideSelection)
return SystemColors.ControlText; //TODO: What color should this be?
}
if (this.SubItem == null || this.ListItem.UseItemStyleForSubItems)
return this.ListItem.ForeColor;
else
return this.SubItem.ForeColor;
}
/// <summary>
/// Return the image that should be drawn against this subitem
/// </summary>
/// <returns>An Image or null if no image should be drawn.</returns>
protected virtual Image GetImage()
{
return this.GetImage(this.GetImageSelector());
}
/// <summary>
/// Return the actual image that should be drawn when keyed by the given image selector.
/// An image selector can be: <list>
/// <item>an int, giving the index into the image list</item>
/// <item>a string, giving the image key into the image list</item>
/// <item>an Image, being the image itself</item>
/// </list>
/// </summary>
/// <param name="imageSelector">The value that indicates the image to be used</param>
/// <returns>An Image or null</returns>
protected virtual Image GetImage(Object imageSelector)
{
if (imageSelector == null || imageSelector == System.DBNull.Value)
return null;
ImageList il = this.ListView.BaseSmallImageList;
if (il != null) {
if (imageSelector is Int32) {
Int32 index = (Int32)imageSelector;
if (index < 0 || index >= il.Images.Count)
return null;
else
return il.Images[index];
}
String str = imageSelector as String;
if (str != null) {
if (il.Images.ContainsKey(str))
return il.Images[str];
else
return null;
}
}
return imageSelector as Image;
}
/// <summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -