Page 1 of 1

What is wrong? I don't get it!

Posted: Mon Dec 10, 2007 9:50 am
by rjara
Hi All,

I'm experimenting with bits of code from here and there. So I put this code together expecting to see all the info in the designated string gadgets yet the first string gadget seem to be updated and everything else appart form the box is not been refreshed. Any Idea why. Have i missed something? :?

Yes I'm new to purebasic, but love it!

Code: Select all

;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #String_0
  #String_1
  #String_2
  #String_3
  #String_4
  #Frame3D_5
  #Button_6
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
WindowDC.l = GetWindowDC_(#Null)
Global CPosX=2
Global CPosY=2
Global Width=80
Global Height=95

Procedure UpdateColor(Colour.l)
  RedComponent.l = Red(Colour)
  GreenComponent.l = Green(Colour)
  BlueComponent.l = Blue(Colour)
  ;SetGadgetColor(#Frame3D_5,#PB_Gadget_BackColor,RGB(RedComponent,GreenComponent,BlueComponent))
  Box(CPosX+3,CPosY+3,Width-6,Height-6,RGB(RedComponent,GreenComponent,BlueComponent))
EndProcedure

Procedure OpenWindow_Window_0()
  GPosX=85
  GposY=2
  GWidth=180
  GHeight=20
  Gspace=25
  If OpenWindow(#Window_0, 450, 197, 268, 170, "Pick Color", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(#Window_0))
      ;StringGadget(#String_0, 1,1,290,20,"", #PB_String_ReadOnly);MOUSE COORDS
      StringGadget(#String_1, GPosX, GposY, GWidth, GHeight, "", #PB_String_ReadOnly);Mouse Coords
      StringGadget(#String_2, GPosX, GposY+Gspace, GWidth, GHeight, "", #PB_String_ReadOnly);RGB
      StringGadget(#String_3, GPosX,GposY+Gspace*2, GWidth, GHeight, "", #PB_String_ReadOnly);hex
      StringGadget(#String_4, GPosX, GposY+Gspace*3, GWidth, GHeight,"", #PB_String_ReadOnly);Window handle under mouse
      Frame3DGadget(#Frame3D_5,CPosX , CPosY, Width, Height , "", #PB_Frame3D_Double)
      ButtonGadget(#Button_6, 89, 120, 85, 40, "Close")
    EndIf
  EndIf
EndProcedure

OpenWindow_Window_0()

Repeat
  
  
  GetCursorPos_(Coords.POINT) ;get x/y position of mouse into point structure
  wDC = WindowFromPoint_ (Coords\x, Coords\y) ;get the windows handle under the mouse
  Colour = GetPixel_(WindowDC, Coords\x, Coords\y) ; get the colour of pixel under the mouse
  
  RedC.l = Red(Colour)
  GreenC.l = Green(Colour)
  BlueC.l = Blue(Colour)
  PixelColour.s = "R:"+Str(RedC)+" G:"+Str(GreenC)+" B:"+Str(BlueC)
  PixelColourHex.s = "$"+Hex(RedC)+Hex(GreenC)+Hex(BlueC)
  MousePosition.s = "PosX: "+Str(Coords\x)+" PosY: "+Str(Coords\y)
  WindowsHandle.s = "wH: "+Str(wDC)+" Hex$"+Hex(wDC)
  
  SetGadgetText(#String_1,MousePosition )
  SetGadgetText(#String_2,PixelColour)
  SetGadgetText(#String_3,PixelColourHex)
  SetGadgetText(#String_4,WindowsHandle)
  
  
  
  If StartDrawing(WindowOutput(#Window_0))
    UpdateColor(Colour)
    StopDrawing()
  EndIf
  
  Event = WaitWindowEvent()
  Select Event
    ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #String_0
      ElseIf EventGadget = #String_2
      ElseIf EventGadget = #String_3
      ElseIf EventGadget = #String_4
      ElseIf EventGadget = #Frame3D_5
      ElseIf EventGadget = #Button_6 
        End
      EndIf
      ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
  Delay(1)
ForEver
;
;}


Posted: Mon Dec 10, 2007 2:05 pm
by Trond
This seems to work. I don't know exactly why, but it has something to do with the fact that every time you get an event you create a new event (repaint) by refreshing the colour.

Code: Select all

;{ Windows 
Enumeration 
  #Window_0 
EndEnumeration 
;} 
;{ Gadgets 
Enumeration 
  #String_0 
  #String_1 
  #String_2 
  #String_3 
  #String_4 
  #Frame3D_5 
  #Button_6 
EndEnumeration 
;} 
Define.l Event, EventWindow, EventGadget, EventType, EventMenu 
;} 
WindowDC.l = GetWindowDC_(#Null) 
Global CPosX=2 
Global CPosY=2 
Global Width=80 
Global Height=95 

Procedure UpdateColor(Colour.l) 
  RedComponent.l = Red(Colour) 
  GreenComponent.l = Green(Colour) 
  BlueComponent.l = Blue(Colour) 
  ;SetGadgetColor(#Frame3D_5,#PB_Gadget_BackColor,RGB(RedComponent,GreenComponent,BlueComponent)) 
  Box(CPosX+3,CPosY+3,Width-6,Height-6,RGB(RedComponent,GreenComponent,BlueComponent)) 
EndProcedure 

Procedure OpenWindow_Window_0() 
  GPosX=85 
  GposY=2 
  GWidth=180 
  GHeight=20 
  Gspace=25 
  If OpenWindow(#Window_0, 450, 197, 268, 170, "Pick Color", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(#Window_0)) 
      ;StringGadget(#String_0, 1,1,290,20,"", #PB_String_ReadOnly);MOUSE COORDS 
      StringGadget(#String_1, GPosX, GposY, GWidth, GHeight, "", #PB_String_ReadOnly);Mouse Coords 
      StringGadget(#String_2, GPosX, GposY+Gspace, GWidth, GHeight, "", #PB_String_ReadOnly);RGB 
      StringGadget(#String_3, GPosX,GposY+Gspace*2, GWidth, GHeight, "", #PB_String_ReadOnly);hex 
      StringGadget(#String_4, GPosX, GposY+Gspace*3, GWidth, GHeight,"", #PB_String_ReadOnly);Window handle under mouse 
      Frame3DGadget(#Frame3D_5,CPosX , CPosY, Width, Height , "", #PB_Frame3D_Double) 
      ButtonGadget(#Button_6, 89, 120, 85, 40, "Close") 
    EndIf 
  EndIf 
EndProcedure 

OpenWindow_Window_0() 

Repeat 
  
  
  GetCursorPos_(Coords.POINT) ;get x/y position of mouse into point structure 
  wDC = WindowFromPoint_ (Coords\x, Coords\y) ;get the windows handle under the mouse 
  Colour = GetPixel_(WindowDC, Coords\x, Coords\y) ; get the colour of pixel under the mouse 
  
  RedC.l = Red(Colour) 
  GreenC.l = Green(Colour) 
  BlueC.l = Blue(Colour) 
  PixelColour.s = "R:"+Str(RedC)+" G:"+Str(GreenC)+" B:"+Str(BlueC) 
  PixelColourHex.s = "$"+Hex(RedC)+Hex(GreenC)+Hex(BlueC) 
  MousePosition.s = "PosX: "+Str(Coords\x)+" PosY: "+Str(Coords\y) 
  WindowsHandle.s = "wH: "+Str(wDC)+" Hex$"+Hex(wDC) 
  
  SetGadgetText(#String_1,MousePosition ) 
  SetGadgetText(#String_2,PixelColour) 
  SetGadgetText(#String_3,PixelColourHex) 
  SetGadgetText(#String_4,WindowsHandle) 
  
  
  
  If StartDrawing(WindowOutput(#Window_0)) 
    UpdateColor(Colour) 
    StopDrawing() 
  EndIf 
  
  Repeat
  Event = WindowEvent() 
  Select Event 
    ; /////////////////// 
    Case #PB_Event_Gadget 
      EventGadget = EventGadget() 
      EventType = EventType() 
      If EventGadget = #String_0 
      ElseIf EventGadget = #String_2 
      ElseIf EventGadget = #String_3 
      ElseIf EventGadget = #String_4 
      ElseIf EventGadget = #Frame3D_5 
      ElseIf EventGadget = #Button_6 
        End 
      EndIf 
      ; //////////////////////// 
    Case #PB_Event_CloseWindow 
      EventWindow = EventWindow() 
      If EventWindow = #Window_0 
        CloseWindow(#Window_0) 
        Break 
      EndIf 
  EndSelect 
  Until Event = 0
  Delay(1)
ForEver 
; 
;}

Posted: Mon Dec 10, 2007 8:23 pm
by rjara
This seems to work.
Thanks Trond

Yep it works here now too but I can find the changes you made to get it to work. I had your code and mine side by side and could not see it...its late at nite here :oops:

Posted: Mon Dec 10, 2007 8:54 pm
by Trond
Basically I process all remaining events before I check the colour again. You process just one event and then update the colour. Since updating the colour sends at least one redraw event those remaing events go "somewhere else" (at least they don't seem to be processed).

Posted: Mon Dec 10, 2007 11:43 pm
by ABBKlaus
or a textgadget :

Code: Select all

;{ Windows 
Enumeration 
  #Window_0 
EndEnumeration 
;} 
;{ Gadgets 
Enumeration 
  #String_0 
  #String_1 
  #String_2 
  #String_3 
  #String_4 
  #Frame3D_5 
  #Button_6 
  #Text_1
EndEnumeration 
;} 
Define.l Event, EventWindow, EventGadget, EventType, EventMenu 
;} 
WindowDC.l = GetWindowDC_(#Null) 
Global CPosX=2 
Global CPosY=2 
Global Width=80 
Global Height=95 
Global Coords.POINT
Global oldCoords.POINT

Procedure OpenWindow_Window_0() 
  GPosX=85 
  GposY=2 
  GWidth=180 
  GHeight=20 
  Gspace=25 
  If OpenWindow(#Window_0, 450, 197, 268, 170, "Pick Color", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(#Window_0)) 
      TextGadget(#Text_1, CPosX,CPosY,Width,Height,"",#PB_Text_Border);Color
      StringGadget(#String_1, GPosX, GposY, GWidth, GHeight, "", #PB_String_ReadOnly);Mouse Coords 
      StringGadget(#String_2, GPosX, GposY+Gspace, GWidth, GHeight, "", #PB_String_ReadOnly);RGB 
      StringGadget(#String_3, GPosX,GposY+Gspace*2, GWidth, GHeight, "", #PB_String_ReadOnly);hex 
      StringGadget(#String_4, GPosX, GposY+Gspace*3, GWidth, GHeight,"", #PB_String_ReadOnly);Window handle under mouse 
      ButtonGadget(#Button_6, 89, 120, 85, 40, "Close") 
    EndIf 
  EndIf 
EndProcedure 

OpenWindow_Window_0() 

Repeat 
  Event = WaitWindowEvent(10)
  Select Event 
    Case 0 ; zero means nothing to do
      GetCursorPos_(Coords.POINT) ;get x/y position of mouse into point structure 
      If Coords\x <> oldCoords\x Or Coords\y <> oldCoords\y
        wDC = WindowFromPoint_ (Coords\x, Coords\y) ;get the windows handle under the mouse 
        Colour = GetPixel_(WindowDC, Coords\x, Coords\y) ; get the colour of pixel under the mouse 
        RedC.l = Red(Colour) 
        GreenC.l = Green(Colour) 
        BlueC.l = Blue(Colour) 
        PixelColour.s = "R:"+Str(RedC)+" G:"+Str(GreenC)+" B:"+Str(BlueC) 
        PixelColourHex.s = "$"+Hex(RedC)+Hex(GreenC)+Hex(BlueC) 
        MousePosition.s = "PosX: "+Str(Coords\x)+" PosY: "+Str(Coords\y) 
        WindowsHandle.s = "wH: "+Str(wDC)+" Hex$"+Hex(wDC) 
        SetGadgetText(#String_1,MousePosition ) 
        SetGadgetText(#String_2,PixelColour) 
        SetGadgetText(#String_3,PixelColourHex) 
        SetGadgetText(#String_4,WindowsHandle) 
        SetGadgetColor(#Text_1,#PB_Gadget_BackColor,Colour)
        oldCoords\x=Coords\x
        oldCoords\y=Coords\y
      EndIf
    Case #PB_Event_Gadget 
      EventGadget = EventGadget() 
      EventType = EventType() 
      If EventGadget = #String_0 
      ElseIf EventGadget = #String_2 
      ElseIf EventGadget = #String_3 
      ElseIf EventGadget = #String_4 
      ElseIf EventGadget = #Frame3D_5 
      ElseIf EventGadget = #Button_6 
        Break
      EndIf
  EndSelect 
Until Event = #PB_Event_CloseWindow 

Posted: Tue Dec 11, 2007 10:21 am
by rjara
After a good night sleep (not) my little girls has a cold, I looked at the code and understood the concept. I basically was not allowing to refresh/update the other gadgets on the form, right.

Onother thing i have been trying to find/do is to change the mouse cursor to a cross so that i can more accurately point to a pixel/color i found this in the forum:

Code: Select all

SetClassLong_(WindowID(#Window_0), #GCL_HCURSOR, LoadCursor_(0, #IDC_CROSS)); change mouse pointer to a cross
Of course that only works while the mouse is over my app's form. How do i change the mouse pointer to stay changed outside my app's form?

Thank you both for your help thus far.

Perhaps you might want to help me on this one :wink:
http://www.purebasic.fr/english/viewtop ... highlight=[/code][/quote]