Wyśrodkowanie tekstu w TEdit

0

Mam problem z przerobieniem kodu z C++ do wyśrodkowania tekstu w Edit.

VOID CenterEditText(HWND hEdit)
{
  LONG oldEditStyle;
  // Pobieramy styl edita
  oldEditStyle = GetWindowLong(hEdit, GWL_STYLE);
  // Ustawiamy nowy styl edita z wycentrowanym tekstem
  SetWindowLong(hEdit, GWL_STYLE, (LONG)(oldEditStyle | ES_CENTER));
}

Oczywiście nie działa...

procedure CenterEditText(Handle: THandle);
var
 oldEditStyle: integer;
begin
 // LONG oldEditStyle;
 // Pobieramy styl edita
 oldEditStyle := GetWindowLong(Handle, GWL_STYLE);
 // Ustawiamy nowy styl edita z wycentrowanym tekstem
 SetWindowLong(Handle, GWL_STYLE, ES_CENTER);
end;

procedure TForm4.Button1Click(Sender: TObject);
begin
 CenterEditText(Edit1.Handle);
end;

Proszę o pomoc

0

To mi działa:

procedure CenterEditText(Handle: THandle);
begin
SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle, GWL_STYLE) or ES_CENTER);
end;
0

ten kod niestety mi nie dziala

procedure CenterEditText(Handle: THandle);
begin
 SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle, GWL_STYLE) or ES_CENTER);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 CenterEditText(Edit1.Handle);
end;

kompiluje sie ale wcale tekst w edit1 nie jest na srodku

0

dziala wam ta metoda czy tylko mi nie dziala ?

0

Z tego co wyczytałem, to dla już "namalowanego" komponentu Edit, nie da się zastosować justowania. Po prostu funkcja SetWindowLong nie odmalowuje komponentu.

Wyjść jest kilka:

  • użycie Memo jako Edit
  • użycie RichEdit jako Edit
  • stworzenie komponentu obsługującego justowanie, na bazie TEdit:
{
OVERVIEW

This document demonstrates how to create a descendant of
TEdit that allows alignment of the text.

WHO SHOULD USE THIS DOCUMENT

Anyone with a basic familiarity with Delphi programming.
Applies to any version of Delphi.

CREATING JUSTAEDIT

One of the features the TMemo surfaces that a TEdit does
not is the ability to justify the text.  This functionality
exists via the Alignment property.  Great!  So why doesn't
a TEdit have this functionality, or perhaps a more pertinent
question is how do we add it?

This property, unfortunately can not be surfaced from an
ancestor, because the functionality does not exist in any
ancestor.  So it needs to be implemented via the window style
flags. ES_LEFT, ES_RIGHT, and ES_CENTER specify the text
alingment for edit controls. These flags do not have
any bearing on single line edit controls...unless running NT 4,
Service pack 3, and that is the reason the functionality
does not exist in the native control. This functionality
in now implemented in NT 4.0 but will have no effect in Win
3.1 and Windows 95.
}

unit JusEdit;

interface

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

type
  TJustaEdit = class(TEdit)
  private
    { Private declarations }
     fAlignment : TAlignment;
  protected
    { Protected declarations }
    procedure SetAlignment(Value: TAlignment);
  public
    { Public declarations }
     procedure createParams(var Params : TCreateParams); override;
  published
    { Published declarations }
     property Alignment: TAlignment read FAlignment write SetAlignment
      default taLeftJustify;
  end;

procedure Register;

implementation

procedure TJustaEdit.CreateParams(var Params : TCreateParams);
var
  x : Longint;
begin
  inherited CreateParams(Params);
  case fAlignment of
    tarightjustify: x := es_right;
    taleftjustify : x := es_left;
    tacenter      : x := es_center;
  end;
  params.style := params.style or x;
end;

procedure TJustaEdit.SetAlignment;
begin
   if FAlignment <> Value then
  begin
    FAlignment := Value;
    RecreateWnd;
  end;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TJustaEdit]);
end;

end.

1 użytkowników online, w tym zalogowanych: 0, gości: 1