View Full Version : button background color change
sandyrepope
08-07-2007, 04:41
I've been trying to change the background color of a button using control set color and then control redraw but it doesn't seem to work. Other controls will change background color but not button.
Can the background color of a button be changed?
Thanks
Sandy
sandyrepope
08-07-2007, 05:24
Here is a sample of what I've been trying:
uses "UI"
dim hDlg AS long
dim Msg AS LONG
dim wParam AS LONG
dim lParam AS LONG
%ID_BUTTON = 100
DIALOG NEW 0, "Month Calendar", -1, -1, 300, 300, _
%WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, _
0 TO hDlg
control add button, hDlg, %ID_BUTTON, "Hello", 10, 10, 43, 43
'msgbox 0, "button added"
DIALOG SHOW modeless hDlg
WHILE IsWindow(hDlg)
'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)
'---Now test the message
SELECT CASE Msg
case %WM_INITDIALOG '---Message fired at the very beginning when dialog is initialized
'---This can be the right place to initialize dialog or create controls
' SELECT CASE Msg
case %WM_COMMAND
select case wParam
case %ID_BUTTON
CHANGE
END SELECT
CASE %WM_SYSCOMMAND
select case wParam
CASE %SC_CLOSE
EXIT WHILE
end select
CASE %WM_SYSCOMMAND
SELECT CASE wParam
CASE %SC_CLOSE
EXIT WHILE
END SELECT
case else
end select
wend
DIALOG END hDlg
function change()
MSGBOX 0, "changing button background color"
control set color hDlg, %ID_BUTTON, rgb(255,0,0), rgb(0,0,255)
control redraw hDlg, %ID_BUTTON
end function
Petr Schreiber
08-07-2007, 08:53
Hi,
buttons behave different regarding colors ( also LINE I think ), it is behaviour of Windows design. I think only way to paint them is to handle WM_PAINT message or something similar.
Here is cheap workaround, using static control as button, not great, but as temporary solution ... :)
uses "UI"
dim hDlg AS long
dim Msg AS LONG
dim wParam AS LONG
dim lParam AS LONG
%ID_BUTTON = 100
DIALOG NEW 0, "Month Calendar", -1, -1, 300, 300, _
%WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, _
0 TO hDlg
control add label, hDlg, %ID_BUTTON, "Hello", 10, 10, 43, 43, %SS_NOTIFY or %SS_CENTER or %SS_CENTERIMAGE or %SS_SUNKEN
'msgbox 0, "button added"
DIALOG SHOW modeless hDlg
WHILE IsWindow(hDlg)
'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)
'---Now test the message
SELECT CASE Msg
case %WM_INITDIALOG '---Message fired at the very beginning when dialog is initialized
'---This can be the right place to initialize dialog or create controls
' SELECT CASE Msg
case %WM_COMMAND
select case wParam
case %ID_BUTTON
CHANGE
END SELECT
CASE %WM_SYSCOMMAND
select case wParam
CASE %SC_CLOSE
EXIT WHILE
end select
CASE %WM_SYSCOMMAND
SELECT CASE wParam
CASE %SC_CLOSE
EXIT WHILE
END SELECT
end select
wend
DIALOG END hDlg
function change()
MSGBOX 0, "changing button background color"
control set color hDlg, %ID_BUTTON, rgb(255,0,0), rgb(0,0,255)
control redraw hDlg, %ID_BUTTON
end function
Bye,
Petr
ErosOlmi
08-07-2007, 10:04
Yes, Petr is right.
No way to change color to buttons without using a technique called "subclassing". Maily you have to substitute the procedure associated with a contro with a new one, but this is not possible in thinBasic because thinBasic has no procedures stored in binary compiled format but as script.
Another way is to pass %BS_OWNERDRAW style to button when creating and than manage %WM_DRAWITEM message inside the big WHILE/WEND loop. At that time we need to draw by code the button. Problem here is that this will be fired so many times per seconds that your script will mainly just do that job.
I will try to do something internally but not sure I can do it.
Eros
Petr, you always figure out a good solution. I stumbled with this today and couldn't find an answer.
I like your example, one thing this makes it seem more like a button to me ... well I had to add something :)
control add label, hDlg, %ID_BUTTON, "Hello", 10, 10, 43, 43, %SS_NOTIFY or %SS_CENTER or %SS_CENTERIMAGE or %WS_BORDER
my big change is in bold, he he he
Petr Schreiber
08-07-2007, 14:58
Hi kryton,
nice tweak :D
Petr
sandyrepope
08-07-2007, 18:04
Wow! I never would have thought of using a static label like that. Thank you for the example! I plan to use this instead of buttons in my script.
Thanks again.
Sandy
ErosOlmi
08-07-2007, 18:27
Attention.
The trick is to add %SS_NOTIFY style into the label declaration otherwise window message pump will not be notified about the click.
Ciao
Eros