Solved

TSpeedButton

Posted on 1998-09-22
9
546 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
Comment Utility
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
Comment Utility
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
Comment Utility
try it with TSpeedButton.layout:=[blGlyphRight] or [blGlyphLeft]

regards
rene
0
 
LVL 2

Expert Comment

by:333
Comment Utility
Can't you make glyph, that covers whole button?

A.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:Matvey
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Matvey, I don't use glyphs so I'm glad it's easy :-)

M.
0
 
LVL 3

Accepted Solution

by:
Matvey earned 50 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now