Skip to content

Commit e143bb8

Browse files
committed
Improved drawing and added font cache
* Improved system like drawing of listboxes * Added a simple font cache to draw log messages like configured in the option dialog
1 parent 453020c commit e143bb8

18 files changed

+599
-430
lines changed

src/GuiLibrary/Controls/ListBoxEx.cs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
using System.Drawing.Drawing2D;
3636
using System.Drawing.Text;
3737
using System.Windows.Forms;
38+
using System.Windows.Forms.VisualStyles;
3839

3940
namespace Com.Couchcoding.GuiLibrary.Controls
4041
{
@@ -71,6 +72,19 @@ public override string ToString()
7172
#endregion
7273
}
7374

75+
/// <summary>
76+
/// Defines all known <see cref="ListView"/> states.
77+
/// </summary>
78+
private enum ListViewState
79+
{
80+
Normal = 1,
81+
Hot = 2,
82+
Selected = 3,
83+
Disabled = 4,
84+
SelectedAnNotFocused = 5,
85+
HotSelected = 6
86+
}
87+
7488
#endregion
7589

7690
#region Private Fields
@@ -185,15 +199,29 @@ private void InvalidateItem(int index)
185199
/// Handles the item drawing of a non selected item.
186200
/// </summary>
187201
/// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
188-
protected virtual void DrawItemHighlighted(DrawItemEventArgs e)
202+
/// <param name="selected">Determines wheter the item is currently selected.</param>
203+
protected virtual void DrawItemHighlighted(DrawItemEventArgs e, bool selected)
189204
{
205+
if (VisualStyleRenderer.IsSupported)
206+
{
207+
VisualStyleElement elementToDraw = selected
208+
? VisualStyleElement.CreateElement("Explorer::ListView", 1, (int)ListViewState.HotSelected)
209+
: VisualStyleElement.CreateElement("Explorer::ListView", 1, (int)ListViewState.Hot);
210+
211+
if (VisualStyleRenderer.IsElementDefined(elementToDraw))
212+
{
213+
new VisualStyleRenderer(elementToDraw).DrawBackground(e.Graphics, e.Bounds);
214+
return;
215+
}
216+
}
217+
190218
using (Pen backPen = new Pen(Color.FromArgb(25, SystemColors.Highlight)))
191219
{
192220
e.Graphics.FillRectangle(
193221
backPen.Brush
194222
, 0
195223
, e.Bounds.Top
196-
, e.Bounds.Width - 0
224+
, e.Bounds.Width - 0
197225
, e.Bounds.Height - 0);
198226

199227
using (Pen highlightPen = new Pen(Color.FromArgb(100, backPen.Color)))
@@ -202,7 +230,7 @@ protected virtual void DrawItemHighlighted(DrawItemEventArgs e)
202230
highlightPen
203231
, 0
204232
, e.Bounds.Top
205-
, e.Bounds.Width - 1
233+
, e.Bounds.Width - 1
206234
, e.Bounds.Height - 1);
207235
}
208236
}
@@ -212,15 +240,30 @@ protected virtual void DrawItemHighlighted(DrawItemEventArgs e)
212240
/// Handles the item drawing of a selected item.
213241
/// </summary>
214242
/// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
215-
protected virtual void DrawItemSelected(DrawItemEventArgs e)
243+
/// <param name="hover">Determines whether the cursor is currently hovering over the item.</param>
244+
protected virtual void DrawItemSelected(DrawItemEventArgs e, bool hover)
216245
{
246+
if (VisualStyleRenderer.IsSupported)
247+
{
248+
VisualStyleElement elementToDraw = hover
249+
? VisualStyleElement.CreateElement("Explorer::ListView", 1, (int)ListViewState.HotSelected)
250+
: VisualStyleElement.CreateElement("Explorer::ListView", 1, (int)ListViewState.Selected);
251+
252+
if (VisualStyleRenderer.IsElementDefined(elementToDraw))
253+
{
254+
255+
new VisualStyleRenderer(elementToDraw).DrawBackground(e.Graphics, e.Bounds);
256+
return;
257+
}
258+
}
259+
217260
using (Pen backPen = new Pen(Color.FromArgb(50, SystemColors.Highlight)))
218261
{
219262
e.Graphics.FillRectangle(
220263
backPen.Brush
221264
, 0
222265
, e.Bounds.Top
223-
, e.Bounds.Width - 0
266+
, e.Bounds.Width - 0
224267
, e.Bounds.Height - 0);
225268

226269
using (Pen highlightPen = new Pen(Color.FromArgb(200, backPen.Color)))
@@ -229,7 +272,7 @@ protected virtual void DrawItemSelected(DrawItemEventArgs e)
229272
highlightPen
230273
, 0
231274
, e.Bounds.Top
232-
, e.Bounds.Width - 1
275+
, e.Bounds.Width - 1
233276
, e.Bounds.Height - 1);
234277
}
235278
}
@@ -289,11 +332,11 @@ protected override void OnDrawItem(DrawItemEventArgs e)
289332
}
290333

291334
bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
292-
bool isHover = !isSelected && mMouseIndex != -1 && e.Index == mMouseIndex;
335+
bool isHover = mMouseIndex != -1 && e.Index == mMouseIndex;
293336

294337
if (isSelected)
295338
{
296-
DrawItemSelected(e);
339+
DrawItemSelected(e, isHover);
297340

298341
if (Focused && ShowFocusCues)
299342
{
@@ -307,7 +350,7 @@ protected override void OnDrawItem(DrawItemEventArgs e)
307350

308351
if (isHover)
309352
{
310-
DrawItemHighlighted(e);
353+
DrawItemHighlighted(e, isSelected);
311354
}
312355

313356
DrawItemText(e);
@@ -357,10 +400,10 @@ protected override void OnPaint(PaintEventArgs e)
357400
if (GetStyle(ControlStyles.UserPaint))
358401
{
359402
Message m = new Message();
360-
m.HWnd = Handle;
361-
m.Msg = WM_PRINTCLIENT;
362-
m.WParam = e.Graphics.GetHdc();
363-
m.LParam = (IntPtr)PRF_CLIENT;
403+
m.HWnd = Handle;
404+
m.Msg = WM_PRINTCLIENT;
405+
m.WParam = e.Graphics.GetHdc();
406+
m.LParam = (IntPtr)PRF_CLIENT;
364407

365408
DefWndProc(ref m);
366409
e.Graphics.ReleaseHdc(m.WParam);

src/GuiLibrary/GuiLibrary.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
<Compile Include="Interop\Win32.cs" />
104104
<Compile Include="Properties\AssemblyInfo.cs" />
105105
</ItemGroup>
106-
<ItemGroup />
106+
<ItemGroup>
107+
<Folder Include="Controls\Debug\" />
108+
</ItemGroup>
107109
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
108110
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
109111
Other similar extension points exist, see Microsoft.Common.targets.

src/GuiLibrary/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.3.2.0")]
35-
[assembly: AssemblyFileVersion("1.3.2.0")]
34+
[assembly: AssemblyVersion("1.3.3.0")]
35+
[assembly: AssemblyFileVersion("1.3.3.0")]

src/Logbert/Controls/CustomDetailsControl.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,13 @@ public bool ZoomIn()
406406
{
407407
this.SuspendDrawing();
408408

409-
tblLogMessage.Font = new Font(
410-
Font.FontFamily
411-
, tblLogMessage.Font.Size + 1);
409+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
410+
Font.Name
411+
, tblLogMessage.Font.Size + 1
412+
, FontStyle.Regular);
412413

413-
mBoldCaptionFont = new Font(
414-
Font.FontFamily
414+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
415+
Font.Name
415416
, mBoldCaptionFont.Size + 1
416417
, FontStyle.Bold);
417418

@@ -453,12 +454,13 @@ public bool ZoomOut()
453454
{
454455
this.SuspendDrawing();
455456

456-
tblLogMessage.Font = new Font(
457-
Font.FontFamily
458-
, tblLogMessage.Font.Size - 1);
457+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
458+
Font.Name
459+
, tblLogMessage.Font.Size - 1
460+
, FontStyle.Regular);
459461

460-
mBoldCaptionFont = new Font(
461-
Font.FontFamily
462+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
463+
Font.Name
462464
, mBoldCaptionFont.Size - 1
463465
, FontStyle.Bold);
464466

@@ -503,8 +505,9 @@ public CustomDetailsControl(Columnizer columnizer)
503505
ThemeManager.CurrentApplicationTheme.ApplyTo(logDetailToolStrip);
504506

505507
mColumnizer = columnizer;
506-
mBoldCaptionFont = new Font(
507-
Font
508+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
509+
Font.Name
510+
, Font.Size
508511
, FontStyle.Bold);
509512

510513
lblCaptionNumber.Font = mBoldCaptionFont;

src/Logbert/Controls/EventLogDetailsControl.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,13 @@ public bool ZoomIn()
387387
{
388388
this.SuspendDrawing();
389389

390-
tblLogMessage.Font = new Font(
391-
Font.FontFamily
392-
, tblLogMessage.Font.Size + 1);
390+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
391+
Font.Name
392+
, tblLogMessage.Font.Size + 1
393+
, FontStyle.Regular);
393394

394-
mBoldCaptionFont = new Font(
395-
Font.FontFamily
395+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
396+
Font.Name
396397
, mBoldCaptionFont.Size + 1
397398
, FontStyle.Bold);
398399

@@ -428,12 +429,13 @@ public bool ZoomOut()
428429
{
429430
this.SuspendDrawing();
430431

431-
tblLogMessage.Font = new Font(
432-
Font.FontFamily
433-
, tblLogMessage.Font.Size - 1);
432+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
433+
Font.Name
434+
, tblLogMessage.Font.Size - 1
435+
, FontStyle.Regular);
434436

435-
mBoldCaptionFont = new Font(
436-
Font.FontFamily
437+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
438+
Font.Name
437439
, mBoldCaptionFont.Size - 1
438440
, FontStyle.Bold);
439441

@@ -471,8 +473,9 @@ public EventLogDetailsControl()
471473
// Apply the current application theme to the control.
472474
ThemeManager.CurrentApplicationTheme.ApplyTo(logDetailToolStrip);
473475

474-
mBoldCaptionFont = new Font(
475-
Font
476+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
477+
Font.Name
478+
, Font.Size
476479
, FontStyle.Bold);
477480

478481
lblCaptionNumber.Font = mBoldCaptionFont;

src/Logbert/Controls/Log4NetDetailsControl.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,13 @@ public bool ZoomIn()
401401
{
402402
this.SuspendDrawing();
403403

404-
tblLogMessage.Font = new Font(
405-
Font.FontFamily
406-
, tblLogMessage.Font.Size + 1);
404+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
405+
Font.Name
406+
, tblLogMessage.Font.Size + 1
407+
, FontStyle.Regular);
407408

408-
mBoldCaptionFont = new Font(
409-
Font.FontFamily
409+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
410+
Font.Name
410411
, mBoldCaptionFont.Size + 1
411412
, FontStyle.Bold);
412413

@@ -441,12 +442,13 @@ public bool ZoomOut()
441442
{
442443
this.SuspendDrawing();
443444

444-
tblLogMessage.Font = new Font(
445-
Font.FontFamily
446-
, tblLogMessage.Font.Size - 1);
445+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
446+
Font.Name
447+
, tblLogMessage.Font.Size - 1
448+
, FontStyle.Regular);
447449

448-
mBoldCaptionFont = new Font(
449-
Font.FontFamily
450+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
451+
Font.Name
450452
, mBoldCaptionFont.Size - 1
451453
, FontStyle.Bold);
452454

@@ -483,8 +485,9 @@ public Log4NetDetailsControl()
483485
// Apply the current application theme to the control.
484486
ThemeManager.CurrentApplicationTheme.ApplyTo(logDetailToolStrip);
485487

486-
mBoldCaptionFont = new Font(
487-
Font
488+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
489+
Font.Name
490+
, Font.Size
488491
, FontStyle.Bold);
489492

490493
lblCaptionLogger.Font = mBoldCaptionFont;

src/Logbert/Controls/SyslogDetailsControl.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,13 @@ public bool ZoomIn()
372372
{
373373
this.SuspendDrawing();
374374

375-
tblLogMessage.Font = new Font(
376-
Font.FontFamily
377-
, tblLogMessage.Font.Size + 1);
375+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
376+
Font.Name
377+
, tblLogMessage.Font.Size + 1
378+
, FontStyle.Regular);
378379

379-
mBoldCaptionFont = new Font(
380-
Font.FontFamily
380+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
381+
Font.Name
381382
, mBoldCaptionFont.Size + 1
382383
, FontStyle.Bold);
383384

@@ -412,12 +413,13 @@ public bool ZoomOut()
412413
{
413414
this.SuspendDrawing();
414415

415-
tblLogMessage.Font = new Font(
416-
Font.FontFamily
417-
, tblLogMessage.Font.Size - 1);
416+
tblLogMessage.Font = FontCache.GetFontFromIdentifier(
417+
Font.Name
418+
, tblLogMessage.Font.Size - 1
419+
, FontStyle.Regular);
418420

419-
mBoldCaptionFont = new Font(
420-
Font.FontFamily
421+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
422+
Font.Name
421423
, mBoldCaptionFont.Size - 1
422424
, FontStyle.Bold);
423425

@@ -454,8 +456,9 @@ public SyslogDetailsControl()
454456
// Apply the current application theme to the control.
455457
ThemeManager.CurrentApplicationTheme.ApplyTo(logDetailToolStrip);
456458

457-
mBoldCaptionFont = new Font(
458-
Font
459+
mBoldCaptionFont = FontCache.GetFontFromIdentifier(
460+
Font.Name
461+
, Font.Size
459462
, FontStyle.Bold);
460463

461464
lblCaptionNumber.Font = mBoldCaptionFont;

0 commit comments

Comments
 (0)