Jump to content
Sign in to follow this  
funkey

Questions about NonClientArea

Recommended Posts

funkey

Hello!

I made a cool script to draw text to the caption bar, but I have some questions.

How do I get the position of the icon?

How do I get the position of the syscommand buttons (close, minimize, maximize)?

How can i avoid flickering while resizing the window?

There is a problem with drawing the standard title when there are 2 GUIs. Can this be fixed?

TIA

;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 = "This is a special caption! - This is a special caption! - This is a special caption!"
Global $sTitle2 = "Something interessting"
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)
Global $BorderWidth = _WinAPI_GetSystemMetrics(5)

Global $hForm1 = GUICreate($sTitle1, 400, 400, 100, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX))
_RemoveTheme($hForm1)
Global $nBtn1 = GUICtrlCreateButton("Switch-Button", 50, 50, 100, 30)
Global $hForm2 = GUICreate($sTitle2, 400, 400, 700, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX))
_RemoveTheme($hForm2)
Global $nBtn2 = GUICtrlCreateButton("Switch-Button", 50, 50, 100, 30)

_SpecialTitle($hForm1, 1, 0)

GUISetState(@SW_SHOW, $hForm1)
GUISetState(@SW_SHOW, $hForm2)

Local $msg
Do
    $msg = GUIGetMsg()
    If $msg = $nBtn1 Then
        _SpecialTitle($hForm1, 0)
        _SpecialTitleColor(0, 0xFFFFFF, 0xFFFF00, 0xFFFF00)
        _SpecialTitle($hForm2, 1)
    ElseIf $msg = $nBtn2 Then
        _SpecialTitle($hForm2, 0)
        _SpecialTitleColor(0, 0x0, 0x0, 0x0000FF)
        _SpecialTitle($hForm1, 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
        If $redraw Then _WinAPI_RedrawWindow($hWnd, 0, 0, BitOR(1024, 256, 1))
        $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)
        If $redraw Then _WinAPI_RedrawWindow($hWnd, 0, 0, BitOR(1024, 1))
        $TitleOn = 0
        $hHook = 0
        $pHook = 0
        $ghWndSpecialTitle = 0
    EndIf
EndFunc   ;==>_SpecialTitle

Func _SpecialTitleColor($iSwitch, $ColorText = 0x0, $ColorPen = 0x0, $ColorBrush = 0x0000FF)
    Static $sColorText = $ColorText, $sColorPen = $ColorPen, $sColorBrush = $ColorBrush
    Switch $iSwitch
        Case 0
            $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, $sTitle, $iRectRight
    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)
            DllStructSetData($tRect, "Left", 20)    ;how do I get this value?
            DllStructSetData($tRect, "Top", $BorderWidth + ($CaptionHeight - $CaptionFontSize) / 2)
            DllStructSetData($tRect, "Right", 0)
            DllStructSetData($tRect, "Bottom", 0)

            _WinAPI_DrawText($hDC, " " & $sTitle & " ", $tRect, $DT_CALCRECT)
            DllStructSetData($tRect, "Bottom", DllStructGetData($tRect, "Bottom") + 1)

            $iRectRight = WinGetPos($hWnd)
            $iRectRight = $iRectRight[2] - 58
            If DllStructGetData($tRect, "Right") > $iRectRight Then DllStructSetData($tRect, "Right", $iRectRight)  ;how do I get this value?

            _WinAPI_RoundRect($hDC, $tRect, 8, 8)

            _WinAPI_DrawText($hDC, " " & $sTitle, $tRect, $DT_LEFT + $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

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
UEZ

You can try to get the values from the system metrics values.

Example which might be incorrect to get the icon position and size:

#include <APIConstants.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>

Opt("MouseCoordMode", 0)
$hGUI = GUICreate("Test", 200, 100)
GUISetState()

$metricIconW = _WinAPI_GetSystemMetrics($SM_CXSMICON)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $metricIconW = ' & $metricIconW & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$metricIconH = _WinAPI_GetSystemMetrics($SM_CYSMICON)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $metricIconH = ' & $metricIconH & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$metricIconX = _WinAPI_GetSystemMetrics($SM_CXFRAME)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $metricIconX = ' & $metricIconX & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$metricIconY= _WinAPI_GetSystemMetrics($SM_CYFRAME)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $metricIconY = ' & $metricIconY & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console


Do
    $msg = GUIGetMsg()

Until $msg = $GUI_EVENT_CLOSE

System Metric Constants:

#include-once
Global Const $SM_ARRANGE = 56
Global Const $SM_CLEANBOOT = 67
Global Const $SM_CMONITORS = 80
Global Const $SM_CMOUSEBUTTONS = 43
Global Const $SM_CXBORDER = 5
Global Const $SM_CXCURSOR = 13
Global Const $SM_CXDLGFRAME = 7
Global Const $SM_CXDOUBLECLK = 36
Global Const $SM_CXDRAG = 68
Global Const $SM_CXEDGE = 45
Global Const $SM_CXFIXEDFRAME = 7
Global Const $SM_CXFOCUSBORDER = 83
Global Const $SM_CXFRAME = 32
Global Const $SM_CXFULLSCREEN = 16
Global Const $SM_CXHSCROLL = 21
Global Const $SM_CXHTHUMB = 10
Global Const $SM_CXICON = 11
Global Const $SM_CXICONSPACING = 38
Global Const $SM_CXMAXIMIZED = 61
Global Const $SM_CXMAXTRACK = 59
Global Const $SM_CXMENUCHECK = 71
Global Const $SM_CXMENUSIZE = 54
Global Const $SM_CXMIN = 28
Global Const $SM_CXMINIMIZED = 57
Global Const $SM_CXMINSPACING = 47
Global Const $SM_CXMINTRACK = 34
Global Const $SM_CXPADDEDBORDER = 92
Global Const $SM_CXSCREEN = 0
Global Const $SM_CXSIZE = 30
Global Const $SM_CXSIZEFRAME = 32
Global Const $SM_CXSMICON = 49
Global Const $SM_CXSMSIZE = 52
Global Const $SM_CXVIRTUALSCREEN = 78
Global Const $SM_CXVSCROLL = 2
Global Const $SM_CYBORDER = 6
Global Const $SM_CYCAPTION = 4
Global Const $SM_CYCURSOR = 14
Global Const $SM_CYDLGFRAME = 8
Global Const $SM_CYDOUBLECLK = 37
Global Const $SM_CYDRAG = 69
Global Const $SM_CYEDGE = 46
Global Const $SM_CYFIXEDFRAME = 8
Global Const $SM_CYFOCUSBORDER = 84
Global Const $SM_CYFRAME = 33
Global Const $SM_CYFULLSCREEN = 17
Global Const $SM_CYHSCROLL = 3
Global Const $SM_CYICON = 12
Global Const $SM_CYICONSPACING = 39
Global Const $SM_CYKANJIWINDOW = 18
Global Const $SM_CYMAXIMIZED = 62
Global Const $SM_CYMAXTRACK = 60
Global Const $SM_CYMENU = 15
Global Const $SM_CYMENUCHECK = 72
Global Const $SM_CYMENUSIZE = 55
Global Const $SM_CYMIN = 29
Global Const $SM_CYMINIMIZED = 58
Global Const $SM_CYMINSPACING = 48
Global Const $SM_CYMINTRACK = 35
Global Const $SM_CYSCREEN = 1
Global Const $SM_CYSIZE = 31
Global Const $SM_CYSIZEFRAME = 33
Global Const $SM_CYSMCAPTION = 51
Global Const $SM_CYSMICON = 50
Global Const $SM_CYSMSIZE = 53
Global Const $SM_CYVIRTUALSCREEN = 79
Global Const $SM_CYVSCROLL = 20
Global Const $SM_CYVTHUMB = 9
Global Const $SM_DBCSENABLED = 42
Global Const $SM_DEBUG = 22
Global Const $SM_DIGITIZER = 94
Global Const $SM_IMMENABLED = 82
Global Const $SM_MAXIMUMTOUCHES = 95
Global Const $SM_MEDIACENTER = 87
Global Const $SM_MENUDROPALIGNMENT = 40
Global Const $SM_MIDEASTENABLED = 74
Global Const $SM_MOUSEPRESENT = 19
Global Const $SM_MOUSEHORIZONTALWHEELPRESENT = 91
Global Const $SM_MOUSEWHEELPRESENT = 75
Global Const $SM_NETWORK = 63
Global Const $SM_PENWINDOWS = 41
Global Const $SM_REMOTECONTROL = 02001
Global Const $SM_REMOTESESSION = 01000
Global Const $SM_SAMEDISPLAYFORMAT = 81
Global Const $SM_SECURE = 44
Global Const $SM_SERVERR2 = 89
Global Const $SM_SHOWSOUNDS = 70
Global Const $SM_SHUTTINGDOWN = 02000
Global Const $SM_SLOWMACHINE = 73
Global Const $SM_STARTER = 88
Global Const $SM_SWAPBUTTON = 23
Global Const $SM_TABLETPC = 86
Global Const $SM_XVIRTUALSCREEN = 76

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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
Sign in to follow this  

×