Detect height of a window's title bar

穿精又带淫゛_ 提交于 2019-12-11 08:25:15

问题


I am using ClipCursor to lock a mouse in a window. How do I detect the height of the window's title bar and border of the window (so the only place the mouse can't click the title bar and the minimize, restore and maximize button)?

  • The height of the title bar depends on OS (I can't give a definite value for this).
  • I'm not sure if the borders have different widths with different operating systems.
  • I'm using windows XP on classic mode.
  • When I change to themed mode the height of the title bar changes so it won't work.
  • No specific language used.

回答1:


You can use AutoIt.

You don't have to deal with that specific titlebar height and border width. Instead try to set the MouseCoordMode to relative client position, move the mouse to the top/left position and get the window client size. This is the exact area you want your mouse to be trapped into.

These values can then be used in the _MouseTrap function. It should be similar to your ClipCursor().

The old mouse Position could be saved and restored but it won't make sense as your mouse might be repositioned into the trap field anyways so I commented this out.

#include <GuiConstantsEx.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)

_Main()

Func _Main()
    Local $GUI, $oldMouseCoordMode, $topLeft, $size ;,$oldMousePos

    $GUI = GUICreate("Example MouseTrap", 392, 323)
    GUISetBkColor( 0xff0000, $GUI)

    GUISetState()

;~  $oldMousePos = MouseGetPos()
    $oldMouseCoordMode = Opt("MouseCoordMode", 2)
    MouseMove(0, 0, 0)
    Opt("MouseCoordMode", 1)
    $topLeft = MouseGetPos()
;~  MouseMove($oldMousePos[0], $oldMousePos[1], 0)
    $size = WinGetClientSize($GUI)
    Opt("MouseCoordMode", $oldMouseCoordMode)
    _MouseTrap($topLeft[0], $topLeft[1], $topLeft[0] + $size[0], $topLeft[1] + $size[1])

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case Else
                ;;;
        EndSwitch
    WEnd
    _MouseTrap()
    Exit
EndFunc   ;==>_Main



回答2:


Would it work to get the window's client rect, rather than its full rect? I believe that will return the client area of the window, which is the window's rect minus the border and title bar.

If you go this route, you will need to convert the rect into screen coordinates before calling ClipCursor(), though.




回答3:


I just found out a more specific answer to your problem while browsing the AutoIt help. In the description of the function _WinAPI_CreateRectRgn() there is the following way to get the wished sizes:

#include <WinAPI.au3>

; get height of window title and width of window frame - may be different when
; XP theme is ON/OFF
Global $htit = _WinAPI_GetSystemMetrics($SM_CYCAPTION)
Global $frame = _WinAPI_GetSystemMetrics($SM_CXDLGFRAME)



回答4:


looks like

GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYSIZEFRAME) 

is the correct height of title bar




回答5:


One solution in AutoHotKey is to simply remove the bar! That will still allow people to use short cuts to manipulate the window though...

^F11:: ; Ctrl+F11 = Toggle show Window title bar
WinSet, Style, ^0xC00000, A  ; Toggle the active window's title bar (WS_CAPTION).
If (TopbarHide := !TopbarHide) ;
    ToolTip Topbar Ctrl F11,A_ScreenWidth/2-50,0
else
    Tooltip
Return


来源:https://stackoverflow.com/questions/4270920/detect-height-of-a-windows-title-bar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!