Solved

TSpeedButton

Posted on 1998-09-22
9
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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