File size: 11,458 Bytes
e26fba6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using BrightIdeasSoftware;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using System.Diagnostics;
namespace MovieFinder.Client
{
/// <summary>
/// Hackish renderer that draw a fancy version of a person for a Tile view.
/// </summary>
/// <remarks>This is not the way to write a professional level renderer.
/// It is hideously inefficient (we should at least cache the images),
/// but it is obvious</remarks>
internal class BusinessCardRenderer : AbstractRenderer
{
#region Fields
private static Queue<Movie> imagedUrls = new Queue<Movie>();
private static Thread workerThread = new Thread(ImageDowloadWorker);
private static volatile bool stopThread;
#endregion
#region Constructor
static BusinessCardRenderer()
{
if (!Directory.Exists(Movie.ImgDirectory))
Directory.CreateDirectory(Movie.ImgDirectory);
workerThread.IsBackground = true;
workerThread.Start();
}
#endregion
#region Methods
public override bool RenderItem(DrawListViewItemEventArgs e, Graphics g, Rectangle itemBounds, object rowObject)
{
// If we're in any other view than Tile, return false to say that we haven't done
// the rendereing and the default process should do it's stuff
ObjectListView olv = e.Item.ListView as ObjectListView;
if (olv == null || olv.View != View.Tile)
return false;
// Use buffered graphics to kill flickers
BufferedGraphics buffered = BufferedGraphicsManager.Current.Allocate(g, itemBounds);
g = buffered.Graphics;
g.Clear(olv.BackColor);
g.SmoothingMode = ObjectListView.SmoothingMode;
g.TextRenderingHint = ObjectListView.TextRenderingHint;
if (e.Item.Selected)
{
this.BorderPen = Pens.Blue;
this.HeaderBackBrush = new SolidBrush(olv.HighlightBackgroundColorOrDefault);
}
else
{
this.BorderPen = new Pen(Color.FromArgb(0x33, 0x33, 0x33));
this.HeaderBackBrush = new SolidBrush(Color.FromArgb(0x33, 0x33, 0x33));
}
DrawBusinessCard(g, itemBounds, rowObject, olv, (OLVListItem)e.Item);
// Finally render the buffered graphics
buffered.Render();
buffered.Dispose();
// Return true to say that we've handled the drawing
return true;
}
internal Pen BorderPen = new Pen(Color.FromArgb(0x33, 0x33, 0x33));
internal Brush TextBrush = new SolidBrush(Color.FromArgb(0x22, 0x22, 0x22));
internal Brush HeaderTextBrush = Brushes.AliceBlue;
internal Brush HeaderBackBrush = new SolidBrush(Color.FromArgb(0x33, 0x33, 0x33));
internal Brush BackBrush = Brushes.LemonChiffon;
public void DrawBusinessCard(Graphics g, Rectangle itemBounds, object rowObject, ObjectListView olv, OLVListItem item)
{
try
{
const int spacing = 8;
// Allow a border around the card
itemBounds.Inflate(-2, -2);
// Draw card background
const int rounding = 20;
GraphicsPath path = this.GetRoundedRect(itemBounds, rounding);
g.FillPath(this.BackBrush, path);
g.DrawPath(this.BorderPen, path);
g.Clip = new Region(itemBounds);
// Draw the photo
Rectangle photoRect = itemBounds;
photoRect.Inflate(-spacing, -spacing);
Movie movie = rowObject as Movie;
if (movie != null)
{
photoRect.Width = 200;
if (!File.Exists(movie.ImagePath))
DownloadImage(movie);
if (IsFileExists(movie.ImagePath))
{
try
{
using (var photo = Image.FromFile(movie.ImagePath))
{
if (photo.Width > photoRect.Width)
photoRect.Height = (int)(photo.Height * ((float)photoRect.Width / photo.Width));
else
photoRect.Height = photo.Height;
g.DrawImage(photo, photoRect);
}
}
catch
{
g.DrawRectangle(Pens.DarkGray, photoRect);
File.Delete(movie.ImagePath);
}
}
else
{
g.DrawRectangle(Pens.DarkGray, photoRect);
}
}
// Now draw the text portion
RectangleF textBoxRect = photoRect;
textBoxRect.X += (photoRect.Width + spacing);
textBoxRect.Width = itemBounds.Right - textBoxRect.X - spacing;
using (StringFormat fmt = new StringFormat())
{
fmt.Trimming = StringTrimming.EllipsisCharacter;
fmt.Alignment = StringAlignment.Center;
fmt.LineAlignment = StringAlignment.Near;
String txt = item.Text;
using (Font font = new Font("Tahoma", 11))
{
// Measure the height of the title
SizeF size = g.MeasureString(txt, font, (int)textBoxRect.Width, fmt);
// Draw the title
RectangleF r3 = textBoxRect;
r3.Height = size.Height;
path = this.GetRoundedRect(r3, 15);
g.FillPath(this.HeaderBackBrush, path);
g.DrawString(txt, font, this.HeaderTextBrush, textBoxRect, fmt);
textBoxRect.Y += size.Height + spacing;
}
// Draw the other bits of information
using (Font font = new Font("Tahoma", 8))
{
for (int i = 0; i < olv.Columns.Count; i++)
{
OLVColumn column = olv.GetColumn(i);
if (column.IsTileViewColumn)
{
SizeF size = g.MeasureString(column.GetStringValue(movie), font, (int)textBoxRect.Width, fmt);
textBoxRect.Height = size.Height;
fmt.Alignment = StringAlignment.Near;
txt = column.GetStringValue(rowObject);
g.DrawString(txt, font, this.TextBrush, textBoxRect, fmt);
textBoxRect.Y += size.Height ;
}
}
}
}
}
catch (Exception ex) { Debug.WriteLine("This exception: " + ex.Message); }
}
private GraphicsPath GetRoundedRect(RectangleF rect, float diameter)
{
GraphicsPath path = new GraphicsPath();
RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
private bool IsFileExists(string file)
{
try
{
if (File.Exists(file))
{
using (var currentWriteableFile = File.OpenWrite(file)) { }
}
}
catch { return false; }
return true;
}
private void DownloadImage(Movie movie)
{
if (String.IsNullOrEmpty(movie.ImagePath))
return;
if (!File.Exists(movie.ImagePath))
{
lock (imagedUrls)
{
imagedUrls.Enqueue(movie);
}
}
}
private static void ImageDowloadWorker()
{
while (!stopThread)
{
try
{
Movie movie = null;
lock (imagedUrls)
{
if (imagedUrls.Count > 0)
movie = imagedUrls.Dequeue();
else
Thread.Sleep(100);
}
if (movie != null && !String.IsNullOrEmpty(movie.ImagePath) && !File.Exists(movie.ImagePath))
{
using (var client = new WebClient())
{
try
{
client.DownloadFile(movie.ImageUrl, movie.ImagePath);
}
catch { }
}
}
}
catch { }
}
}
public static void StopThread()
{
stopThread = true;
workerThread.Join(5000);
}
#endregion
}
/// <summary>
/// This simple class just shows how an overlay can be drawn when the hot item changes.
/// </summary>
internal class BusinessCardOverlay : AbstractOverlay
{
public BusinessCardOverlay()
{
businessCardRenderer.HeaderBackBrush = Brushes.DarkBlue;
businessCardRenderer.BorderPen = new Pen(Color.DarkBlue, 2);
this.Transparency = 255;
}
#region IOverlay Members
public override void Draw(ObjectListView olv, Graphics g, Rectangle r)
{
if (olv.HotRowIndex < 0)
return;
if (olv.View == View.Tile)
return;
OLVListItem item = olv.GetItem(olv.HotRowIndex);
if (item == null)
return;
Size cardSize = new Size(400, 250);
Rectangle cardBounds = new Rectangle(
r.Right - cardSize.Width - 8, r.Bottom - cardSize.Height - 20, cardSize.Width, cardSize.Height);
businessCardRenderer.DrawBusinessCard(g, cardBounds, item.RowObject, olv, item);
}
#endregion
private readonly BusinessCardRenderer businessCardRenderer = new BusinessCardRenderer();
}
}
|