Thema: Delphi TEdit mit Rahmen

Einzelnen Beitrag anzeigen

zahor

Registriert seit: 27. Jun 2006
Ort: im hintersten Winkel des RAMs
182 Beiträge
 
Delphi 2007 Professional
 
#8

Re: TEdit mit Rahmen

  Alt 26. Mär 2008, 22:15
um das ganze gleich wieder weiterzuentwickeln:
die farben können jetzt im objektinspektor eingestellt werden,
ausserdem werden jetzt abgerundete rechtecke verwendet.
ich hängs's auch gleich mal dran.
was mich im moment noch stört, ist wie das ganze zur entwicklungszeit auf dem formular aussieht (beispiel siehe anhang)
Delphi-Quellcode:
unit SEdit;

interface

uses
  Windows, SysUtils, Classes, Controls, StdCtrls, Messages, Graphics;

type
  TSEdit = class(TEdit)
  private
    Fh: TWMNCPAINT;
    Fdc: hDc;
    FPen: hPen;
    FOldPen: hPen;
    FOldBrush: hBrush;
    FSelected: boolean;
    rcolorfocused, rcolornotfocused: tcolor;
    procedure WMNCCalcSize(var Msg: TWMNCCALCSIZE); message WM_NCCALCSIZE;
    procedure WMNCPAINT(var Msg: TWMNCPaint); message WM_NCPAINT;
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
    procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
    procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
    procedure RedrawBorder;
  protected
    { Protected-Deklarationen }
  public
    procedure RePaint; override;
    constructor Create(AOwner: TComponent); override;
  published
    property ColorFocused: TColor read rColorFocused write rcolorFocused;
    property ColorNotFocused: TColor read rcolornotfocused write rColorNotFocused;
  end;

procedure Register;


implementation

procedure Register;
begin
  RegisterComponents('Zusätzlich', [TSEdit]);
end;

constructor TSedit.Create(AOwner: TComponent);
begin
  inherited;
  ParentFont := True;
  FSelected := False;
end;

procedure TSEdit.RePaint;
begin
  WMNCPAINT(Fh);
end;

procedure TSedit.WMNCCalcSize(var Msg: TWMNCCalcSize);
begin
  inherited;
  InflateRect(Msg.CalcSize_Params^.rgrc[0], -1, -1);
end;

procedure TSEdit.WMNCPAINT(var Msg: TWMNCPAINT);
begin
  inherited;
  RedrawBorder;
end;


procedure TSedit.WMSetFocus(var Message: TWMSetFocus);
begin
  inherited;
  if not (csDesigning in ComponentState) then
  begin
    FSelected := True;
    RedrawBorder;
  end;
end;

procedure TSedit.WMKillFocus(var Message: TWMKillFocus);
begin
  inherited;
  if not (csDesigning in ComponentState) then
  begin
    FSelected := False;
    RedrawBorder;
  end;
end;

procedure TSedit.CMMouseEnter(var Message: TMessage);
begin
  inherited;
  if (GetActiveWindow <> 0) then
  begin
    RedrawBorder();
  end;
end;

procedure TSedit.CMMouseLeave(var Message: TMessage);
begin
  inherited;
  RedrawBorder();
end;

procedure TSEdit.RedrawBorder;
begin
  inherited;
  if FSelected then
  begin
    Fdc := GetWindowDC(Handle);
    //Change the RGB value to change the color
    FPen := CreatePen(PS_SOLID, 1, rColorFocused);
    FOldPen := SelectObject(Fdc, FPen);
    FOldBrush := SelectObject(Fdc, GetStockObject(NULL_BRUSH));
    RoundRect(Fdc, 0, 0, Width, Height, 3, 3);
    SelectObject(Fdc, FOldBrush);
    SelectObject(Fdc, FOldPen);
    DeleteObject(FPen);
    ReleaseDC(Handle, Handle);
  end else
  begin
    Fdc := GetWindowDC(Handle);
  //Change the RGB value to change the color
    FPen := CreatePen(PS_SOLID, 1, rColorNotFocused);
    FOldPen := SelectObject(Fdc, FPen);
    FOldBrush := SelectObject(Fdc, GetStockObject(NULL_BRUSH));
    RoundRect(Fdc, 0, 0, Width, Height, 3, 3);
    SelectObject(Fdc, FOldBrush);
    SelectObject(Fdc, FOldPen);
    DeleteObject(FPen);
    ReleaseDC(Handle, Handle);
  end;
end;


end.
Miniaturansicht angehängter Grafiken
w_h_148.png  
Angehängte Dateien
Dateityp: pas sedit_126.pas (3,2 KB, 31x aufgerufen)
Real Programmers always confuse Christmas and Halloween because Oct31 = Dec25. - Andrew Rutherford
  Mit Zitat antworten Zitat