<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Динамическое изменение цвета фона и текста в static]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=11424&amp;type=atom" />
	<updated>2016-03-17T21:36:57Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=11424</id>
		<entry>
			<title type="html"><![CDATA[AHK: Динамическое изменение цвета фона и текста в static]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=101917#p101917" />
			<content type="html"><![CDATA[<p>Код создан на основе <a href="https://autohotkey.com/docs/commands/RegisterCallback.htm#Examples">примера</a> сабклассинга окна. Задача изменения цвета фона и текста в контроле <a href="https://msdn.microsoft.com/en-us/library/bb760769%28v=vs.85%29.aspx"><em>static</em></a> (в AHK — <a href="https://autohotkey.com/docs/commands/GuiControls.htm#Text">Text</a>) оформлена в виде функции и может выполняться динамически, в любой момент.<br /></p><div class="codebox"><pre><code>Gui, Add, Text, hwndhText1 w150 center, Первый текст
Gui, Add, Text, hwndhText2 wp center, Второй текст
SetStaticColor(hText1     ; хэндл контрола static (text), цвет которого нужно изменить
					, 0xFF0000   ; цвет фона, RGB, красный
					, 0x0000FF)  ; цвет текста, RGB, синий
SetStaticColor(hText2, 0x0000FF, 0xFFFFFF)
Gui, Show
Return

F11::
	Loop 2  {
		Random, Rand, 0, 0xFFFFFF
		SetStaticColor(hText%A_Index%, Rand, Rand^0xFFFFFF)
	}
	Return

SetStaticColor(hStatic, b_color, f_color)
{
	static arr := [], GWL_WNDPROC := -4
	hGui := DllCall(&quot;GetParent&quot;, Ptr, hStatic, Ptr)
	if !arr.HasKey(hGui)  {
		arr[hGui] := {}, arr[hGui].Statics := []
		arr[hGui].ProcOld := DllCall(&quot;SetWindowLong&quot; . (A_PtrSize = 8 ? &quot;Ptr&quot; : &quot;&quot;), Ptr, hGui, Int, GWL_WNDPROC
												, Ptr, RegisterCallback(&quot;WindowProc&quot;, &quot;&quot;, 4, Object(arr[hGui])), Ptr)
	}
	else if arr[hGui].Statics.HasKey(hStatic)
		DllCall(&quot;DeleteObject&quot;, Ptr, arr[hGui].Statics[hStatic].hBrush)
	arr[hGui].Statics[hStatic] := { b_color: Rgb2Bgr(b_color), f_color: Rgb2Bgr(f_color)
											, hBrush: DllCall(&quot;CreateSolidBrush&quot;, UInt, Rgb2Bgr(b_color), Ptr) }
	WinSet, Redraw,, ahk_id %hGui%
}

WindowProc(hwnd, uMsg, wParam, lParam)
{
	Critical
	static WM_CTLCOLORSTATIC := 0x138
	obj := Object(A_EventInfo)
	if (uMsg = WM_CTLCOLORSTATIC &amp;&amp; k := obj.Statics[lParam])  {
		DllCall(&quot;SetBkColor&quot;, Ptr, wParam, UInt, k.b_color)
		DllCall(&quot;SetTextColor&quot;, Ptr, wParam, UInt, k.f_color)
		Return k.hBrush
	}
	Return DllCall(&quot;CallWindowProc&quot;, Ptr, obj.ProcOld, Ptr, hwnd, UInt, uMsg, Ptr, wParam, Ptr, lParam)
}
	
Rgb2Bgr(color)
{
	Return (color &amp; 0xff) &lt;&lt; 16 | ((color &gt;&gt; 8) &amp; 0xff) &lt;&lt; 8 | color &gt;&gt; 16
}</code></pre></div><p>При нажатии горячей клавиши F11 цвет фона и текста будет изменяться рандомно.</p><p><a href="http://forum.script-coding.com/viewtopic.php?id=11137"><strong>Связанная тема</strong></a></p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2016-03-17T21:36:57Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=101917#p101917</id>
		</entry>
</feed>
