Static color

Windows specific forum
ly47
User
User
Posts: 23
Joined: Mon May 05, 2014 6:51 pm

Static color

Post by ly47 »

Hi

Please help, window does not update colors properly.
click test-1 or test-2 than drag window left out of screen and drag it back, colors will be all same (as static-3).
How to fix it ?

thanks
ly

Code: Select all


Enumeration
	#ID_BT1 = 1001
	#ID_BT2
	#ID_BT3
	#ID_ST1
	#ID_ST2
	#ID_ST3
EndEnumeration

Global.l hInstance

hInstance = GetModuleHandle_( null )

Global.l hSt1,hSt2,hSt3,hBtn1,hBtn2,hBtn3
Global.l ColorText,ColorBkg
Global.l hBrush

Declare WndProc(hWnd, uMsg, wParam, lParam) 
Declare XHiWord(a.l) 
Declare XLoWord(a.l) 

Global Appname.s,Caption.s
Appname = "Win32" 
Caption = "Simple Window"

Global wc.WNDCLASS 
wc\style          =  #CS_VREDRAW | #CS_HREDRAW 
wc\lpfnWndProc    =  @WndProc() 
wc\cbClsExtra     =  0 
wc\cbWndExtra     =  0 
wc\hInstance      =  hInstance 
wc\hIcon          =  LoadIcon_(hInstance, "#1") 
wc\hCursor        =  LoadCursor_(0, #IDC_ARROW) 
wc\hbrBackground  =  CreateSolidBrush_(GetSysColor_(15)) 
wc\lpszMenuName   =  0 
wc\lpszClassName  =  @Appname 

RegisterClass_(wc) 

hWnd = CreateWindowEx_(0,Appname,Caption,
                       #WS_OVERLAPPEDWINDOW | #WS_VISIBLE ,
                       100, 100, 600, 400,
                       0,0,wc\hInstance,0) 

UpdateWindow_(hWnd) 
ShowWindow_(hWnd,#SW_SHOWNORMAL) 
SetForegroundWindow_(hWnd) 

While GetMessage_(m.MSG, 0, 0, 0) 
	TranslateMessage_(m) 
	DispatchMessage_(m) 
Wend 

Procedure WndProc(hWnd,uMsg,wParam,lParam) 
	Select uMsg
		Case #WM_CREATE
			hBtn1 = CreateWindowEx_( 0,
			                         "button", "Exit", 
			                         #WS_VISIBLE|#WS_CHILD, 
			                         520, 328, 60, 25,
			                         hWnd, #ID_BT1, hInstance, null )
			
			hBtn2 = CreateWindowEx_( 0, 
			                         "button", "Test-1",  
			                         #WS_VISIBLE|#WS_CHILD,  
			                         440, 328, 60, 25,  
			                         hWnd,#ID_BT2, hInstance, null )
			
			hBtn3 = CreateWindowEx_( 0,  
			                         "button", "Test-2",  
			                         #WS_VISIBLE|#WS_CHILD,  
			                         360, 328, 60, 25,  
			                         hWnd,# ID_BT3, hInstance, null )
			
			hSt1 = CreateWindowEx_( 0, 
			                        "static", "st1",  
			                        #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY,  
			                        5, 5, 40, 60,  
			                        hWnd, #ID_ST1, hInstance, null )
			
			hSt2 = CreateWindowEx_( 0,  
			                        "static", "st2",  
			                        #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY,  
			                        5, 70, 40, 60,  
			                        hWnd, #ID_ST2, hInstance, null )
			hSt3 = CreateWindowEx_( 0,  
			                        "static", "st3",  
			                        #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY,  
			                        5, 135, 40, 60,  
			                        hWnd,#ID_ST3, hInstance, null )
			
		Case #WM_COMMAND 
			
			Select wParam
				Case #BN_CLICKED << 16 | #ID_BT1
					
					PostQuitMessage_(0)
					
				Case #BN_CLICKED << 16 | #ID_BT2
					
					ColorText = RGB(0,255,255)
					ColorBkg = RGB(255,0,0)
					InvalidateRect_(hSt1,NULL,TRUE)
					UpdateWindow_(hSt1)
					
					ColorText = RGB(255,0,255)
					ColorBkg = RGB(0,255,0)
					InvalidateRect_(hSt2,NULL,TRUE)
					UpdateWindow_(hSt2)
					
					ColorText = RGB(255,255,0)
					ColorBkg = RGB(0,0,255)
					InvalidateRect_(hSt3,NULL,TRUE)
					UpdateWindow_(hSt3)
					
				Case #BN_CLICKED << 16 | #ID_BT3
					
					ColorText = RGB(0,0,255)
					ColorBkg = RGB(255,255,0)
					InvalidateRect_(hSt1,NULL,TRUE)
					UpdateWindow_(hSt1)
					
					ColorText = RGB(255,0,0)
					ColorBkg = RGB(0,255,255)
					InvalidateRect_(hSt2,NULL,TRUE)
					UpdateWindow_(hSt2)
					
					ColorText = RGB(0,255,0)
					ColorBkg = RGB(255,0,255)
					InvalidateRect_(hSt3,NULL,TRUE)
					UpdateWindow_(hSt3)
					
			EndSelect
			
		Case #WM_CTLCOLORSTATIC	
			
			SetTextColor_(wParam,ColorText)
			SetBkColor_(wParam,ColorBkg)
			
			hBrush = CreateSolidBrush_(ColorBkg)
			
			ProcedureReturn hBrush
			
		Case WM_DESTROY 
			PostQuitMessage_( 0 )	
			
	EndSelect	
	
	ProcedureReturn  DefWindowProc_(hWnd, uMsg, wParam, lParam)
	
EndProcedure

Procedure XHiWord(a.l) 
	ProcedureReturn Int(a / $10000) 
EndProcedure 

Procedure XLoWord(a.l) 
	ProcedureReturn Int(a - (Int(a/$10000)*$10000)) 
EndProcedure
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Static color

Post by RASHAD »

Hi ly47
It will be much easier using PureBasic Native commands
But anyway here it is

Code: Select all

Enumeration
   #ID_BT1 = 1001
   #ID_BT2
   #ID_BT3
   #ID_ST1
   #ID_ST2
   #ID_ST3
EndEnumeration

Global.l hInstance,Clicked

hInstance = GetModuleHandle_( null )

Global.l hSt1,hSt2,hSt3,hBtn1,hBtn2,hBtn3
Global.l ColorText,ColorBkg
Global.l hBrush

Declare WndProc(hWnd, uMsg, wParam, lParam)
Declare XHiWord(a.l)
Declare XLoWord(a.l)

Global Appname.s,Caption.s
Appname = "Win32"
Caption = "Simple Window"

Global wc.WNDCLASS
wc\style          =  #CS_VREDRAW | #CS_HREDRAW
wc\lpfnWndProc    =  @WndProc()
wc\cbClsExtra     =  0
wc\cbWndExtra     =  0
wc\hInstance      =  hInstance
wc\hIcon          =  LoadIcon_(hInstance, "#1")
wc\hCursor        =  LoadCursor_(0, #IDC_ARROW)
wc\hbrBackground  =  CreateSolidBrush_(GetSysColor_(15))
wc\lpszMenuName   =  0
wc\lpszClassName  =  @Appname

RegisterClass_(wc)

hWnd = CreateWindowEx_(0,Appname,Caption,
                       #WS_OVERLAPPEDWINDOW | #WS_VISIBLE ,
                       100, 100, 600, 400,
                       0,0,wc\hInstance,0)

UpdateWindow_(hWnd)
ShowWindow_(hWnd,#SW_SHOWNORMAL)
SetForegroundWindow_(hWnd)

While GetMessage_(m.MSG, 0, 0, 0)
   TranslateMessage_(m)
   DispatchMessage_(m)
Wend

Procedure WndProc(hWnd,uMsg,wParam,lParam)
   Select uMsg
      Case #WM_EXITSIZEMOVE
 							PostMessage_(GetDlgItem_(hWnd,Clicked), #BM_CLICK, 0, 0)
                
      Case #WM_CREATE
         hBtn1 = CreateWindowEx_( 0,
                                  "button", "Exit",
                                  #WS_VISIBLE|#WS_CHILD,
                                  520, 328, 60, 25,
                                  hWnd, #ID_BT1, hInstance, null )
         
         hBtn2 = CreateWindowEx_( 0,
                                  "button", "Test-1", 
                                  #WS_VISIBLE|#WS_CHILD, 
                                  440, 328, 60, 25, 
                                  hWnd,#ID_BT2, hInstance, null )
         
         hBtn3 = CreateWindowEx_( 0, 
                                  "button", "Test-2", 
                                  #WS_VISIBLE|#WS_CHILD, 
                                  360, 328, 60, 25, 
                                  hWnd,# ID_BT3, hInstance, null )
         
         hSt1 = CreateWindowEx_( 0,
                                 "static", "st1", 
                                 #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY, 
                                 5, 5, 40, 60, 
                                 hWnd, #ID_ST1, hInstance, null )
         
         hSt2 = CreateWindowEx_( 0, 
                                 "static", "st2", 
                                 #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY, 
                                 5, 70, 40, 60, 
                                 hWnd, #ID_ST2, hInstance, null )
         hSt3 = CreateWindowEx_( 0, 
                                 "static", "st3", 
                                 #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY, 
                                 5, 135, 40, 60, 
                                 hWnd,#ID_ST3, hInstance, null )
         
      Case #WM_COMMAND
         
         Select wParam
            Case #BN_CLICKED << 16 | #ID_BT1
               
               PostQuitMessage_(0)
               
            Case #BN_CLICKED << 16 | #ID_BT2
               Clicked = #BN_CLICKED << 16 | #ID_BT2
               ColorText = RGB(0,255,255)
               ColorBkg = RGB(255,0,0)
               InvalidateRect_(hSt1,#Null,#True)
               UpdateWindow_(hSt1)
               
               ColorText = RGB(255,0,255)
               ColorBkg = RGB(0,255,0)
               InvalidateRect_(hSt2,#Null,#True)
               UpdateWindow_(hSt2)
               
               ColorText = RGB(255,255,0)
               ColorBkg = RGB(0,0,255)
               InvalidateRect_(hSt3,#Null,#True)
               UpdateWindow_(hSt3)

               
            Case #BN_CLICKED << 16 | #ID_BT3
               Clicked = #BN_CLICKED << 16 | #ID_BT3
               ColorText = RGB(0,0,255)
               ColorBkg = RGB(255,255,0)
               InvalidateRect_(hSt1,#Null,#True)
               UpdateWindow_(hSt1)
               
               ColorText = RGB(255,0,0)
               ColorBkg = RGB(0,255,255)
               InvalidateRect_(hSt2,#Null,#True)
               UpdateWindow_(hSt2)
               
               ColorText = RGB(0,255,0)
               ColorBkg = RGB(255,0,255)
               InvalidateRect_(hSt3,#Null,#True)
               UpdateWindow_(hSt3)
               
         EndSelect
         
      Case #WM_CTLCOLORSTATIC
         
         SetTextColor_(wParam,ColorText)
         SetBkColor_(wParam,ColorBkg)
         
         hBrush = CreateSolidBrush_(ColorBkg)
         
         ProcedureReturn hBrush
         
      Case #WM_DESTROY
         PostQuitMessage_( 0 )   
         
   EndSelect   
   
   ProcedureReturn  DefWindowProc_(hWnd, uMsg, wParam, lParam)
   
EndProcedure

Procedure XHiWord(a.l)
   ProcedureReturn Int(a / $10000)
EndProcedure

Procedure XLoWord(a.l)
   ProcedureReturn Int(a - (Int(a/$10000)*$10000))
EndProcedure
Egypt my love
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Static color

Post by netmaestro »

A couple of things:

1) NULL and TRUE are both 0 in Purebasic as they have no native meaning. Use #Null and #True.

2) You are setting background colors for each static control in the buttonpress handler but not in the #WM_CTLCOLORSTATIC handler. So there it just uses the last background color that was set for all the static controls. You need something like:

Code: Select all

Case #WM_CTLCOLORSTATIC
  Select lparam
    Case hSt1
      ; choose a brush
      ; return the brush
    Case hSt2
      [..]
3) You are doing this each time the #WM-CTLCOLORSTATIC message is processed: hBrush = CreateSolidBrush_(ColorBkg) which is a large memory leak. If many random colors are going to be used, use DeleteObject_() on the previous color before creating a brush for the next color. If the colors are few and known ahead of time, use a global array or such, create them once at program start and just return the one you want each time.
BERESHEIT
ly47
User
User
Posts: 23
Joined: Mon May 05, 2014 6:51 pm

Re: Static color

Post by ly47 »

Hi Rashad
it works only after releasing the mouse.(mouse up)
It should work even mouse is not released(mouse down)
can you improve your code please.
ly


Hi netmaestro
Thanks

ly
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Static color

Post by RASHAD »

Welcome aboard

Code: Select all

Enumeration
   #ID_BT1 = 1001
   #ID_BT2
   #ID_BT3
   #ID_ST1
   #ID_ST2
   #ID_ST3
EndEnumeration

Global.l hInstance,Clicked

hInstance = GetModuleHandle_( null )

Global.l hSt1,hSt2,hSt3,hBtn1,hBtn2,hBtn3
Global.l ColorText,ColorBkg
Global.l hBrush

Declare WndProc(hWnd, uMsg, wParam, lParam)
Declare XHiWord(a.l)
Declare XLoWord(a.l)

Global Appname.s,Caption.s
Appname = "Win32"
Caption = "Simple Window"

Global wc.WNDCLASS
wc\style          =  #CS_VREDRAW | #CS_HREDRAW
wc\lpfnWndProc    =  @WndProc()
wc\cbClsExtra     =  0
wc\cbWndExtra     =  0
wc\hInstance      =  hInstance
wc\hIcon          =  LoadIcon_(hInstance, "#1")
wc\hCursor        =  LoadCursor_(0, #IDC_ARROW)
wc\hbrBackground  =  CreateSolidBrush_(GetSysColor_(15))
wc\lpszMenuName   =  0
wc\lpszClassName  =  @Appname

RegisterClass_(wc)

hWnd = CreateWindowEx_(0,Appname,Caption,
                       #WS_OVERLAPPEDWINDOW | #WS_VISIBLE ,
                       100, 100, 600, 400,
                       0,0,wc\hInstance,0)

UpdateWindow_(hWnd)
ShowWindow_(hWnd,#SW_SHOWNORMAL)
SetForegroundWindow_(hWnd)

While GetMessage_(m.MSG, 0, 0, 0)
   TranslateMessage_(m)
   DispatchMessage_(m)
Wend

Procedure WndProc(hWnd,uMsg,wParam,lParam)
   Select uMsg
       Case #WM_MOVE,#WM_MOVING,#WM_SIZE,#WM_SIZING
                If Clicked = 1002
				               ColorText = RGB(0,255,255)
				               ColorBkg = RGB(255,0,0)
				               UpdateWindow_(hSt1)
				               
				               ColorText = RGB(255,0,255)
				               ColorBkg = RGB(0,255,0)
				               UpdateWindow_(hSt2)
				               
				               ColorText = RGB(255,255,0)
				               ColorBkg = RGB(0,0,255)
				               UpdateWindow_(hSt3)
				               
				        ElseIf Clicked = 1003
				               ColorText = RGB(0,0,255)
				               ColorBkg = RGB(255,255,0)
				               UpdateWindow_(hSt1)
				               
				               ColorText = RGB(255,0,0)
				               ColorBkg = RGB(0,255,255)
				               UpdateWindow_(hSt2)
				               
				               ColorText = RGB(0,255,0)
				               ColorBkg = RGB(255,0,255)
				               UpdateWindow_(hSt3)
				        EndIf
               
      Case #WM_CREATE
         hBtn1 = CreateWindowEx_( 0,
                                  "button", "Exit",
                                  #WS_VISIBLE|#WS_CHILD,
                                  520, 328, 60, 25,
                                  hWnd, #ID_BT1, hInstance, #Null )
         
         hBtn2 = CreateWindowEx_( 0,
                                  "button", "Test-1",
                                  #WS_VISIBLE|#WS_CHILD,
                                  440, 328, 60, 25,
                                  hWnd,#ID_BT2, hInstance, #Null )
         
         hBtn3 = CreateWindowEx_( 0,
                                  "button", "Test-2",
                                  #WS_VISIBLE|#WS_CHILD,
                                  360, 328, 60, 25,
                                  hWnd,# ID_BT3, hInstance, #Null )
         
         hSt1 = CreateWindowEx_( 0,
                                 "static", "st1",
                                 #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY,
                                 5, 5, 40, 60,
                                 hWnd, #ID_ST1, hInstance, #Null )
         
         hSt2 = CreateWindowEx_( 0,
                                 "static", "st2",
                                 #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY,
                                 5, 70, 40, 60,
                                 hWnd, #ID_ST2, hInstance, #Null )
         hSt3 = CreateWindowEx_( 0,
                                 "static", "st3",
                                 #WS_VISIBLE|#WS_CHILD|#SS_SUNKEN|#SS_NOTIFY,
                                 5, 135, 40, 60,
                                 hWnd,#ID_ST3, hInstance, #Null )
         
      Case #WM_COMMAND
         
         Select wParam
            Case #BN_CLICKED << 16 | #ID_BT1
               
               PostQuitMessage_(0)
               
            Case #BN_CLICKED << 16 | #ID_BT2
               Clicked = 1002
               ColorText = RGB(0,255,255)
               ColorBkg = RGB(255,0,0)
               InvalidateRect_(hSt1,#Null,#True)
               UpdateWindow_(hSt1)
               
               ColorText = RGB(255,0,255)
               ColorBkg = RGB(0,255,0)
               InvalidateRect_(hSt2,#Null,#True)
               UpdateWindow_(hSt2)
               
               ColorText = RGB(255,255,0)
               ColorBkg = RGB(0,0,255)
               InvalidateRect_(hSt3,#Null,#True)
               UpdateWindow_(hSt3)

               
            Case #BN_CLICKED << 16 | #ID_BT3
               Clicked = 1003
               ColorText = RGB(0,0,255)
               ColorBkg = RGB(255,255,0)
               InvalidateRect_(hSt1,#Null,#True)
               UpdateWindow_(hSt1)
               
               ColorText = RGB(255,0,0)
               ColorBkg = RGB(0,255,255)
               InvalidateRect_(hSt2,#Null,#True)
               UpdateWindow_(hSt2)
               
               ColorText = RGB(0,255,0)
               ColorBkg = RGB(255,0,255)
               InvalidateRect_(hSt3,#Null,#True)
               UpdateWindow_(hSt3)
               
         EndSelect
         
      
      Case #WM_CTLCOLORSTATIC
         SetTextColor_(wParam,ColorText)
         SetBkColor_(wParam,ColorBkg)
         
          hBrush = CreateSolidBrush_(ColorBkg)
         
          ProcedureReturn hBrush
         
      Case #WM_DESTROY
         PostQuitMessage_( 0 )   
         
   EndSelect   
   
   ProcedureReturn  DefWindowProc_(hWnd, uMsg, wParam, lParam)
   
EndProcedure

Procedure XHiWord(a.l)
   ProcedureReturn Int(a / $10000)
EndProcedure

Procedure XLoWord(a.l)
   ProcedureReturn Int(a - (Int(a/$10000)*$10000))
EndProcedure
Egypt my love
ly47
User
User
Posts: 23
Joined: Mon May 05, 2014 6:51 pm

Re: Static color

Post by ly47 »

Hi RASHAD

Now it is complete !!! :D :D

Thanks a lot
ly
Post Reply