Что нового

GUICtrlSetBkColor как получить цвет кнопки?

saraconor

Новичок
Сообщения
404
Репутация
3
с помощь GUICtrlSetBkColor можно назначить цвет кнопки, а как можно получить этот цвет обратно?
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$1_ogange='0xFFEBCC'
$Form1 = GUICreate("Form1", 367, 202, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 16, 8, 273, 145)
GUICtrlSetBkColor($Button1, $1_ogange)
GUISetState(@SW_SHOW)

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $Button1
			MsgBox(0,'','цвет кнопки?')
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd

в данном случае желательно получить в окно сообщения 0xFFEBCC
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$1_ogange = '0xFFEBCC'

$Form1 = GUICreate("Form1", 367, 202, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 16, 8, 273, 145)
GUICtrlSetBkColor($Button1, $1_ogange)
GUISetState(@SW_SHOW)

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $Button1
			MsgBox(0, '', 'цвет кнопки?' & @CRLF & _ControlGetColor($Form1, '', $Button1))
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd

Func _ControlGetColor($sTitle, $sText, $iCtrlID)
	Local $hCtrl, $aCtrlPos, $iOpt, $nColor
	
	If Not WinExists($sTitle, $sText) Then
		Return SetError(1)
	EndIf
	
	$hCtrl = ControlGetHandle($sTitle, $sText, $iCtrlID)
	$aCtrlPos = ControlGetPos($sTitle, $sText, $iCtrlID)
	
	$iOpt = Opt("PixelCoordMode", 0)
	$nColor = PixelGetColor($aCtrlPos[0] + 1, $aCtrlPos[1] + 1, $hCtrl)
	Opt("PixelCoordMode", $iOpt)
	
	Return '0x' & Hex($nColor, 6)
EndFunc
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,322
OffTopic:
Не понимаю, почему _WinAPI_GetBkColor() у окна возвращает правильный цвет, а у кнопки нет.
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Или так:

Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIEx.au3>

$1_ogange = '0xFFEBCC'

$Form1 = GUICreate("Form1", 367, 202, 192, 124)

$Button1 = GUICtrlCreateButton("Button1", 16, 8, 273, 145)
$hBttnDC1 = _GUICtrlSetBkColorEx($Button1, $1_ogange)

GUISetState(@SW_SHOW, $Form1)

While 1
	$nMsg = GUIGetMsg()
	
	Switch $nMsg
		Case $Button1
			MsgBox(0, '', 'цвет кнопки?' & @CRLF & '0x' & Hex(_WinAPI_GetBkColor($hBttnDC1), 6))
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd

Func _GUICtrlSetBkColorEx($CtrlID, $iColor)
	GUICtrlSetBkColor($CtrlID, $iColor)
	Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($CtrlID))
	_WinAPI_SetBkColor($hDC, '0x' & StringRight($iColor, 2) & StringMid($iColor, 5, 2) & StringMid($iColor, 3, 2))
	Return $hDC
EndFunc


:
madmasles [?]
почему _WinAPI_GetBkColor() у окна возвращает правильный цвет, а у кнопки нет.
Потому что элементу нужно задавать цвет через _WinAPI_SetBkColor.
 
Автор
S

saraconor

Новичок
Сообщения
404
Репутация
3
CreatoR Спасибо, все работает только мне как новичку сложно разобраться и сам код получился громоздким, мне вот так оказалось удобнее:
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$1_ogange='0xFFEBCC'
$Form1 = GUICreate("Form1", 367, 202, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 16, 8, 273, 145)
GUICtrlSetBkColor($Button1, $1_ogange)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
		Case $Button1
			MsgBox(0,'','0x'&Hex(PixelGetColor(MouseGetPos(0),MouseGetPos(1))))
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
 
Автор
S

saraconor

Новичок
Сообщения
404
Репутация
3
madmasles сказал(а):
А если курсор на тексте?
Тогда наверное так:
Код:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>

$Test = '0x00ABFASS'
$1_ogange='0xFFEBCC'
$Form1 = GUICreate("Form1", 367, 202, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 16, 8, 273, 145)
GUICtrlSetFont(-1, 22, 800, 2, "Arial Black")
GUICtrlSetBkColor($Button1, $1_ogange)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
		Case $Button1
			_GUICtrlButton_SetText($Button1,'')
			$ORI = '0x'&Hex(PixelGetColor(MouseGetPos(0),MouseGetPos(1)))
			_GUICtrlButton_SetText($Button1,'Button1')
			if $ORI = 0x00FFEBCC then
			ToolTip('кнопка нажата')
			sleep(200)
			ToolTip('')
			endif
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Перед проверкой цвета убираем текст. Все равно кнопка мерцает при нажатии хоть с убиранием текста хоть без (кстати почему?)
 
Автор
S

saraconor

Новичок
Сообщения
404
Репутация
3

Yashied

Модератор
Команда форума
Глобальный модератор
Сообщения
5,379
Репутация
2,724
madmasles сказал(а):
Не понимаю, почему _WinAPI_GetBkColor() у окна возвращает правильный цвет, а у кнопки нет.
Эта функция возвращает цвет фона DC. Представьте себе лист зеленой бумаги, это и есть DC. _WinAPI_GetBkColor() в этом случае вернет зеленый. Теперь, даже, если вы нарисуете красную кнопку размером с этот лист, то _WinAPI_GetBkColor() все равно вернет зеленый, т.к. цвет самого листа (DC) вы не меняли.

saraconor, логичнее просто запоминать тот цвет, который был установлен для кнопки, т.к. красивого решения здесь нет.
 
Верх