Jump to content

about GUI title


 Share

Recommended Posts

How to make equal to the distance between two words in the subject line?

I present here is what I tried to do but still the second word is not exactly the same place:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <winapi.au3>

$guiwid = 600
$Title = "Abc" ; when there's 'Abcsadsadsaghfgfgh' then 'deF' is not in a good position now... ;[
$Title2 = "deF"

$hG = GUICreate("", $guiwid, 300, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION))

$click = GUICtrlCreateButton("click",15,15,100,20)
GUISetState(@SW_SHOW)
SetTitle($hG, $Title, $Title2)


While 1
    $msg = GUIGetMsg()
    If $msg = $click Then SetTitle($hG, 'Abcsadsadsaghfgfgh', $Title2)
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Func SetTitle($hWnd, $sTitle, $sTitle2)
    Local $wp = WinGetPos($hWnd)
    $hDC = _WinAPI_GetDC($hWnd)

    Do
        $sTitle = $sTitle &" "
        $size = _WinAPI_GetTextExtentPoint32($hDC, $sTitle)
    Until DllStructGetData($size, 1) > $wp[2] - 110
    $sTitle = $sTitle&$Title2
    WinSetTitle($hWnd, "", $sTitle)
EndFunc ;==>SetTitle

Pls help if You can. Zwinny.

Link to comment
Share on other sites

There have been lots of issues trying to get _WinAPI_GetTextExtentPoint32() to work. It's buggy and non-trivial. One step I don't see you taking is getting a handle to a font and setting it for the DC you attempt to get the text size from. Here's an example from trancexx.

You might want to look into Melba23's StringSize.au3 UDF.

;)

Edit: Added links.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

ZwinnyRolnik,

Seems to work all right when you use StringSize: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

#include <StringSize.au3>

$guiwid = 600
$Title = "Abc"
$Title2 = "deF"

$hG = GUICreate("", $guiwid, 300, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION))

$click = GUICtrlCreateButton("click",15,15,100,20)

GUICtrlCreateLabel("", 405, 0, 10, 10) ; Just to show the second part of the title does not move <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GUICtrlSetBkColor(-1, 0xFF0000)

GUISetState(@SW_SHOW)SetTitle($hG, $Title, $Title2)

While 1
    $msg = GUIGetMsg()
    If $msg = $click Then SetTitle($hG, 'Abcsadsadsaghfgfgh', $Title2)
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Func SetTitle($hWnd, $sTitle, $sTitle2)
    Local $wp = WinGetPos($hWnd)
    Do
        $sTitle = $sTitle &" "
        $aSize = _StringSize($sTitle)
    Until $aSize[2] > $wp[2] - 210 ; You will have to modify this number to get the right place <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $sTitle = $sTitle & $Title2
    WinSetTitle($hWnd, "", $sTitle)
EndFunc ;==>SetTitle

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Seems to work all right when you use StringSize: ;)

M23

@Melba2, I'm not convinced!

It looks to me like the _StringSize function doesn't give the width of the string correctly.

Try changing the width of the window in the modified example below. - It doesn't work for me.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

#include <StringSize.au3>

$guiwid = 600
$Title = "Abc"
$Title2 = "deF"

$hG = GUICreate("", $guiwid, 300, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_SIZEBOX))

$click = GUICtrlCreateButton("click", 15, 15, 100, 20)

GUICtrlCreateLabel("", 405, 0, 10, 10) ; Just to show the second part of the title does not move <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GUICtrlSetResizing(-1,$GUI_DOCKWIDTH + $GUI_DOCKRIGHT)
GUICtrlSetBkColor(-1, 0xFF0000)

GUISetState(@SW_SHOW)
SetTitle($hG, $Title, $Title2)
GUIRegisterMsg(0x0005, "retitle");$WM_SIZE

While 1
    $msg = GUIGetMsg()
    If $msg = $click Then
        $Title = 'Abcsadsadsaghfgfgh'
        SetTitle($hG, $Title, $Title2)
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Func SetTitle($hWnd, $sTitle, $sTitle2)
    Local $wp = WinGetPos($hWnd)
    Local $RealTitle
    Do
        $sTitle = $sTitle & " "
        $RealTitle = $sTitle & $sTitle2
        $aSize = _StringSize($RealTitle);this actually made no difference of course but should be done in case $sTitle2 changes
    Until $aSize[2] > $wp[2] - 250 ; 

    WinSetTitle($hWnd, "", $RealTitle)
EndFunc ;==>SetTitle


Func retitle($Hw, $iM, $wp, $lp)
    SetTitle($hG, $Title, $Title2)
EndFunc ;==>retitle

Probably because you need to pass the Window caption font details which can be obtained from SystemParameterInfo(SPI_GETNONCLIENTMETRICS)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

martin,

I'm not convinced!

I am! ;)

The label is purely there to show the second part of the title has not moved. If I change the label creation line in the code you posted to read:

GUICtrlCreateLabel("", 375, 0, 10, 10) ; Note the change in X coordinate

everything lines up as before when I run it initially and after the title is changed. I hope it does for you too. ;)

I did warn the OP that he would have to play with the numbers in the function to get the alignment as it was before. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

martin,

I am! ;)

The label is purely there to show the second part of the title has not moved. If I change the label creation line in the code you posted to read:

GUICtrlCreateLabel("", 375, 0, 10, 10) ; Note the change in X coordinate

everything lines up as before when I run it initially and after the title is changed. I hope it does for you too. ;)

I did warn the OP that he would have to play with the numbers in the function to get the alignment as it was before. :)

M23

No, I still am not convinced. If my modified example works for you then you must have a different font for your window titles than I have. I changed the label so that it is kept at a fixed distance from the right side of the window. When I change the width of the window the title moves from left of the label when the window is narrow, to off the right edge of the window when the window is wide but it should stay level with the label. I think it is probably because your function has not been passed the information on the font for the window title. Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

To confirm what I thought, if I send the font name and size to the function it works for me.

$aSize = _StringSize($RealTitle,10,400,0,"Trebuchet MS");<- now works

So to set the title to be a certain width in a window you have to first get the font details for window titles.

Here's some code from a post by wraithdu. The 20th element of the structure is the font name, but I don't see how to get the font size. maybe it is from the caption height so

$fontname = _GetSysFontInfo(20) 
$fontsize = Ceiling(Abs(_GetSysFontInfo(7) *72/96); (height/pixelsperinch)*72pointperinch ?
;although you would have to get the correct value for pixels per inch which might not be 96

Func _GetSysFontInfo($sElem)
    Local Const $LF_FACESIZE = 32
    ; 14 elements
    Local Const $tagLOGFONTW = "long lfHeight;long lfWidth;long lfEscapement;long lfOrientation;long lfWeight;byte lfItalic;byte lfUnderline;byte lfStrikeOut;" _
    & "byte lfCharSet;byte lfOutPrecision;byte lfClipPrecision;byte lfQuality;byte lfPitchAndFamily;wchar lfFaceName[" & $LF_FACESIZE & "]"
    ; font structs in order: lfCaptionFont, lfSmCaptionFont, lfMenuFont, lfStatusFont, lfMessageFont
    Local Const $tagNONCLIENTMETRICS = "uint cbSize;int iBorderWidth;int iScrollWidth;int iScrollHeight;int iCaptionWidth;int iCaptionHeight;" _
    & _SuffixStruct($tagLOGFONTW, 1) & ";" & "int iSmCaptionWidth;int iSmCaptionHeight;" & _SuffixStruct($tagLOGFONTW, 2) & ";int iMenuWidth;" _
    & "int iMenuHeight;" & _SuffixStruct($tagLOGFONTW, 3) & ";" & _SuffixStruct($tagLOGFONTW, 4) & ";" & _SuffixStruct($tagLOGFONTW, 5)
    Local Const $NONCLIENTMETRICS = DllStructCreate($tagNONCLIENTMETRICS)
    DllStructSetData($NONCLIENTMETRICS, "cbSize", DllStructGetSize($NONCLIENTMETRICS))
    Local $ret = DllCall("user32.dll", "bool", "SystemParametersInfoW", "uint", 41, "uint", DllStructGetSize($NONCLIENTMETRICS), _
    "ptr", DllStructGetPtr($NONCLIENTMETRICS), "uint", 0) ; 41 = SPI_GETNONCLIENTMETRICS
    Return DllStructGetData($NONCLIENTMETRICS, $sElem)
EndFunc ;==>_GetSysFontInfo

Func _SuffixStruct($sStruct, $suff)
    ; suffixes elements in a structure with a given string
    ; useful when embedding multiple copies of a structure within another structure
    Return StringRegExpReplace(StringRegExpReplace($sStruct, "\[", $suff & "["), "([^\]])\s*;", "${1}" & $suff & ";")
EndFunc ;==>_IncrStruct
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...