Jump to content

ControlGetPos return strange width value for a radio button.


 Share

Recommended Posts

Hello:
Please, after looking deep inside this forum, and in vain, I would like some help about using the command "ControlGetPos" to retrieve the width of radio buttons declared
within a group. The purpose of my little script is to encapsulate 4 radio buttons inside the group's rectangle. My plan was to first create a group with a dummy width and a dummy height, then to add the radio 4 buttons with different lengths of text and finalize this creation by computing the optimal rectangle size for the encapsulation.

Here are the problems I discovered:

  • Using width = -1 in the GUICtrlCreateRadio command does not provide the text as expected according the Help ([Optional] The width of the control (default text of. automatic width adjustment)) -> to see what happens, I force a background color to better see the radio control rectangle (last letters are truncated) --> see the jpeg image
  • But resizing my radio control using the control panel ControlGetPos (without add-on in the return values), shows now a rather too large rectangle, big space after the last character ???!
  • But I continue by calculating for each button the broadest one to be sure to complete correctly my encapsulation -> and I discovered that each width values of each radio button printed in the console does not match what I can see on the screen -> In fact, the printed values appear to represent only the length of the text portion inside the radio control, forgetting the size of the left circle !

I'm quite sure this is my bad understanding of how to use these commands, but I do not succeed to detect alone where I am wrong ! So I will be really happy if someone could help me to understand my mistakes.

Thanks, Alain.
Please find here the screenshot of my GUI and the short script.

FYI: I'm using :

  • Autoit 3.3.14.2
  • Windows 7 Home Premium Service Pack 1
  • Computer ASUS N56V
  • Screen resolution: 1920x1080 (recommended)
  • Theme :Windows Classic

 

histogram_issue.au3

ControlGetPos_pb.jpg

Link to comment
Share on other sites

It's fairly simple. You need to set the width of each radio button according to the maximum width of the longest text. You can do this using the fourth parameter of GUICtrlCreateRadio(). Probably the easiest way to get the width of the text is to use Melba23's StringSize UDF
 https://www.autoitscript.com/forum/topic/114034-stringsize-m23-new-version-16-aug-11/ You need to rewrite your code and only create the first radio button once you know the required width. With your present code, the following works for only the last three radio controls because of the current method you are using.

Global $Histo2 = GUICtrlCreateRadio("Mode 2 (Draw Line GUI) ", $xleft, $ytop, $ctrlwMax)

 

Edited by czardas
Link to comment
Share on other sites

Thanks czardas for your help !

But after checking with the Melba23's function "StringSize", this does not solve my issue. I have written a simple script below  --> really, it looks like the Radio Button's width computation is forgetting the "left circle" object.

So how to understand the "GUICtrlCreateRadio" -> width parameter sentence (https://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateRadio.htm) :

  • width [optional] The width of the control (default text autofit in width). ==> AUTOFIT ???

Thanks, Alain.

#include <Misc.au3>
#include <GUIConstants.au3>
#include <ColorConstants.au3>
#include "StringSize.au3"

    local $hgui = GUICreate("Histogram", 500, 500)

    local $xleft = 20, $ytop = 20, $hctrl = 30, $sp = 10
    local $sText = "balabla 0123456789"
    local $Histo1 = GUICtrlCreateRadio($sText, $xleft, $ytop)
    GUICtrlSetBkColor(-1, $COLOR_RED) ; just to check the control rectangle expansion !
    GUICtrlSetState(-1, $GUI_CHECKED)
    $ytop += $sp + $hctrl
    GUICtrlCreateLabel("Just verify above that the last digit '9' is truncated !!!!", $xleft, $ytop)

    $sText = "blablablablablablablablablabla ABCDEFGH"
    $ytop += $sp + $hctrl
    local $Histo2 = GUICtrlCreateRadio($sText, $xleft, $ytop)
    GUICtrlSetBkColor(-1, $COLOR_RED)
    $ytop += $sp + $hctrl
    GUICtrlCreateLabel("Just verify above that the last letter 'H' is truncated !!!!", $xleft, $ytop)
    ;
    ; using StringSize from Melba23
    ;
    $ytop += $sp + $hctrl
    GUICtrlCreateLabel("--------------- Using Melba23 StringSize method ----------------------", $xleft, $ytop)
    local $aSize = _StringSize($sText)
    $ytop += $sp + $hctrl
    local $Histo3 = GUICtrlCreateRadio($aSize[0], $xleft, $ytop, $aSize[2], $aSize[3])
    GUICtrlSetBkColor(-1, $COLOR_RED)
    $ytop += $sp + $hctrl
    GUICtrlCreateLabel("Using '_StringSize($sText)' -> the text is still truncated !!!!", $xleft, $ytop)

    $ytop += $sp + $hctrl
    GUICtrlCreateLabel("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", $xleft, $ytop)
    $ytop += $sp + $hctrl
    GUICtrlCreateLabel("See below to check that Melba23's function works nice for a Label Control !", $xleft, $ytop)
    $aSize = _StringSize($sText, Default, Default, Default, Default, 200)
    $ytop += $sp + $hctrl
    GUICtrlCreateLabel($aSize[0], $xleft, $ytop, $aSize[2], $aSize[3])
    GUICtrlSetBkColor(-1, 0x80FF80)

    GUISetState(@SW_SHOW)

    While 1
        $msg = GUIGetMsg()
        Switch $msg
            ; **********************************************************************
            Case $GUI_EVENT_CLOSE ; Exit
                GUIDelete($hgui)
                Exit
        EndSwitch
    WEnd

 

ControlGetPos_pb2.jpg

Link to comment
Share on other sites

If StringSize doesn't take the size of the radio button into account, then you should in your code. Figure out what the offset is, and add it to the width of the control. It's pretty obvious that the function doesn't know about radio control sizing, so YOU have to adapt the result returned to suit your needs.

The _StringSize function is a tool that assists in getting the proper size, you just need to know how the tool works, and adjust to fit it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You are right BrewManNH, and thanks for your advice.

This is what I was starting to do yesterday, writing my own "_GetTextSize()" function (of course based on several lecture from AutoiT forum - :P) -> and it works not so bad :

#include <GDIPlus.au3>
#include <GUIImageList.au3>
#include <GUIConstants.au3>
#include <FontConstants.au3>
#include <GuiButton.au3>

    local $hgui = GUICreate("Radio Button", 600, 300)
    local $xleft = 10, $ytop = 20
    Local $sText = "0123456789 blablaplaplaplablabla ABCDEFGH"
    ;
    Local $sFont = "Comic Sans MS"
    local $FontName = "Arial"
    local $iFontStyle = 0x00000000
        ; 0 - Normal weight or thickness of the typeface
        ; 1 - Bold typeface
        ; 2 - Italic typeface
        ; 4 - Underline
        ; 8 - Strikethrough
    local $fFontSize = 8
    ;
    ; Note: curiously, the "GUISetFont" and the "_GDIPlus_FontCreate" commands do not use the same logic for the "bold" type !
    ; Please compare "weight"+"attribute" to "iStyle", and adapt correctly
    ;
    GUISetFont($fFontSize, $FW_NORMAL, $GUI_FONTNORMAL, $FontName)
        ; The weight of the font in the range 0 through 1000. For example, 400 is normal and 700 is bold. If this value is zero, a default weight is used.
        ; The following values are defined for convenience.
        ; $FW_DONTCARE = 0 (Use the default font weight)
        ; $FW_THIN = 100
        ; $FW_EXTRALIGHT = 200
        ; $FW_LIGHT = 300
        ; $FW_NORMAL = 400
        ; $FW_MEDIUM = 500
        ; $FW_SEMIBOLD = 600
        ; $FW_BOLD = 700
        ; $FW_EXTRABOLD = 800
        ; $FW_HEAVY = 900
    Local $Button1 = GUICtrlCreateRadio($sText, $xleft, $ytop)
    Local $HandleButton1 = GUICtrlGetHandle($Button1)
    ;
    Local $tRectF = _GetTextSize($HandleButton1, $sText, $FontName, $iFontStyle, $fFontSize)
    Local $RealWidth = Ceiling(DllStructGetData($tRectF, "Width"))
    Local $RealHeight = Ceiling(DllStructGetData($tRectF, "Height"))
    ConsoleWrite("! Button1 (method 1) - Ideal width: " & $RealWidth & " height: " & $RealHeight & @CRLF)
    GUICtrlSetPos($Button1, $xleft, $ytop, $RealWidth, $RealHeight)
    GUICtrlSetBkColor(-1, $COLOR_RED) ; just to check the control rectangle expansion !
    Local $aIdealSize = _GUICtrlButton_GetIdealSize($Button1)
    ConsoleWrite("! Button1 (method 2) - Ideal width: " & $aIdealSize[0] & " height: " & $aIdealSize[1] & @crlf)
    ;
    $ytop += 20 + $RealHeight
    ;
    $FontName = "arial"
    $iFontStyle = 0x00000000
    $fFontSize = 12
    GUISetFont($fFontSize, $FW_NORMAL, $GUI_FONTNORMAL, $FontName)
    Local $Button2 = GUICtrlCreateRadio($sText, $xleft, $ytop)
    Local $HandleButton2 = GUICtrlGetHandle($Button2)
    $tRectF = _GetTextSize($HandleButton2, $sText, $FontName, $iFontStyle, $fFontSize)
    $RealWidth = Ceiling(DllStructGetData($tRectF, "Width"))
    $RealHeight = Ceiling(DllStructGetData($tRectF, "Height"))
    ConsoleWrite("! Button2 (method 1) - Ideal width: " & $RealWidth & " height: " & $RealHeight & @CRLF)
    GUICtrlSetPos($Button2, $xleft, $ytop, $RealWidth, $RealHeight)
    GUICtrlSetBkColor(-1, $COLOR_RED) ; just to check the control rectangle expansion !
    ;
    $ytop += 20 + $RealHeight
    ;
    $FontName = "Comic Sans MS"
    $fFontSize = 14
    GUISetFont($fFontSize, $FW_BOLD, $GUI_FONTNORMAL, $FontName)
    Local $Button3 = GUICtrlCreateRadio($sText, $xleft, $ytop)
    ; _GUICtrlButton_SetImageList($Button3, $hImage)
    $aIdealSize = _GUICtrlButton_GetIdealSize($Button3)
    ConsoleWrite("! Button3 (method 2) - Ideal width: " & $aIdealSize[0] & " height: " & $aIdealSize[1] & @crlf)
    _GUICtrlButton_SetSize($Button3, $aIdealSize[0], $aIdealSize[1])
    GUICtrlSetBkColor(-1, $COLOR_RED) ; just to check the control rectangle expansion !
    ;
    GUISetState(@SW_SHOW)

    While 1
        $msg = GUIGetMsg()
        Switch $msg
            ; **********************************************************************
            Case $GUI_EVENT_CLOSE ; Exit
                GUIDelete($hgui)
                Exit
        EndSwitch
    WEnd
; ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Func _GetTextSize($CtrlHandle, $sText, $sFontName, $iStyle, $fSize)
    _GDIPlus_Startup()
        Local $hGraphic  = _GDIPlus_GraphicsCreateFromHWND($CtrlHandle)
        Local $hFormat = _GDIPlus_StringFormatCreate()
        Local $hFamily = _GDIPlus_FontFamilyCreate($sFontName)
        Local $hFont = _GDIPlus_FontCreate($hFamily, $fSize, $iStyle)
        Local $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)
        Local $aInfo  = _GDIPlus_GraphicsMeasureString($hGraphic , $sText, $hFont, $tLayout, $hFormat)
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        _GDIPlus_StringFormatDispose($hFormat)
        _GDIPlus_GraphicsDispose($hGraphic )
    _GDIPlus_Shutdown()
    If IsArray($aInfo ) then
        return $aInfo [0]
    else
        return setError(0, 0, -1)
    endif
Endfunc ; _GetTextSize

; oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

But, finally, exploring the AutoIt help, I discover these functions which are better : "_GUICtrlButton_GetIdealSize()" and "_GUICtrlButton_SetIdealSize()".:drool:

So I think that a link to them into the "GUICtrlCreateRadio" presentation (https://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateRadio.htm) would be welcome.

Anyhow, thanks to everybody for your help.:D

(This post could be closed.)

Alain.

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...