PDA

View Full Version : switching color background (gui)



largo_winch
10-01-2013, 20:20
here's a simple dialog with colour switching for background. Only the "mod" problem change for three time background, but perhaps anybody can fix this little problem. bye, largo


' Empty GUI script created on 01-10-2013 17:57:34 by (thinAir)
Uses "UI"


%ID_TIMER = 1000


Function TBMain() As Long
Local lRslt As Long
Local hDlg As DWord
Dialog New 0, "Switching Color Background", -1, -1, 260, 140, _
%WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPCHILDREN Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_MINIMIZEBOX, _
0 To hDlg
Dialog Show Modal hDlg, Call myColorProc
End Function


Function ChangeColor(ByVal hDlg As Long) As Long
Static static_counter As Double
Incr static_counter
If static_counter = Mod(1,2) Then 'Mod 2 = 0
Dialog Set Color hDlg, -1, %BLUE
Else
Dialog Set Color hDlg, -1, %RED
End If
Dialog Redraw hDlg
End Function


CallBack Function myColorProc()
Select Case CBMSG
Case %WM_INITDIALOG
Dialog Set Timer CBHNDL, %ID_Timer, 1000, 0
Case %WM_COMMAND
Case %WM_TIMER
ChangeColor(CBHNDL)
Case %WM_DESTROY
Function = 0
Dialog Kill Timer CBHNDL, %ID_TIMER
End Select
End Function

Petr Schreiber
10-01-2013, 21:14
Hi Largo_winch,

the "mod problem" is not a problem - let's have a look how to achieve what you need.

What is mod?
Mod = modulo = remainder of division of one number by another.

How does thinBASIC mod function work?
The function takes two arguments - dividend and divisor.

Why it doesn't work in the example?
You use Mod(1, 2). This will always result in constant.

You need to use it like Mod(static_counter, 2) -> this is "give me the remainder of static_counter / 2", and it will change in time.

So you can change the original:


If static_counter = Mod(1,2) Then


to


If Mod(static_counter, 2) Then


Switching of the two states could be easily implemented using Not:


Static static_counter As Long = FALSE

static_counter = Not static_counter ' -- If static_counter was false, it will be true & if static_counter was true it will be false - it is negation

If static_counter Then
Dialog Set Color hDlg, -1, %BLUE
Else
Dialog Set Color hDlg, -1, %RED
End If
Dialog Redraw hDlg


The counter will not grow to infinity at all - it will just switch from false to true and back.


Petr

largo_winch
11-01-2013, 11:30
You need to use it like Mod(static_counter, 2) -> this is "give me the remainder of static_counter / 2", and it will change in time.


that's it, many thanks petr and I've understood this way of programming. thanks for explanations. There were many attempt with mod(0,1), mod(1,1) and so on at my side, but there weren't perfect solution. I hope "mod" command will be my friend in next weeks and month ;) bye, largo