CanvasGadgetContainer

Just starting out? Need help? Post your questions and find answers here.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

CanvasGadgetContainer

Post by electrochrisso »

Josh, posted this on the German forum.
http://www.purebasic.fr/german/viewtopi ... 32#p303932

I modify a bit to add another StringGadget, all additions have ;*** at the end of the line.
Is this the correct way to add more gadgets, it seams to work, should I be using OldWinProc for more than one gadget. :?:

Code: Select all

;CanvasGadgetContainer

Global OldWinProc.i

Procedure WinCallback (hWnd, uMsg, wParam, lParam)
  Static Cursor.i = -1
  Define Tme.TRACKMOUSEEVENT
  
  
  Select uMsg
      
    Case #WM_MOUSEMOVE ;notwendig, wenn kein XP Skin Support
      Tme\cbSize    = SizeOf (TRACKMOUSEEVENT)
      Tme\dwFlags   = #TME_LEAVE
      Tme\hwndTrack = hwnd
      TrackMouseEvent_(Tme)
      
    Case #WM_LBUTTONDBLCLK
      SetFocus_(hWnd)
      
    Case #WM_SETCURSOR
      If PeekW (@lParam) = #HTCLIENT
        If Cursor = -1
          Cursor = GetGadgetAttribute (0, #PB_Canvas_Cursor)
        EndIf
        SetGadgetAttribute (0, #PB_Canvas_Cursor, #PB_Cursor_IBeam)
      EndIf
      
    Case #WM_MOUSELEAVE
      SetGadgetAttribute (0, #PB_Canvas_Cursor, Cursor)
      Cursor = -1
      
  EndSelect
  
  ProcedureReturn CallWindowProc_(OldWinProc ,hWnd ,uMsg, wParam, lParam)
  
EndProcedure

If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
  
  LongPtr = GetWindowLongPtr_(GadgetID(0), #GWL_STYLE)
  SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, LongPtr|#WS_CLIPCHILDREN)
  
  StringGadget (1, 10, 90, 180, 20,  "Test1")
  StringGadget (2, 10, 115, 180, 20, "Test2") ;***
  SetParent_ (GadgetID(1), GadgetID(0))
  SetParent_ (GadgetID(2), GadgetID(0)) ;***
  
  OldWinProc = SetWindowLongPtr_ (GadgetID(1), #GWL_WNDPROC, @WinCallback())
  OldWinProc = SetWindowLongPtr_ (GadgetID(2), #GWL_WNDPROC, @WinCallback()) ;***
  
  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget And EventGadget() = 0
      If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
        If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
          StopDrawing()
        EndIf
      EndIf
    EndIf   
    
  Until Event = #PB_Event_CloseWindow
EndIf
PureBasic! Purely the best 8)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4947
Joined: Sun Apr 12, 2009 6:27 am

Re: CanvasGadgetContainer

Post by RASHAD »

Hi :D
What is wrong with simple code ?

Code: Select all

;CanvasGadgetContainer

  OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
 
  StringGadget (1, 15, 90, 190, 20,  "Test1")
  StringGadget (2, 15, 115, 190, 20, "Test2") ;***
  SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #WS_CLIPSIBLINGS)
  SetWindowPos_(GadgetID(0), #HWND_BOTTOM, -1, -1, -1, -1, #SWP_NOSIZE | #SWP_NOMOVE)
;  
  Repeat
    Select WaitWindowEvent()
    
        Case  #PB_Event_CloseWindow
            Quit = 1
       
        Case  #PB_Event_Gadget
            Select EventGadget()
                Case 0
                    Select EventType()
                       Case #PB_EventType_LeftButtonDown
                            Drag = 1
                            
                       Case #PB_EventType_LeftButtonUp
                            drag = 0                       
                       
                       Case #PB_EventType_MouseMove
                               If Drag = 1
                                StartDrawing(CanvasOutput(0))
                                  x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
                                  y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
                                  Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
                                StopDrawing()
                               EndIf
                    EndSelect
                  
                Case 1
                     Debug GetGadgetText(1)
                
                Case 2
                     Debug GetGadgetText(2)
                
            EndSelect
    EndSelect
   
  Until Quit = 1
Edit : Modified for more control over the other gadgets
Egypt my love
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: CanvasGadgetContainer

Post by Josh »

looks good :D
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: CanvasGadgetContainer

Post by skywalk »

Very clean RASHAD!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: CanvasGadgetContainer

Post by IdeasVacuum »

....the time has come for Rashad to write a book on programming Windows with PB. I'm first in the queue to buy it! 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4947
Joined: Sun Apr 12, 2009 6:27 am

Re: CanvasGadgetContainer

Post by RASHAD »

Guys :mrgreen: Thank you very much
And take it easy I am lucky sometimes that is it.
Egypt my love
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: CanvasGadgetContainer

Post by srod »

With Rashad's code, the string gadgets disappear if you mouse down in the canvas.

Here's another take...

Code: Select all

Global OldWinProc.i

Procedure WinCallback (hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_SETCURSOR
      If wParam = GadgetID(0)
        result = CallWindowProc_(OldWinProc ,hWnd ,uMsg, wParam, lParam)
      EndIf
    Default      
      result = CallWindowProc_(OldWinProc ,hWnd ,uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
  UseGadgetList(GadgetID(0))  
  StringGadget (1, 10, 90, 180, 20,  "Test1")
  StringGadget (2, 10, 115, 180, 20, "Test2") ;***
  SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #WS_CLIPCHILDREN)
  
  OldWinProc = SetWindowLongPtr_ (GadgetID(0), #GWL_WNDPROC, @WinCallback()) ;***
  
  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget And EventGadget() = 0
      If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
        If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
          StopDrawing()
        EndIf
      EndIf
    EndIf   
    
  Until Event = #PB_Event_CloseWindow
EndIf
I may look like a mule, but I'm not a complete ass.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4947
Joined: Sun Apr 12, 2009 6:27 am

Re: CanvasGadgetContainer

Post by RASHAD »

Can not reproduce what you are saying srod,Sorry
Always The 2 StringGadgets on Top
PB 4.61 XP x86 SP2 - Win 7 x64
Egypt my love
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: CanvasGadgetContainer

Post by srod »

RASHAD wrote:Can not reproduce what you are saying srod,Sorry
Always The 2 StringGadgets on Top
PB 4.61 XP x86 SP2 - Win 7 x64
Neither can I!!! :D

I must be going nuts!

That is strange. Reckon I must have inadvertently commented out the SetWindowPos_()!
I may look like a mule, but I'm not a complete ass.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: CanvasGadgetContainer

Post by electrochrisso »

Gee!, you make it look so easy RASHAD. :wink:
IdeasVacuum wrote:....the time has come for Rashad to write a book on programming Windows with PB. I'm first in the queue to buy it! 8)
I already asked him that, I think on the lines of a reference guide to Tip & Tricks, PB API, I reckon he would have a bit of a seller on his hands, me for one. :)
PureBasic! Purely the best 8)
Post Reply