[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

SendMessage question

Is there a way to send the same message to all objects on the same window?

ie: i am using SendMessage(<window>, WM_SETFONT, hFont, 0); to change the font on all buttons, static text objects, etc - but i need to use a SendMessage for each object.  Is there a way to tell all objects to adopt the same font in one call instead of using a call for each object?
0
GroundFloor
Asked:
GroundFloor
1 Solution
 
Slick812Commented:
I have never seen anything, API function or setting, to affect the hFont assigned to a window in any "Do Many at the Same Time" sort of way. . .
The windows system API uses the handle of a window to reference that specific window, I would guess that you will need to call the WM_SETFONT for each window.
0
 
Eddie ShipmanAll-around developerCommented:
You can write a custom message and handle it yourself.

const
  WM_MYSETFONT = WM_USER + $999;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure WMMySetFont(var Msg: TMessage);
      message WM_MYSETFONT;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostMessage(handle, WM_MYSETFONT, 0, 0);
end;

// hack to get the font property
type TMyControl = class(TControl);

procedure TForm1.WMMySetFont(var Msg: TMessage);
var
  i: Integer;
begin
  for i := 0 to Self.ControlCount-1 do
  begin
    TMyControl(Self.Controls[i]).Font.Name := 'Algerian';
  end;
end;


Now, as far as passing the font info in the message, I don't know how to do that.
0
 
Eddie ShipmanAll-around developerCommented:
I'm checking for an answer to pass the font info via the message.
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!

 
Eddie ShipmanAll-around developerCommented:
OK, here's an improved version that allows you to pass the font info, modify the TFontRec to include all the font properties
you want to pass.

unit Unit1;

interface

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

const
  WM_MYSETFONT = WM_USER + $999;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure WMMySetFont(var Msg: TMessage);
      message WM_MYSETFONT;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
type
  PFontRec = ^TFontRec;
  TFontRec = record
    fontSize: Integer;
    fontName: string[20];
  end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Rec: TFontRec;
begin
  Rec.fontName := 'Algerian';
  Rec.fontSize := 12;
  PostMessage(handle, WM_MYSETFONT, 0, Integer(@Rec));
end;

// hack to get the font property
type TMyControl = class(TControl);

procedure TForm1.WMMySetFont(var Msg: TMessage);
var
  i: Integer;
  PRec: PFontRec;
  F: TFont;
begin
  f := TFont.Create;
  try
    PRec := pointer(msg.LParam);
    f.Name := PRec.fontName;
    f.Size := Prec.fontSize;
    for i := 0 to Self.ControlCount-1 do
    begin
      TMyControl(Self.Controls[i]).Font.Assign(f);
    end;
  finally
    f.Free;
  end;
end;

end.
0
 
ZhaawZSoftware DeveloperCommented:
You can make a simple loop to send message to all child controls of some specific window. Here's an example:

var
  fnt : cardinal;
  wnd : cardinal;
........
fnt := CreateFont(-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, 'Courier New');
wnd := Form1.Handle; //handle of the main window
wnd := GetWindow(wnd, gw_child); //get first child control
repeat
  SendMessage(wnd, wm_setfont, fnt, 1);
  wnd := GetWindow(wnd, gw_hwndnext);
until wnd = 0; //loop until there are no child controls left

0
 
ricswikaCommented:
Checkout the ParentFont property; it may help.

Note, once you change the font property of a child control, ParentFont automatically gets set false.


From delphi 3 help.

property ParentFont: Boolean;

Description

To have a control use the same font as its parent control, set ParentFont to true If ParentFont is false the control uses its own Font property.

Set ParentFont to true for all controls in order to ensure that all the controls on a form have a uniform appearance. For example, if ParentFont is true for all controls in a form, changing the form’s Font property to 12-point Courier causes all controls on the form to use that font.

When the value of a control's Font property changes, ParentFont becomes false automatically.

When ParentFont is true for a form, the form uses the default font.

Rick
0
 
GroundFloorAuthor Commented:
worked exactly for what i needed
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now