示例: 下面是个可运行脚本, 它演示了如何在脚本中通过把 GUI 窗口的WindowProc 重定向到新的 WindowProc 来子类化窗口. 此时, 文本控件的背景颜色被改变为自定义颜色.
; 示例: 下面是个可运行脚本, 它演示了如何在脚本中通过把 GUI 窗口的
; WindowProc 重定向到新的 WindowProc 来子类化窗口. 此时, 文本控件的
; 背景颜色被改变为自定义颜色.
TextBackgroundColor := 0xFFBBBB ; BGR 格式的自定义颜色.
TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor)
Gui, Add, Text, HwndMyTextHwnd, Here is some text that is given`na custom background color.
Gui +LastFound
GuiHwnd := WinExist()
; 64 位脚本必须调用 SetWindowLongPtr 代替 SetWindowLong:
SetWindowLong := A_PtrSize=8 ? "SetWindowLongPtr" : "SetWindowLong"
WindowProcNew := RegisterCallback("WindowProc", "" ; 指定 "" 来避免子类化中使用快速模式.
, 4, MyTextHwnd) ; 使用了 EventInfo 参数时必须明确指定 ParamCount.
WindowProcOld := DllCall(SetWindowLong, Ptr, GuiHwnd, Int, -4 ; -4 是 GWL_WNDPROC
, Ptr, WindowProcNew, Ptr) ; 返回值必须设置为 Ptr 或 UPtr 而不是 Int.
Gui Show
return
WindowProc(hwnd, uMsg, wParam, lParam)
{
Critical
global TextBackgroundColor, TextBackgroundBrush, WindowProcOld
if (uMsg = 0x138 && lParam = A_EventInfo) ; 0x138 为 WM_CTLCOLORSTATIC.
{
DllCall("SetBkColor", Ptr, wParam, UInt, TextBackgroundColor)
return TextBackgroundBrush ; 返回 HBRUSH 来通知操作系统我们改变了 HDC.
}
; 否则 (如果上面没有返回), 传递所有的未处理事件到原来的 WindowProc.
return DllCall("CallWindowProc", Ptr, WindowProcOld, Ptr, hwnd, UInt, uMsg, Ptr, wParam, Ptr, lParam)
}
GuiClose:
ExitApp
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

