NewPlaza Posted July 3, 2013 Posted July 3, 2013 Hello, Is there a way to center text for GUICreate? #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Local $MyGUI = GUICreate("GUI Test", 400, 100, -1, -1, $WS_BORDER) GUISetState(@SW_SHOW) Do Local $msg = GUIGetMsg() Until $msg = $GUI_EVENT_CLOSE I have tried many ways but the text 'GUI Test' is always on the left. What I don't want to do is whitespace before the text. Any help would be appreciated.
BrewManNH Posted July 3, 2013 Posted July 3, 2013 Even if there were an easy way to center the text, it would still have whitespace before the title text. But, there's no easy way that I'm aware of to do 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 GudeHow 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
Moderators Melba23 Posted July 3, 2013 Moderators Posted July 3, 2013 NewPlaza,If you use my StringSize UDF you can do it this way: expandcollapse popup#include <GUIConstantsEx.au3> #include <StringSize.au3> #include <WinAPI.au3> $iGUI_Width = 500 $sGUI_Title = "Centred Title" $hGUI = GUICreate("Test", $iGUI_Width, 500) GUISetState() $iButton_Width = _WinAPI_GetSystemMetrics(30) ; SM_CXSIZE $iBorder_Width = _WinAPI_GetSystemMetrics(7) ; SM_CXFIXEDFRAME $iTitle_Width = $iGUI_Width - (3 * $iButton_Width) - (8 * $iBorder_Width) While 1 ; Add white space to each end of title $sGUI_Title = " " & $sGUI_Title & " " ; Get width of title $aRet = _StringSize($sGUI_Title) If $aRet[2] > $iTitle_Width Then ; If now too large, remove last added spaces $sGUI_Title = StringTrimLeft(StringTrimRight($sGUI_Title, 1), 1) ExitLoop EndIf WEnd ; Set title WinSetTitle($hGUI, "", $sGUI_Title) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEndAny use? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
funkey Posted July 3, 2013 Posted July 3, 2013 (edited) I modified an old example of mine. You can play around with it. expandcollapse popup;Coded by funkey!!!! ;2011, Oct 20 #include <Constants.au3> #include <GUIConstantsEx.au3> #include <WinAPIEx.au3> #include <WindowsConstants.au3> Global Const $SPI_GETNONCLIENTMETRICS = 41 Global Const $SPI_SETNONCLIENTMETRICS = 42 Global Const $SPIF_SENDCHANGE = 2 Global Const $SPIF_UPDATEINIFILE = 1 Global Const $tagNONCLIENTMETRICS = "uint cbSize;int iBorderWidth;int iScrollWidth;int iScrollHeight;int iCaptionWidth;int iCaptionHeight;" & _ "byte lfCaptionFont[92];int iSmCaptionWidth;int iSmCaptionHeight;byte lfSmCaptionFont[92];int iMenuWidth;int iMenuHeight;" & _ "byte lfMenuFont[92];byte lfStatusFont[92];byte lfMessageFont[92]";int iPaddedBorderWidth" Global $sTitle1 = "Something special" Global $ghProcSpecialTitle, $ghWndSpecialTitle Global $nonclientmetrics = DllStructCreate($tagNONCLIENTMETRICS) DllStructSetData($nonclientmetrics, "cbSize", sizeof($nonclientmetrics)) Global $LogFontCaption = DllStructCreate($tagLOGFONT, DllStructGetPtr($nonclientmetrics, "lfCaptionFont")) _WinAPI_SystemParametersInfo($SPI_GETNONCLIENTMETRICS, sizeof($nonclientmetrics), DllStructGetPtr($nonclientmetrics), 0) Global $CaptionFontSize = Abs(DllStructGetData($LogFontCaption, "Height")) Global $CaptionHeight = _WinAPI_GetSystemMetrics(4) ;SM_CYCAPTION Global $BorderWidth = _WinAPI_GetSystemMetrics(5) ;SM_CXBORDER Global $hForm1 = GUICreate($sTitle1, 400, 400, 100, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX)) _RemoveTheme($hForm1) Global $nSwitch = GUICtrlCreateButton("Change Color", 50, 50, 100, 30) _SpecialTitle($hForm1, 1, 0) GUISetState(@SW_SHOW, $hForm1) Local $msg Do $msg = GUIGetMsg() If $msg = $nSwitch Then _SpecialTitleColor(0, 0xFFFFFF, 0x0, Random(0, 0xffffff, 1)) _SpecialTitle($ghWndSpecialTitle, 1, 1) EndIf Until $msg = $GUI_EVENT_CLOSE _SpecialTitle($ghWndSpecialTitle, 0, 0) Func _SpecialTitle($hWnd, $acitive = 1, $redraw = 1) Static $TitleOn = 0, $hHook, $pHook If $TitleOn = 0 And $acitive = 1 Then $hHook = DllCallbackRegister('_WinProc', 'ptr', 'hwnd;uint;long;ptr') $pHook = DllCallbackGetPtr($hHook) $ghProcSpecialTitle = _WinAPI_SetWindowLongEx($hWnd, -4, $pHook); GWL_WNDPROC = -4 $TitleOn = 1 $ghWndSpecialTitle = $hWnd ElseIf $TitleOn = 1 And $acitive = 0 Then If $hWnd <> $ghWndSpecialTitle Then Return _WinAPI_SetWindowLongEx($hWnd, -4, $ghProcSpecialTitle); GWL_WNDPROC = -4 DllCallbackFree($ghProcSpecialTitle) $TitleOn = 0 $hHook = 0 $pHook = 0 $ghWndSpecialTitle = 0 EndIf If $redraw Then _WinAPI_RedrawWindow($hWnd, 0, 0, BitOR(1024, 256, 1)) EndFunc ;==>_SpecialTitle Func _SpecialTitleColor($iSwitch, $ColorText = 0xFFFFFF, $ColorPen = 0x00, $ColorBrush = 0x0000FF) Static $sColorText = $ColorText, $sColorPen = $ColorPen, $sColorBrush = $ColorBrush Switch $iSwitch Case 0 ;new values $sColorText = $ColorText $sColorPen = $ColorPen $sColorBrush = $ColorBrush Case 1 Return $sColorText Case 2 Return $sColorPen Case 3 Return $sColorBrush EndSwitch EndFunc Func _WinProc($hWnd, $iMsg, $wParam, $lParam) Local $hDC, $hFont, $hFont_orig, $hPen, $hPen_orig, $hBrush, $hBrush_orig, $tRect, $yCaption Local $sTitle, $iRectRight, $iIconX, $iIconW Local $Res = _WinAPI_CallWindowProc($ghProcSpecialTitle, $hWnd, $iMsg, $wParam, $lParam) Switch $iMsg Case $WM_NCACTIVATE, $WM_NCPAINT Switch $iMsg Case $WM_NCACTIVATE $hDC = _WinAPI_GetWindowDC($hWnd) Case $WM_NCPAINT $hDC = _WinAPI_GetDCEx($hWnd, $wParam, BitOR(1, 128)) EndSwitch $sTitle = WinGetTitle($hWnd) $hFont = _WinAPI_CreateFontIndirect($LogFontCaption) $hFont_orig = _WinAPI_SelectObject($hDC, $hFont) _WinAPI_SetTextColor($hDC, _SpecialTitleColor(1)) ;TextFarbe _WinAPI_SetBkMode($hDC, $TRANSPARENT) $hPen = _WinAPI_CreatePen($PS_SOLID, 1, _SpecialTitleColor(2)) ;Rahmenfarbe $hPen_orig = _WinAPI_SelectObject($hDC, $hPen) $hBrush = _WinAPI_CreateSolidBrush(_SpecialTitleColor(3)) ;Farbe Füllung $hBrush_orig = _WinAPI_SelectObject($hDC, $hBrush) $tRect = DllStructCreate($tagRect) $iIconX = _WinAPI_GetSystemMetrics(32) ;SM_CXFRAME $iIconW = _WinAPI_GetSystemMetrics(49) ;SM_CXSMICON $iRectRight = WinGetPos($hWnd) $iRectRight = $iRectRight[2] - 58 DllStructSetData($tRect, "Left", $iIconX + $iIconW + 1) ;how do I get this value? DllStructSetData($tRect, "Top", $BorderWidth + 1 + ($CaptionHeight - $CaptionFontSize) / 2) DllStructSetData($tRect, "Right", $iRectRight) ;how do I get this value? DllStructSetData($tRect, "Bottom", $iIconX + $iIconW - 1) ;?? ;~ _WinAPI_DrawText($hDC, " " & $sTitle & " ", $tRect, $DT_CALCRECT) _WinAPI_DrawText($hDC, " " & $sTitle & " ", $tRect, $DT_CENTER) DllStructSetData($tRect, "Bottom", DllStructGetData($tRect, "Bottom") + 1) _WinAPI_RoundRect($hDC, $tRect, 8, 8) _WinAPI_DrawText($hDC, " " & $sTitle, $tRect, $DT_CENTER + $DT_END_ELLIPSIS) _WinAPI_SelectObject($hDC, $hBrush_orig) _WinAPI_DeleteObject($hBrush) _WinAPI_SelectObject($hDC, $hPen_orig) _WinAPI_DeleteObject($hPen) _WinAPI_SelectObject($hDC, $hFont_orig) _WinAPI_DeleteObject($hFont) _WinAPI_ReleaseDC($hWnd, $hDC) EndSwitch Return $Res EndFunc ;==>_WinProc Func sizeof($struct) Return DllStructGetSize($struct) EndFunc ;==>sizeof Func _RemoveTheme($hCtrl) If Not IsHWnd($hCtrl) Then $hCtrl = GUICtrlGetHandle($hCtrl) If IsHWnd($hCtrl) Then Local $aR = DllCall("UxTheme.dll", "Long", "SetWindowTheme", "HWND", $hCtrl, "Ptr", 0, "WStr", " ") If Not @error And $aR[0] = 0 Then Return True EndIf Return False EndFunc ;==>_RemoveThemeEdit: Just the title text centered. expandcollapse popup;Coded by funkey!!!! ;2011, Oct 20 #include <Constants.au3> #include <GUIConstantsEx.au3> #include <WinAPIEx.au3> #include <WindowsConstants.au3> Global Const $SPI_GETNONCLIENTMETRICS = 41 Global Const $SPI_SETNONCLIENTMETRICS = 42 Global Const $SPIF_SENDCHANGE = 2 Global Const $SPIF_UPDATEINIFILE = 1 Global Const $tagNONCLIENTMETRICS = "uint cbSize;int iBorderWidth;int iScrollWidth;int iScrollHeight;int iCaptionWidth;int iCaptionHeight;" & _ "byte lfCaptionFont[92];int iSmCaptionWidth;int iSmCaptionHeight;byte lfSmCaptionFont[92];int iMenuWidth;int iMenuHeight;" & _ "byte lfMenuFont[92];byte lfStatusFont[92];byte lfMessageFont[92]";int iPaddedBorderWidth" Global $sTitle1 = "Something special" Global $ghProcSpecialTitle, $ghWndSpecialTitle Global $nonclientmetrics = DllStructCreate($tagNONCLIENTMETRICS) DllStructSetData($nonclientmetrics, "cbSize", sizeof($nonclientmetrics)) Global $LogFontCaption = DllStructCreate($tagLOGFONT, DllStructGetPtr($nonclientmetrics, "lfCaptionFont")) _WinAPI_SystemParametersInfo($SPI_GETNONCLIENTMETRICS, sizeof($nonclientmetrics), DllStructGetPtr($nonclientmetrics), 0) Global $CaptionFontSize = Abs(DllStructGetData($LogFontCaption, "Height")) Global $CaptionHeight = _WinAPI_GetSystemMetrics(4) ;SM_CYCAPTION Global $BorderWidth = _WinAPI_GetSystemMetrics(5) ;SM_CXBORDER Global $hForm1 = GUICreate("", 400, 400, 100, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX)) _RemoveTheme($hForm1) Global $nSwitch = GUICtrlCreateButton("Change Color", 50, 50, 100, 30) _SpecialTitle($hForm1, 1, 0) GUISetState(@SW_SHOW, $hForm1) Local $msg Do $msg = GUIGetMsg() If $msg = $nSwitch Then _SpecialTitleColor(0, Random(0, 0xffffff, 1)) _SpecialTitle($ghWndSpecialTitle, 1, 1) EndIf Until $msg = $GUI_EVENT_CLOSE _SpecialTitle($ghWndSpecialTitle, 0, 0) Func _SpecialTitle($hWnd, $acitive = 1, $redraw = 1) Static $TitleOn = 0, $hHook, $pHook If $TitleOn = 0 And $acitive = 1 Then $hHook = DllCallbackRegister('_WinProc', 'ptr', 'hwnd;uint;long;ptr') $pHook = DllCallbackGetPtr($hHook) $ghProcSpecialTitle = _WinAPI_SetWindowLongEx($hWnd, -4, $pHook); GWL_WNDPROC = -4 $TitleOn = 1 $ghWndSpecialTitle = $hWnd ElseIf $TitleOn = 1 And $acitive = 0 Then If $hWnd <> $ghWndSpecialTitle Then Return _WinAPI_SetWindowLongEx($hWnd, -4, $ghProcSpecialTitle); GWL_WNDPROC = -4 DllCallbackFree($ghProcSpecialTitle) $TitleOn = 0 $hHook = 0 $pHook = 0 $ghWndSpecialTitle = 0 EndIf If $redraw Then _WinAPI_RedrawWindow($hWnd, 0, 0, BitOR(1024, 256, 1)) EndFunc ;==>_SpecialTitle Func _SpecialTitleColor($iSwitch, $ColorText = 0xFFFFFF, $ColorPen = 0x00, $ColorBrush = 0x0000FF) Static $sColorText = $ColorText, $sColorPen = $ColorPen, $sColorBrush = $ColorBrush Switch $iSwitch Case 0 ;new values $sColorText = $ColorText $sColorPen = $ColorPen $sColorBrush = $ColorBrush Case 1 Return $sColorText Case 2 Return $sColorPen Case 3 Return $sColorBrush EndSwitch EndFunc Func _WinProc($hWnd, $iMsg, $wParam, $lParam) Local $hDC, $hFont, $hFont_orig, $hPen, $hPen_orig, $hBrush, $hBrush_orig, $tRect, $yCaption Local $sTitle, $iRectRight, $iIconX, $iIconW Local $Res = _WinAPI_CallWindowProc($ghProcSpecialTitle, $hWnd, $iMsg, $wParam, $lParam) Switch $iMsg Case $WM_NCACTIVATE, $WM_NCPAINT Switch $iMsg Case $WM_NCACTIVATE $hDC = _WinAPI_GetWindowDC($hWnd) Case $WM_NCPAINT $hDC = _WinAPI_GetDCEx($hWnd, $wParam, BitOR(1, 128)) EndSwitch ;~ $sTitle = WinGetTitle($hWnd) $sTitle = $sTitle1 $hFont = _WinAPI_CreateFontIndirect($LogFontCaption) $hFont_orig = _WinAPI_SelectObject($hDC, $hFont) _WinAPI_SetTextColor($hDC, _SpecialTitleColor(1)) ;TextFarbe _WinAPI_SetBkMode($hDC, $TRANSPARENT) ;~ $hPen = _WinAPI_CreatePen($PS_SOLID, 1, _SpecialTitleColor(2)) ;Rahmenfarbe ;~ $hPen_orig = _WinAPI_SelectObject($hDC, $hPen) ;~ $hBrush = _WinAPI_CreateSolidBrush(_SpecialTitleColor(3)) ;Farbe Füllung ;~ $hBrush_orig = _WinAPI_SelectObject($hDC, $hBrush) $tRect = DllStructCreate($tagRect) $iIconX = _WinAPI_GetSystemMetrics(32) ;SM_CXFRAME $iIconW = _WinAPI_GetSystemMetrics(49) ;SM_CXSMICON $iRectRight = WinGetPos($hWnd) $iRectRight = $iRectRight[2] - 58 DllStructSetData($tRect, "Left", $iIconX + $iIconW + 1) ;how do I get this value? DllStructSetData($tRect, "Top", $BorderWidth + 1 + ($CaptionHeight - $CaptionFontSize) / 2) DllStructSetData($tRect, "Right", $iRectRight) ;how do I get this value? DllStructSetData($tRect, "Bottom", $iIconX + $iIconW - 1) ;?? ;~ _WinAPI_DrawText($hDC, " " & $sTitle & " ", $tRect, $DT_CALCRECT) _WinAPI_DrawText($hDC, " " & $sTitle & " ", $tRect, $DT_CENTER) ;~ DllStructSetData($tRect, "Bottom", DllStructGetData($tRect, "Bottom") + 1) ;~ _WinAPI_RoundRect($hDC, $tRect, 8, 8) ;~ _WinAPI_DrawText($hDC, " " & $sTitle, $tRect, $DT_CENTER + $DT_END_ELLIPSIS) ;~ _WinAPI_SelectObject($hDC, $hBrush_orig) ;~ _WinAPI_DeleteObject($hBrush) ;~ _WinAPI_SelectObject($hDC, $hPen_orig) ;~ _WinAPI_DeleteObject($hPen) _WinAPI_SelectObject($hDC, $hFont_orig) _WinAPI_DeleteObject($hFont) _WinAPI_ReleaseDC($hWnd, $hDC) EndSwitch Return $Res EndFunc ;==>_WinProc Func sizeof($struct) Return DllStructGetSize($struct) EndFunc ;==>sizeof Func _RemoveTheme($hCtrl) If Not IsHWnd($hCtrl) Then $hCtrl = GUICtrlGetHandle($hCtrl) If IsHWnd($hCtrl) Then Local $aR = DllCall("UxTheme.dll", "Long", "SetWindowTheme", "HWND", $hCtrl, "Ptr", 0, "WStr", " ") If Not @error And $aR[0] = 0 Then Return True EndIf Return False EndFunc ;==>_RemoveTheme Edit: I just saw, that on Win7 the coords for the RECT don't match. I will need time to work the right way on WinXP AND above. Edited July 3, 2013 by funkey Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning.
NewPlaza Posted September 11, 2013 Author Posted September 11, 2013 I decided to play around with some code to center text and came up with this. #include <WinAPI.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Local $pGUI = GUICreate("", -1, -1, -1, -1, $WS_BORDER) GUISetState() Local $pSize = WinGetPos($pGUI) Local $cGUI = GUICreate("", $pSize[2], $pSize[3], $pSize[0], $pSize[1], $WS_POPUP, $WS_EX_LAYERED, $pGUI) GUICtrlCreateLabel("THE TEXT IS CENTERED", 0, 0, $pSize[2], $pSize[3], $SS_CENTER) GUICtrlSetFont(-1, 10, 800) GUISetBkColor(0xABCDEF) GUICtrlCreateLabel("Some kind of text or whatever", 10, 30, 70) _WinAPI_SetLayeredWindowAttributes($cGUI, 0xABCDEF, 255) GUISetState() While 1 GUIGetMsg() WEnd I would like to know if there might be any issues I'm not aware of and for any WinXP/Vista users, I would like to know if it will even work. Thanks for viewing.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now