본문 바로가기

카테고리 없음

[일반/컴포넌트] 표준 TListBox 에 Radio 버튼 올리기

반응형
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure GetAllWindowsProc(WinHandle: HWND; Slist: TStrings);
var
  P: array[0..256] of Char; {title bar를 저장 할 buffer}
begin
  P[0] := #0;
  GetWindowText(WinHandle, P, 255); {window's title bar를 알아낸다}
  if (P[0] <> #0) then
    if IsWindowVisible(WinHandle) then {invisible한 window는 제외}
      Slist.AddObject(P, TObject(WinHandle)); {window의 handle 저장}
end;

procedure GetAllWindows(Slist: TStrings);
var
  WinHandle: HWND;
Begin
  WinHandle := FindWindow(nil, nil);
  GetAllWindowsProc(WinHandle, Slist);
  while (WinHandle <> 0) do  {Top level의 window부터 순차적으로 handle을 구한다}
  begin
    WinHandle := GetWindow(WinHandle, GW_HWNDNEXT);
    GetAllWindowsProc(WinHandle, Slist);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.Items.Clear;
  GetAllWindows(ListBox1.Items);

  ListBox1.Style := lbOwnerDrawVariable;
  ListBox1.ItemHeight := 20;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  drawRect: TRect;
begin
  with ListBox1.Canvas do
  begin
    FillRect(rect);
    drawRect.Left := rect.Left + 1;
    drawRect.Right := Rect.Left + 13;
    drawRect.Bottom := Rect.Bottom;
    drawRect.Top := Rect.Top;
    if odSelected in State then
      DrawFrameControl(Handle, drawRect, DFC_BUTTON, DFCS_BUTTONRADIO or DFCS_CHECKED)
    else
      DrawFrameControl(Handle, drawRect, DFC_BUTTON, DFCS_BUTTONRADIO);
    TextOut(15, rect.Top + 3, ListBox1.Items[Index]);
  end;
end;

procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  // ListBox1.ItemIndex := ListBox1.ItemAtPos(Point(x, y), True);
end;

end.
반응형