Jump to content

Calculate required size for gui item?


kev51773
 Share

Recommended Posts

@smashly

Nice solution, here is the implementation:

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

$GUI = GUICreate("Test Script", 300, 200)

$Label = GUICtrlCreateLabel("", 20, 40)
_GUICtrlSetDataEx($Label, "Some very long data")
GUICtrlSetBkColor(-1, 0x00FFFF)

GUISetState(@SW_SHOW, $GUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _GUICtrlSetDataEx($iCtrlID, $sData)
    Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($iCtrlID))
    Local $stText_Data = _WinAPI_GetTextExtentPoint32($hDC, $sData)

    Local $iText_Width = DllStructGetData($stText_Data, "X")
    Local $iText_Height = DllStructGetData($stText_Data, "Y")

    GUICtrlSetPos($iCtrlID, -1, -1, $iText_Width-StringLen($sData), $iText_Height)
    GUICtrlSetData($iCtrlID, $sData)
EndFunc

Just one small problem: as you can see, i highlighted the label specialy to see the real lenght of the text, it seems that it's a little longer then it should be. Any ideas why, or how to fix it correctly? :D

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

@smashly

Nice solution, here is the implementation:

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

$GUI = GUICreate("Test Script", 300, 200)

$Label = GUICtrlCreateLabel("", 20, 40)
_GUICtrlSetDataEx($Label, "Some very long data")
GUICtrlSetBkColor(-1, 0x00FFFF)

GUISetState(@SW_SHOW, $GUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _GUICtrlSetDataEx($iCtrlID, $sData)
    Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($iCtrlID))
    Local $stText_Data = _WinAPI_GetTextExtentPoint32($hDC, $sData)

    Local $iText_Width = DllStructGetData($stText_Data, "X")
    Local $iText_Height = DllStructGetData($stText_Data, "Y")

    GUICtrlSetPos($iCtrlID, -1, -1, $iText_Width-StringLen($sData), $iText_Height)
    GUICtrlSetData($iCtrlID, $sData)
EndFunc
Link to comment
Share on other sites

I found this a while ago.

It works fine for me.

cs_message("This should do the trick, it is a little thinking outside the box...")
cs_message("You can do whatever you want...",12)
cs_message("Thanks to the person who wrote it... it wasn't me...",8.5)
cs_message("Nice Work!",128,5000,0x0000ff00)

Func cs_message($cs_tekst, $cs_font_grootte = 18, $cs_sleep = 3000, $cs_kleur = 0xc6d6fd)
    If $cs_font_grootte = "" Then $cs_font_grootte = 18
    If $cs_kleur = "" Then $cs_kleur = 0xc6d6fd ;default is Details! blauw
    Local $cs_lengte = _GetTextLen($cs_tekst, "Arial", $cs_font_grootte, 400)
    Local $cs_venster = GUICreate("Message", $cs_lengte + 25, $cs_font_grootte + 80, @DesktopWidth / 2- ($cs_lengte + 25) / 2, @DesktopHeight / 2- ($cs_font_grootte + 80) / 2 - 50, 0x00000200, 0x00020000) ;, $DS_SETFOREGROUND, $WS_EX_STATICEDGE)
    GUISetBkColor($cs_kleur)
    $mededeling = GUICtrlCreateLabel($cs_tekst, 10, 20, $cs_lengte, $cs_font_grootte + 75)
    GUICtrlSetFont(-1, $cs_font_grootte)
    GUISetState(@SW_SHOW)
    WinSetOnTop($cs_venster, "", 1)
    Sleep($cs_sleep)
    GUIDelete($cs_venster)
EndFunc   ;==>cs_message

Func _GetTextLen($s_WinText, $s_TextFont, $i_FontSize, $i_FontWeight = -1)
    Local Const $DEFAULT_CHARSET = 0 ; ANSI character set
    Local Const $OUT_CHARACTER_PRECIS = 2
    Local Const $CLIP_DEFAULT_PRECIS = 0
    Local Const $PROOF_QUALITY = 2
    Local Const $FIXED_PITCH = 1
    Local Const $RGN_XOR = 3
    Local Const $LOGPIXELSY = 90
    $h_WinTitle = "Get Label Width"
    If $i_FontWeight = "" Or $i_FontWeight = -1 Then $i_FontWeight = 400 ; default Font weight
    Local $h_GUI = GUICreate($h_WinTitle, 10, 10, -100, -100, 0x80880000, 0x00000080)
    Local $hDC = DllCall("user32.dll", "int", "GetDC", "hwnd", $h_GUI)
    Local $intDeviceCap = DllCall("gdi32.dll", "long", "GetDeviceCaps", "int", $hDC[0], "long", $LOGPIXELSY)
    $intDeviceCap = $intDeviceCap[0]
    Local $intFontHeight = DllCall("kernel32.dll", "long", "MulDiv", "long", $i_FontSize, "long", $intDeviceCap, "long", 72)
    $intFontHeight = -$intFontHeight[0]
    Local $hMyFont = DllCall("gdi32.dll", "hwnd", "CreateFont", "int", $intFontHeight, _
            "int", 0, "int", 0, "int", 0, "int", $i_FontWeight, "int", 0, _
            "int", 0, "int", 0, "int", $DEFAULT_CHARSET, _
            "int", $OUT_CHARACTER_PRECIS, "int", $CLIP_DEFAULT_PRECIS, _
            "int", $PROOF_QUALITY, "int", $FIXED_PITCH, "str", $s_TextFont)
    DllCall("gdi32.dll", "hwnd", "SelectObject", "int", $hDC[0], "hwnd", $hMyFont[0])
    Local $res = DllStructCreate("int;int")
    Local $ret = DllCall("gdi32.dll", "int", "GetTextExtentPoint32", "int", $hDC[0], "str", $s_WinText, "long", StringLen($s_WinText), "ptr", DllStructGetPtr($res))
    Local $intLabelWidth = DllStructGetData($res, 1)
    GUIDelete($h_GUI)
    Return $intLabelWidth
EndFunc   ;==>_GetTextLen
Edited by Details
Link to comment
Share on other sites

I seen this solution before too, i just couldn't find it :D

Here is "readable" example base on it:

#include <GuiConstantsEx.au3>

$GUI = GUICreate("_GetTextLen Demo", 300, 200)

$Label = GUICtrlCreateLabel("", 20, 40)
GUICtrlSetFont(-1, 9, 800, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0x00FFFF)

_GUICtrlSetDataEx($Label, "Some very very long data", 9, 800, "Tahoma")

GUISetState(@SW_SHOW, $GUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _GUICtrlSetDataEx($iCtrlID, $sData, $iFontSize=8.5, $iFontWeight=-1, $sFontName="Arial")
    Local $aData_Len = _GetTextLen($sData, $iFontSize, $iFontWeight, $sFontName)
    
    GUICtrlSetPos($iCtrlID, -1, -1, $aData_Len[0], $aData_Len[1])
    GUICtrlSetData($iCtrlID, $sData)
EndFunc

Func _GetTextLen($s_Data, $i_FontSize=8.5, $i_FontWeight=-1, $s_TextFont="Arial")
    Local Const $DEFAULT_CHARSET = 0 ; ANSI character set
    Local Const $OUT_CHARACTER_PRECIS = 2
    Local Const $CLIP_DEFAULT_PRECIS = 0
    Local Const $PROOF_QUALITY = 2
    Local Const $FIXED_PITCH = 1
    Local Const $RGN_XOR = 3
    Local Const $LOGPIXELSY = 90
    
    If $i_FontWeight = "" Or $i_FontWeight = -1 Then $i_FontWeight = 400 ; default Font weight
    
    Local $h_GDW_GUI = GUICreate("Get Data Width", 10, 10, -100, -100, 0x80880000, 0x00000080)
    
    Local $ah_DC = DllCall("user32.dll", "int", "GetDC", "hwnd", $h_GDW_GUI)
    Local $intDeviceCap = DllCall("gdi32.dll", "long", "GetDeviceCaps", "int", $ah_DC[0], "long", $LOGPIXELSY)
    Local $intFontHeight = DllCall("kernel32.dll", "long", "MulDiv", "long", $i_FontSize, "long", $intDeviceCap[0], "long", 72)
    
    Local $ah_Font = DllCall("gdi32.dll", "hwnd", "CreateFont", "int", -$intFontHeight[0], _
        "int", 0, "int", 0, "int", 0, "int", $i_FontWeight, "int", 0, _
        "int", 0, "int", 0, "int", $DEFAULT_CHARSET, _
        "int", $OUT_CHARACTER_PRECIS, "int", $CLIP_DEFAULT_PRECIS, _
        "int", $PROOF_QUALITY, "int", $FIXED_PITCH, "str", $s_TextFont)
    
    DllCall("gdi32.dll", "hwnd", "SelectObject", "int", $ah_DC[0], "hwnd", $ah_Font[0])
    
    Local $stRet = DllStructCreate("int;int")
    DllCall("gdi32.dll", "int", "GetTextExtentPoint32", _
        "int", $ah_DC[0], "str", $s_Data, "long", StringLen($s_Data), "ptr", DllStructGetPtr($stRet))
    
    Local $a_RetLen[2] = [DllStructGetData($stRet, 1), DllStructGetData($stRet, 2)]
    GUIDelete($h_GDW_GUI)
    
    Return $a_RetLen
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Thanks Zedna, i think this is the best shot we can have on the subject :D

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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