Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TSpeedButton

Posted on 1998-09-22
9
Medium Priority
?
586 Views
Last Modified: 2010-05-18
Is it possible to align text in TSpeedButton (.Caption) to the most left or right?
0
Comment
Question by:gljsentvid16
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:333
ID: 1340624
Hi,
I'm not sure, that it's what you need, but...

Try to change TSpeedButton.Layout to blGlyphLeft or blGlyphRight and set TSpeedButton.Margin to i.e 1. If TSpeedButton.Margin=-1 (default), Text and Glyph are centered horizontaly and verticaly. When you change Margin to 1, then Text and Glyph are aligned depending on TSpeedButton.Layout.

A.
0
 

Author Comment

by:gljsentvid16
ID: 1340625
Hi!

 Nope, this just specifies, how the glyph and text are oriented. What I would like is to set the caption of the button to the most left or right corner of the button (like in Text editors such as MSWord where you can set align).

M.
0
 
LVL 2

Expert Comment

by:rene100
ID: 1340626
try it with TSpeedButton.layout:=[blGlyphRight] or [blGlyphLeft]

regards
rene
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:333
ID: 1340627
Can't you make glyph, that covers whole button?

A.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1340628
Basically, you could play with it, like putting a button without text, and putting a label on it. SpeedButton has no defined way to do exactly what you want, though it's not hard to change it's sources at all. Also if you use the normal TButton instead, I bet you have a way to do this.
0
 

Author Comment

by:gljsentvid16
ID: 1340629
Hi!

  .layout[] will not do. If I wanted to create glyph, I would have to create .bmp image (from .caption), and glue it on - naah, too much to do. Putting label on it covers OnClick event, so this one won't do either. And yes, I need to use TSpeedButton, cos TButton is not .Flat and .Down . If you know how to change sources please, tell me :-).

   M.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1340630
OK, I'll look and try to change them. Do you use bitmaps with your buttons? Because it's VERY simple if you don't use glyphs, otherwise it'll be harder.

Menwhile, you can try: put a label on the button, and in the label's OnMouseDown, OnMouseUp, OnMouseMove, send the same messages to the button beneath. Not MouseDown, MouseUp methods, but messages with SendMesages: WM_LBUTTONDOWN, WM_LBUTTONUP.

--Matvey
0
 

Author Comment

by:gljsentvid16
ID: 1340631
Matvey, I don't use glyphs so I'm glad it's easy :-)

M.
0
 
LVL 3

Accepted Solution

by:
Matvey earned 200 total points
ID: 1340632
Here we go. This works fine unless you want to use glyphs.

save this in a unit, and install as a component.
Have fun!
--Matvey
___________________________________________________________
unit btn_AL;

interface

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

type
  TSpeedButton_AL = class(TSpeedButton)
  private
    FCaption: string;
    FAlignment: TAlignment;
  protected
    procedure Paint; override;
    procedure SetCaption(Value: string);
    procedure SetAlignment(Value: TAlignment);
  published
    property Caption: string read FCaption write SetCaption;
    property Alignment: TAlignment read FAlignment write SetAlignment;
  end;

procedure Register;

implementation

var
  Pattern: TBitmap = nil;

procedure CreateBrushPattern;
var
  X, Y: Integer;
begin
  Pattern := TBitmap.Create;
  Pattern.Width := 8;
  Pattern.Height := 8;
  with Pattern.Canvas do
  begin
    Brush.Style := bsSolid;
    Brush.Color := clBtnFace;
    FillRect(Rect(0, 0, Pattern.Width, Pattern.Height));
    for Y := 0 to 7 do
      for X := 0 to 7 do
        if (Y mod 2) = (X mod 2) then  { toggles between even/odd pixles }
          Pixels[X, Y] := clBtnHighlight;     { on even/odd rows }
  end;
end;

procedure TSpeedButton_AL.SetCaption(Value: string);
begin
  FCaption := Value;
  Paint;
end;

procedure TSpeedButton_AL.SetAlignment(Value: TAlignment);
begin
  FAlignment := Value;
  Paint;
end;

procedure TSpeedButton_AL.Paint;
const
  TextAlignments: array[TAlignment] of Integer =
         (DT_LEFT, DT_RIGHT, DT_CENTER);
var
  rText: TRect;
  P: TPoint;
begin
  inherited Paint;
  rText := Rect(1, 1, Width-1, Height-1);
  with Canvas do
  begin
    GetCursorPos(P);
    if (FState = bsExclusive) and
       (not Flat or not (FindDragTarget(P, True) = Self)) then begin
      if Pattern = nil then CreateBrushPattern;
      Canvas.Brush.Bitmap := Pattern;
    end
    else begin
      Brush.Style := bsSolid;
      Brush.Color := clBtnFace;
    end;
    FillRect(rText);

    Font := Self.Font;
    Brush.Style := bsClear;
    if (FState = bsDown) or (FState = bsExclusive) then begin
      inc(rText.Top);
      inc(rText.Left);
    end;
    if FState = bsDisabled then
    begin
      OffsetRect(rText, 1, 1);
      Font.Color := clBtnHighlight;
      DrawText(Handle, PChar(Caption), Length(Caption), rText,
             TextAlignments[Alignment] or DT_VCENTER or DT_SINGLELINE);
      OffsetRect(rText, -1, -1);
      Font.Color := clBtnShadow;
      DrawText(Handle, PChar(Caption), Length(Caption), rText,
             TextAlignments[Alignment] or DT_VCENTER or DT_SINGLELINE);
    end else
      DrawText(Handle, PChar(Caption), Length(Caption), rText,
             TextAlignments[Alignment] or DT_VCENTER or DT_SINGLELINE);
  end;

end;

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

end.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question