October 12, 2008

Tips 12

0 comments
Listbox with colored lines

Colored lines in listbox are more user-friendly and easy to read. Here is how to do it…
First of all, place ListBox component on a form and set Style property to lbOwnerDrawFixed. The rest is simple:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawFixed;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
with Control as TListBox do
begin
Canvas.FillRect(Rect);
Canvas.Font.Color := TColor(Items.Objects[Index]);
Canvas.TextOut(Rect.Left + 2, Rect.Top, Items[Index]);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.AddObject('Red line', Pointer(clRed));
ListBox1.Items.AddObject('Green line', Pointer(clGreen));
end;

end.

October 11, 2008

Tips 11

0 comments
Change color of ListView items

To change background color of ListView items, you need to write your own OnCustomDrawItem event handler.
The best result is when you set the ViewStyle to vsReport, but it’s completely functional even with vsList style. OnCustomDrawItem code is very simple:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;

type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
with ListView1.Canvas.Brush do
begin
case Item.Index of
0: Color := clYellow;
1: Color := clGreen;
2: Color := clRed;
end;
end;
end;

end.

October 10, 2008

Tips 10

0 comments
Change font, size and style of hint

Hints are everywhere. Almost every GUI element in Windows can be “hinted”. When user hover mouse over element, small yellow bubble with help text pops up. Is it possible to change hint “window” behavior? Of course…

To see how to change default color and timeouts of hint bubble, visit this post. To change font, size and style of the hint text, just use this code:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
TExHint = class(THintWindow)
constructor Create(AOwner: TComponent); override;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor TExHint.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
with Canvas.Font do
begin
Name := 'Verdana';
Size := Size + 15;
Style := [fsBold, fsItalic];
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
HintWindowClass := TExHint;
end;

end.

October 09, 2008

Tips 9

0 comments
Get Process Memory Info

If you want to know how many bytes of memory is using your process, here is simple example.
We will need standard psAPI unit. Using API function GetProcessMemoryInfo, we can get amount of used bytes of memory.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, psAPI;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
pmc: PPROCESS_MEMORY_COUNTERS;
cb: Integer;
begin
cb := SizeOf(_PROCESS_MEMORY_COUNTERS);
GetMem(pmc, cb);
pmc^.cb := cb;
if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then ShowMessage(IntToStr(pmc^.WorkingSetSize) + ' Bytes')
else ShowMessage('Unable to get process info');
FreeMem(pmc);
end;

end.

October 08, 2008

Tips 8

0 comments
How to detect system time change

If the user changes the system time either with Date and Time properties dialog box or using command line, we can detect this by catching WM_TIMECHANGE system message. Here is how.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
private
{ Private declarations }
procedure WMTimeChange(var Msg: TMessage); message WM_TIMECHANGE;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMTimeChange(var Msg: TMessage);
begin
ShowMessage('System time was just changed!');
end;

end.
 

Copyright 2008 All Rights Reserved | Blogger Template by Computer Science and Computer Tips