Auto centre dialog caption?

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
newuser
Pro Scripter
Posts: 64
Joined: Tue Jun 11, 2013 4:53 pm

Auto centre dialog caption?

Post by newuser » Fri Apr 18, 2014 6:27 pm

I'm tired of manually typing space into a dialog caption :( like the example below, anyone have auto centre code for dialog caption? :?:

Code: Select all

//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1
//Simple dialog block
Dialog>Dialog1
object Dialog1: TForm
  Caption = '                                             Modal Dialog'
  object MSButton1: tMSButton
    Width = 75
    Height = 25
    Caption = 'Close'
  end
end
EndDialog>Dialog1

//Dialog handler defining what to do when the button is clicked
AddDialogHandler>Dialog1,msButton1,OnClick,CloseDialog1

//Display modal dialog
//When the subroutine ends the script returns here.
Show>dialog1,res1

//Picking "X" to close the dialog bypasses the modal Show>
MessageModal>Dialog1 was closed by clicking "X"

//Subroutine to execute when the button is clicked
SRT>CloseDialog1
  //Close the dialog
  CloseDialog>Dialog1
  MessageModal>Dialog1 was closed by clicking the button
  //Reopen the dialog
  Show>dialog1
END>CloseDialog1

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Apr 18, 2014 6:37 pm

There's no such thing I'm afraid. Window captions are left aligned. When you see apps that place them in the centre there is a lot of low level UI redrawing going on. There's no simple property for it. For Macro Scheduler dialogs, it just isn't possible.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Apr 18, 2014 8:18 pm

I just couldn't let this go without trying to come up with something. Here is a sample that puts the dialog caption in the center of the dialog and keeps it centered if the dialog is resized. You might have to tweak the first five variables. On my XP computer Let>vPixelsInASpace=3 works good. On my Windows 7 computer I have Let>vPixelsInASpace=3.4. For some reason using a modal dialog fails.

Main caveat is that the caption can't exceed 254 characters so if the dialog gets too wide it will lose its center. But it is just a visual affect anyway so unless you get a ruler out and measure the screen you might not even notice.

Code: Select all

Let>vFont=Arial
Let>vSize=12
Let>vStyle=1
Let>vColor=0
Let>vPixelsInASpace=3

Dialog>TextSizeCatureDialog
object TextSizeCatureDialog: TForm
  AutoSize = True
  AlphaBlend = True
  AlphaBlendValue = 0
  BorderStyle = bsNone
  object Label1: TLabel
    Left = 0
    Top = 0
  end
end
EndDialog>TextSizeCatureDialog

Dialog>Dialog1
object Dialog1: TForm
  Left = 490
  Top = 205
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'A Caption to be Centered in the Dialog'
  ClientHeight = 223
  ClientWidth = 484
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnResize,ReCenterCaption
AddDialogHandler>Dialog1,,OnClose,Quit
GoSub>ReCenterCaption

Show>dialog1
//Show>dialog1,res1

Label>Loop
  Wait>0.01
Goto>Loop

SRT>ReCenterCaption
  GetDialogProperty>Dialog1,,ClientWidth,vDiaWide
  GetDialogProperty>Dialog1,,Caption,vDiaCap
  Trim>vDiaCap,vDiaCap
  Let>vText=vDiaCap
  SetDialogProperty>TextSizeCatureDialog,label1,Caption,vText
  SetDialogObjectFont>TextSizeCatureDialog,label1,vFont,vSize,vStyle,vColor
  Let>WIN_USEHANDLE=1
  Show>TextSizeCatureDialog
  GetWindowSize>TextSizeCatureDialog.handle,TextSizeX,TextSizeY
  Let>WIN_USEHANDLE=0
  Let>SpaceLength={round((%vDiaWide%/2)-(%TextSizeX%/2))}
  Let>CaptionKK=0
  Repeat>CaptionKK
    Add>Captionkk,1
    Let>vDiaCap=%space%%vDiaCap%
    Length>vDiaCap,caplen
    //Apparently a caption is limited to 254 characters.
    If>caplen>253
      Let>captionkk=99999999999
    EndIf
  Until>CaptionKK>{%SpaceLength%/%vPixelsInASpace%}
  Length>vDiaCap,caplen
  SetDialogProperty>Dialog1,,Caption,vDiaCap
END>ReCenterCaption

SRT>Quit
  Exit>0
END>Quit

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts