Advertisement
Guest User

Fix tooltip

a guest
Feb 7th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 15.07 KB | None | 0 0
  1. #include <String.au3>
  2. #include <AutoItConstants.au3>
  3.  
  4. Global $sTitle = "Chương trình tự động 1.1.0.0"
  5. Global $aArray = ["Tasks first", "Tasks Second", "Main execute"]
  6.  
  7. $a = _StringSize($sTitle, 9, 700)
  8.  
  9.  
  10.  
  11. _ToolTipRunTask(0, 399)
  12. Sleep(3000)
  13.  
  14. Func _ToolTipRunTask($iTask, $iNumber)
  15. ;~  $sAddSpace = _StringRepeat(" ", StringLen($sTitle) - StringLen($aArray[$iTask] & " - Time: " & $iNumber))
  16.  
  17.     $b = _StringSize($aArray[$iTask] & " - Time: " & $iNumber, 9, 400)
  18.     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $b = ' & $b[2] & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
  19.  
  20.     $s = ($a[2] - $b[2]) / 2
  21.     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $s = ' & $s & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
  22.  
  23.     $i = 0
  24.     While 1
  25.         $i += 1
  26. ;~  ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $i = ' & $i & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
  27.         $sAddSpace = _StringRepeat(" ", $i)
  28.         $d = _StringSize($sAddSpace, 9, 400)
  29.         If @error Or $d[2] >= $s Then ExitLoop
  30.     WEnd
  31.  
  32.  
  33.     ToolTip($sAddSpace & $aArray[$iTask] & " - Time: " & $iNumber, _
  34.             @DesktopWidth, _
  35.             @DesktopHeight, _
  36.             $sTitle, _
  37.             $TIP_INFOICON, _
  38.             $TIP_FORCEVISIBLE)
  39.  
  40. EndFunc   ;==>_ToolTipRunTask
  41.  
  42.  
  43.  
  44.  
  45.  
  46. #include-once
  47.  
  48. ; #INDEX# ============================================================================================================
  49. ; Title .........: _StringSize
  50. ; AutoIt Version : v3.2.12.1 or higher
  51. ; Language ......: English
  52. ; Description ...: Returns size of rectangle required to display string - maximum width can be chosen
  53. ; Remarks .......:
  54. ; Note ..........:
  55. ; Author(s) .....:  Melba23 - thanks to trancexx for the default DC code
  56. ; ====================================================================================================================
  57.  
  58. ;#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
  59.  
  60. ; #CURRENT# ==========================================================================================================
  61. ; _StringSize: Returns size of rectangle required to display string - maximum width can be chosen
  62. ; ====================================================================================================================
  63.  
  64. ; #INTERNAL_USE_ONLY#=================================================================================================
  65. ; _StringSize_Error_Close: Releases DC and deletes font object after error
  66. ; _StringSize_DefaultFontName: Determines Windows default font
  67. ; ====================================================================================================================
  68.  
  69. ; #FUNCTION# =========================================================================================================
  70. ; Name...........: _StringSize
  71. ; Description ...: Returns size of rectangle required to display string - maximum permitted width can be chosen
  72. ; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth[, $hWnd]]]]]])
  73. ; Parameters ....: $sText   - String to display
  74. ;                  $iSize   - [optional] Font size in points - (default = 8.5)
  75. ;                  $iWeight - [optional] Font weight - (default = 400 = normal)
  76. ;                  $iAttrib - [optional] Font attribute (0-Normal (default), 2-Italic, 4-Underline, 8 Strike)
  77. ;                             + 1 if tabs are to be expanded before sizing
  78. ;                  $sName   - [optional] Font name - (default = Tahoma)
  79. ;                  $iWidth  - [optional] Max width for rectangle - (default = 0 => width of original string)
  80. ;                  $hWnd    - [optional] GUI in which string will be displayed - (default 0 => normally not required)
  81. ; Requirement(s) : v3.2.12.1 or higher
  82. ; Return values .: Success - Returns 4-element array: ($iWidth set // $iWidth not set)
  83. ;                  |$array[0] = String reformatted with additonal @CRLF // Original string
  84. ;                  |$array[1] = Height of single line in selected font // idem
  85. ;                  |$array[2] = Width of rectangle required for reformatted // original string
  86. ;                  |$array[3] = Height of rectangle required for reformatted // original string
  87. ;                  Failure - Returns 0 and sets @error:
  88. ;                  |1 - Incorrect parameter type (@extended = parameter index)
  89. ;                  |2 - DLL call error - extended set as follows:
  90. ;                       |1 - GetDC failure
  91. ;                       |2 - SendMessage failure
  92. ;                       |3 - GetDeviceCaps failure
  93. ;                       |4 - CreateFont failure
  94. ;                       |5 - SelectObject failure
  95. ;                       |6 - GetTextExtentPoint32 failure
  96. ;                  |3 - Font too large for chosen max width - a word will not fit
  97. ; Author ........: Melba23 - thanks to trancexx for the default DC code
  98. ; Modified ......:
  99. ; Remarks .......: The use of the $hWnd parameter is not normally necessary - it is only required if the UDF does not
  100. ;                   return correct dimensions without it.
  101. ; Related .......:
  102. ; Link ..........:
  103. ; Example .......: Yes
  104. ;=====================================================================================================================
  105. Func _StringSize($sText, $iSize = 8.5, $iWeight = 400, $iAttrib = 0, $sName = "", $iMaxWidth = 0, $hWnd = 0)
  106.  
  107.     ; Set parameters passed as Default
  108.     If $iSize = Default Then $iSize = 8.5
  109.     If $iWeight = Default Then $iWeight = 400
  110.     If $iAttrib = Default Then $iAttrib = 0
  111.     If $sName = "" Or $sName = Default Then $sName = _StringSize_DefaultFontName()
  112.  
  113.     ; Check parameters are correct type
  114.     If Not IsString($sText) Then Return SetError(1, 1, 0)
  115.     If Not IsNumber($iSize) Then Return SetError(1, 2, 0)
  116.     If Not IsInt($iWeight) Then Return SetError(1, 3, 0)
  117.     If Not IsInt($iAttrib) Then Return SetError(1, 4, 0)
  118.     If Not IsString($sName) Then Return SetError(1, 5, 0)
  119.     If Not IsNumber($iMaxWidth) Then Return SetError(1, 6, 0)
  120.     If Not IsHWnd($hWnd) And $hWnd <> 0 Then Return SetError(1, 7, 0)
  121.  
  122.     Local $aRet, $hDC, $hFont, $hLabel = 0, $hLabel_Handle
  123.  
  124.     ; Check for tab expansion flag
  125.     Local $iExpTab = BitAND($iAttrib, 1)
  126.     ; Remove possible tab expansion flag from font attribute value
  127.     $iAttrib = BitAND($iAttrib, BitNOT(1))
  128.  
  129.     ; If GUI handle was passed
  130.     If IsHWnd($hWnd) Then
  131.         ; Create label outside GUI borders
  132.         $hLabel = GUICtrlCreateLabel("", -10, -10, 10, 10)
  133.         $hLabel_Handle = GUICtrlGetHandle(-1)
  134.         GUICtrlSetFont(-1, $iSize, $iWeight, $iAttrib, $sName)
  135.         ; Create DC
  136.         $aRet = DllCall("user32.dll", "handle", "GetDC", "hwnd", $hLabel_Handle)
  137.         If @error Or $aRet[0] = 0 Then
  138.             GUICtrlDelete($hLabel)
  139.             Return SetError(2, 1, 0)
  140.         EndIf
  141.         $hDC = $aRet[0]
  142.         $aRet = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hLabel_Handle, "int", 0x0031, "wparam", 0, "lparam", 0) ; $WM_GetFont
  143.         If @error Or $aRet[0] = 0 Then
  144.             GUICtrlDelete($hLabel)
  145.             Return SetError(2, _StringSize_Error_Close(2, $hDC), 0)
  146.         EndIf
  147.         $hFont = $aRet[0]
  148.     Else
  149.         ; Get default DC
  150.         $aRet = DllCall("user32.dll", "handle", "GetDC", "hwnd", $hWnd)
  151.         If @error Or $aRet[0] = 0 Then Return SetError(2, 1, 0)
  152.         $hDC = $aRet[0]
  153.         ; Create required font
  154.         $aRet = DllCall("gdi32.dll", "int", "GetDeviceCaps", "handle", $hDC, "int", 90) ; $LOGPIXELSY
  155.         If @error Or $aRet[0] = 0 Then Return SetError(2, _StringSize_Error_Close(3, $hDC), 0)
  156.         Local $iInfo = $aRet[0]
  157.         $aRet = DllCall("gdi32.dll", "handle", "CreateFontW", "int", -$iInfo * $iSize / 72, "int", 0, "int", 0, "int", 0, _
  158.                 "int", $iWeight, "dword", BitAND($iAttrib, 2), "dword", BitAND($iAttrib, 4), "dword", BitAND($iAttrib, 8), "dword", 0, "dword", 0, _
  159.                 "dword", 0, "dword", 5, "dword", 0, "wstr", $sName)
  160.         If @error Or $aRet[0] = 0 Then Return SetError(2, _StringSize_Error_Close(4, $hDC), 0)
  161.         $hFont = $aRet[0]
  162.     EndIf
  163.  
  164.     ; Select font and store previous font
  165.     $aRet = DllCall("gdi32.dll", "handle", "SelectObject", "handle", $hDC, "handle", $hFont)
  166.     If @error Or $aRet[0] = 0 Then Return SetError(2, _StringSize_Error_Close(5, $hDC, $hFont, $hLabel), 0)
  167.     Local $hPrevFont = $aRet[0]
  168.  
  169.     ; Declare variables
  170.     Local $avSize_Info[4], $iLine_Length, $iLine_Height = 0, $iLine_Count = 0, $iLine_Width = 0, $iWrap_Count, $iLast_Word, $sTest_Line
  171.     ; Declare and fill Size structure
  172.     Local $tSize = DllStructCreate("int X;int Y")
  173.     DllStructSetData($tSize, "X", 0)
  174.     DllStructSetData($tSize, "Y", 0)
  175.  
  176.     ; Ensure EoL is @CRLF and break text into lines
  177.     $sText = StringRegExpReplace($sText, "((?<!\x0d)\x0a|\x0d(?!\x0a))", @CRLF)
  178.     Local $asLines = StringSplit($sText, @CRLF, 1)
  179.  
  180.     ; For each line
  181.     For $i = 1 To $asLines[0]
  182.         ; Expand tabs if required
  183.         If $iExpTab Then
  184.             $asLines[$i] = StringReplace($asLines[$i], @TAB, " XXXXXXXX")
  185.         EndIf
  186.         ; Size line
  187.         $iLine_Length = StringLen($asLines[$i])
  188.         DllCall("gdi32.dll", "bool", "GetTextExtentPoint32W", "handle", $hDC, "wstr", $asLines[$i], "int", $iLine_Length, "ptr", DllStructGetPtr($tSize))
  189.         If @error Then Return SetError(2, _StringSize_Error_Close(6, $hDC, $hFont, $hLabel), 0)
  190.         If DllStructGetData($tSize, "X") > $iLine_Width Then $iLine_Width = DllStructGetData($tSize, "X")
  191.         If DllStructGetData($tSize, "Y") > $iLine_Height Then $iLine_Height = DllStructGetData($tSize, "Y")
  192.     Next
  193.  
  194.     ; Check if $iMaxWidth has been both set and exceeded
  195.     If $iMaxWidth <> 0 And $iLine_Width > $iMaxWidth Then ; Wrapping required
  196.         ; For each Line
  197.         For $j = 1 To $asLines[0]
  198.             ; Size line unwrapped
  199.             $iLine_Length = StringLen($asLines[$j])
  200.             DllCall("gdi32.dll", "bool", "GetTextExtentPoint32W", "handle", $hDC, "wstr", $asLines[$j], "int", $iLine_Length, "ptr", DllStructGetPtr($tSize))
  201.             If @error Then Return SetError(2, _StringSize_Error_Close(6, $hDC, $hFont, $hLabel), 0)
  202.             ; Check wrap status
  203.             If DllStructGetData($tSize, "X") < $iMaxWidth - 4 Then
  204.                 ; No wrap needed so count line and store
  205.                 $iLine_Count += 1
  206.                 $avSize_Info[0] &= $asLines[$j] & @CRLF
  207.             Else
  208.                 ; Wrap needed so zero counter for wrapped lines
  209.                 $iWrap_Count = 0
  210.                 ; Build line to max width
  211.                 While 1
  212.                     ; Zero line width
  213.                     $iLine_Width = 0
  214.                     ; Initialise pointer for end of word
  215.                     $iLast_Word = 0
  216.                     ; Add characters until EOL or maximum width reached
  217.                     For $i = 1 To StringLen($asLines[$j])
  218.                         ; Is this just past a word ending?
  219.                         If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
  220.                         ; Increase line by one character
  221.                         $sTest_Line = StringMid($asLines[$j], 1, $i)
  222.                         ; Get line length
  223.                         $iLine_Length = StringLen($sTest_Line)
  224.                         DllCall("gdi32.dll", "bool", "GetTextExtentPoint32W", "handle", $hDC, "wstr", $sTest_Line, "int", $iLine_Length, "ptr", DllStructGetPtr($tSize))
  225.                         If @error Then Return SetError(2, _StringSize_Error_Close(6, $hDC, $hFont, $hLabel), 0)
  226.                         $iLine_Width = DllStructGetData($tSize, "X")
  227.                         ; If too long exit the loop
  228.                         If $iLine_Width >= $iMaxWidth - 4 Then ExitLoop
  229.                     Next
  230.                     ; End of the line of text?
  231.                     If $i > StringLen($asLines[$j]) Then
  232.                         ; Yes, so add final line to count
  233.                         $iWrap_Count += 1
  234.                         ; Store line
  235.                         $avSize_Info[0] &= $sTest_Line & @CRLF
  236.                         ExitLoop
  237.                     Else
  238.                         ; No, but add line just completed to count
  239.                         $iWrap_Count += 1
  240.                         ; Check at least 1 word completed or return error
  241.                         If $iLast_Word = 0 Then Return SetError(3, _StringSize_Error_Close(0, $hDC, $hFont, $hLabel), 0)
  242.                         ; Store line up to end of last word
  243.                         $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF
  244.                         ; Strip string to point reached
  245.                         $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word)
  246.                         ; Trim leading whitespace
  247.                         $asLines[$j] = StringStripWS($asLines[$j], 1)
  248.                         ; Repeat with remaining characters in line
  249.                     EndIf
  250.                 WEnd
  251.                 ; Add the number of wrapped lines to the count
  252.                 $iLine_Count += $iWrap_Count
  253.             EndIf
  254.         Next
  255.         ; Reset any tab expansions
  256.         If $iExpTab Then
  257.             $avSize_Info[0] = StringRegExpReplace($avSize_Info[0], "\x20?XXXXXXXX", @TAB)
  258.         EndIf
  259.         ; Complete return array
  260.         $avSize_Info[1] = $iLine_Height
  261.         $avSize_Info[2] = $iMaxWidth
  262.         ; Convert lines to pixels and add drop margin
  263.         $avSize_Info[3] = ($iLine_Count * $iLine_Height) + 4
  264.     Else ; No wrapping required
  265.         ; Create return array (add drop margin to height)
  266.         Local $avSize_Info[4] = [$sText, $iLine_Height, $iLine_Width, ($asLines[0] * $iLine_Height) + 4]
  267.     EndIf
  268.  
  269.     ; Clear up
  270.     DllCall("gdi32.dll", "handle", "SelectObject", "handle", $hDC, "handle", $hPrevFont)
  271.     DllCall("gdi32.dll", "bool", "DeleteObject", "handle", $hFont)
  272.     DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "handle", $hDC)
  273.     If $hLabel Then GUICtrlDelete($hLabel)
  274.  
  275.     Return $avSize_Info
  276.  
  277. EndFunc   ;==>_StringSize
  278.  
  279. ; #INTERNAL_USE_ONLY#============================================================================================================
  280. ; Name...........: _StringSize_Error_Close
  281. ; Description ...: Releases DC and deleted font object if required after error
  282. ; Syntax ........: _StringSize_Error_Close ($iExtCode, $hDC, $hGUI)
  283. ; Parameters ....: $iExtCode   - code to return
  284. ;                  $hDC, $hGUI - handles as set in _StringSize function
  285. ; Return value ..: $iExtCode as passed
  286. ; Author ........: Melba23
  287. ; Modified.......:
  288. ; Remarks .......: This function is used internally by _StringSize
  289. ; ===============================================================================================================================
  290. Func _StringSize_Error_Close($iExtCode, $hDC = 0, $hFont = 0, $hLabel = 0)
  291.  
  292.     If $hFont <> 0 Then DllCall("gdi32.dll", "bool", "DeleteObject", "handle", $hFont)
  293.     If $hDC <> 0 Then DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "handle", $hDC)
  294.     If $hLabel Then GUICtrlDelete($hLabel)
  295.  
  296.     Return $iExtCode
  297.  
  298. EndFunc   ;==>_StringSize_Error_Close
  299.  
  300. ; #INTERNAL_USE_ONLY#============================================================================================================
  301. ; Name...........: _StringSize_DefaultFontName
  302. ; Description ...: Determines Windows default font
  303. ; Syntax ........: _StringSize_DefaultFontName()
  304. ; Parameters ....: None
  305. ; Return values .: Success - Returns name of system default font
  306. ;                  Failure - Returns "Tahoma"
  307. ; Author ........: Melba23, based on some original code by Larrydalooza
  308. ; Modified.......:
  309. ; Remarks .......: This function is used internally by _StringSize
  310. ; ===============================================================================================================================
  311. Func _StringSize_DefaultFontName()
  312.  
  313.     ; Get default system font data
  314.     Local $tNONCLIENTMETRICS = DllStructCreate("uint;int;int;int;int;int;byte[60];int;int;byte[60];int;int;byte[60];byte[60];byte[60]")
  315.     DllStructSetData($tNONCLIENTMETRICS, 1, DllStructGetSize($tNONCLIENTMETRICS))
  316.     DllCall("user32.dll", "int", "SystemParametersInfo", "int", 41, "int", DllStructGetSize($tNONCLIENTMETRICS), "ptr", DllStructGetPtr($tNONCLIENTMETRICS), "int", 0)
  317.     Local $tLOGFONT = DllStructCreate("long;long;long;long;long;byte;byte;byte;byte;byte;byte;byte;byte;char[32]", DllStructGetPtr($tNONCLIENTMETRICS, 13))
  318.     If IsString(DllStructGetData($tLOGFONT, 14)) Then
  319.         Return DllStructGetData($tLOGFONT, 14)
  320.     Else
  321.         Return "Tahoma"
  322.     EndIf
  323.  
  324. EndFunc   ;==>_StringSize_DefaultFontName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement