Draw a grid in a "ContainerGadget"

Just starting out? Need help? Post your questions and find answers here.
User avatar
VisualJump3D
User
User
Posts: 75
Joined: Thu Jun 23, 2011 8:32 pm
Location: italy
Contact:

Draw a grid in a "ContainerGadget"

Post by VisualJump3D »

Hello everyone,

How can I draw a grid on a "ContainerGadget"?

I mean, a 2D grid, consisting simply of horizontal and vertical lines,
I do not mean a "GridGadget"

Thanks
T4r4ntul4
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: Draw a grid in a "ContainerGadget"

Post by T4r4ntul4 »

hi,

why not using the container gadget as a grid as itself?
something like this:
Image

put this in menu --> Form --> Switch code / design view and hit F5

Code: Select all

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Global Window_0

Global Container_0, Container_0_Copy1, Container_0_Copy1, Container_0_Copy1_Copy1, Container_0_Copy2, Container_0_Copy1_Copy2, Container_0_Copy1_Copy2_Copy1, Container_0_Copy1_Copy2_Copy2, Container_0_Copy1_Copy2_Copy3, Container_0_Copy1_Copy2_Copy4, Container_0_Copy1_Copy2_Copy5, Container_0_Copy1_Copy2_Copy6, Container_0_Copy1_Copy2_Copy8, Container_0_Copy1_Copy2_Copy9, Container_0_Copy1_Copy2_Copy10, Container_0_Copy1_Copy2_Copy11


Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  Container_0 = ContainerGadget(#PB_Any, 105, 95, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1 = ContainerGadget(#PB_Any, 160, 95, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1 = ContainerGadget(#PB_Any, 215, 95, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy1 = ContainerGadget(#PB_Any, 270, 95, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy2 = ContainerGadget(#PB_Any, 105, 120, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2 = ContainerGadget(#PB_Any, 160, 120, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy1 = ContainerGadget(#PB_Any, 215, 120, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy2 = ContainerGadget(#PB_Any, 270, 120, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy3 = ContainerGadget(#PB_Any, 105, 145, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy4 = ContainerGadget(#PB_Any, 105, 170, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy5 = ContainerGadget(#PB_Any, 215, 145, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy6 = ContainerGadget(#PB_Any, 270, 145, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy8 = ContainerGadget(#PB_Any, 215, 170, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy9 = ContainerGadget(#PB_Any, 160, 170, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy10 = ContainerGadget(#PB_Any, 160, 145, 55, 25, #PB_Container_Single)
  CloseGadgetList()
  Container_0_Copy1_Copy2_Copy11 = ContainerGadget(#PB_Any, 270, 170, 55, 25, #PB_Container_Single)
  CloseGadgetList()
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
infratec
Always Here
Always Here
Posts: 7711
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Draw a grid in a "ContainerGadget"

Post by infratec »

You can not draw in a ContainerGadget().
You need a CanvasGadget() or an ImageGadget() inside the container.

Bernd
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5015
Joined: Sun Apr 12, 2009 6:27 am

Re: Draw a grid in a "ContainerGadget"

Post by RASHAD »

Hi
Use the ContainerGadget as usual beside you can use any image you like

Code: Select all

CreateImage(0,300,200,24,$FFFFFF)
StartDrawing(ImageOutput(0))
For x = 10 To 300 Step 20
  For y = 10 To 200 Step 20
     Line(0,y,300,1,$C0C0C2)
     Line(x,0,1,200,$C0C0C2)
  Next
Next
StopDrawing()

OpenWindow(0,0,0,640,480,"", #PB_Window_SystemMenu|#PB_Window_SizeGadget| #PB_Window_ScreenCentered)
ImageGadget(0,10,10,300,200,ImageID(0))
DisableGadget(0,1)

ContainerGadget(1, 10,10,300,200,#PB_Container_Flat) 
  StringGadget(2,10,10,100,20,"") 
  ButtonGadget(3,120,10,50,20,"OK") 
CloseGadgetList()

SetClassLongPtr_(GadgetID(1),#GCL_HBRBACKGROUND, GetStockObject_(#NULL_BRUSH))

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Egypt my love
User avatar
VisualJump3D
User
User
Posts: 75
Joined: Thu Jun 23, 2011 8:32 pm
Location: italy
Contact:

Re: Draw a grid in a "ContainerGadget"

Post by VisualJump3D »

Thanks to All!
mestnyi
Addict
Addict
Posts: 1109
Joined: Mon Nov 25, 2013 6:41 am

Re: Draw a grid in a "ContainerGadget"

Post by mestnyi »

Alternatively :)

Code: Select all

Procedure ImageDraw()
  Protected Image = CreateImage(#PB_Any,300,200,24,$FFFFFF) 
  StartDrawing(ImageOutput(Image))
  For X = 10 To 300 Step 20
    For Y = 10 To 200 Step 20
      Line(0,Y,300,1,$C0C0C2)
      Line(X,0,1,200,$C0C0C2)
    Next
  Next
  StopDrawing()
  ProcedureReturn Image  
EndProcedure

ProcedureDLL SetGadgetBackGroundImage( Gadget, ImageID) 
  Protected GadgetID
  If IsGadget(Gadget) And ImageID
    GadgetID = GadgetID(Gadget)
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux    
        Protected *Background, FixedBox.I, *Style.GtkStyle
        gdk_pixbuf_render_pixmap_and_mask_(ImageID, @*Background, 0, 0)
        *Style = gtk_style_new_()
        *Style\bg_pixmap[0] = *Background
        gtk_widget_set_style_(GadgetID, *Style)
      CompilerCase #PB_OS_Windows
        If GadgetType(Gadget) = #PB_GadgetType_ScrollArea
          GadgetID = FindWindowEx_(GadgetID,0,0,0)
        ElseIf GadgetType(Gadget) = #PB_GadgetType_Panel
        GadgetID = FindWindowEx_(FindWindowEx_(GadgetID,0,0,0),0,0,0)
          Debug GadgetID
        EndIf
        
        SetClassLongPtr_(GadgetID, #GCL_HBRBACKGROUND, CreatePatternBrush_(ImageID))
        InvalidateRect_(GadgetID, 0, #True) 
    CompilerEndSelect  
  EndIf
EndProcedure


CompilerIf #PB_Compiler_IsMainFile
  If OpenWindow(1, 0, 0, 322, 150, "window 1 gadget event example", #PB_Window_SystemMenu|#PB_Window_ScreenCentered )
    ContainerGadget(10, 8, 28, 306, 90, #PB_Container_Raised)
    ;ScrollAreaGadget(10, 8, 28, 306, 90, 306, 90, 0, #PB_ScrollArea_Raised)
    ;PanelGadget(10, 8, 28, 306, 90) :AddGadgetItem(10,-1,"Panel")
    ButtonGadget(11, 10, 15, 80, 24, "Button 11")
    ButtonGadget(12, 95, 15, 80, 24, "Button 12")
    CloseGadgetList()
    SetGadgetBackGroundImage(10,ImageID(ImageDraw()))
  EndIf
  
  Repeat
  Until WaitWindowEvent()=#PB_Event_CloseWindow
  CompilerEndIf
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Re: Draw a grid in a "ContainerGadget"

Post by hallodri »

Here is my attempt. An api callback with 2DDrawing

Code: Select all

Import ""
  PB_2DDrawing_PrepareGDI()
EndImport

Structure Struct2DDrawing 
  BackColor.i                   ;  $00
  Box.i                         ;  $04
  RoundBox.i                    ;  $08
  Circle.i                      ;  $0C
  DrawingFont.i                 ;  $10
  DrawingBuffer.i               ;  $14
  DrawingBufferPitch.i          ;  $18
  DrawingBufferPixelFormat.i    ;  $1C
  DrawingMode.i                 ;  $20
  DrawImage.i                   ;  $24
  DrawImage2.i                  ;  $28
  DrawAlphaImage2.i             ;  $2C
  DrawText.i                    ;  $30
  DrawRotatedText.i             ;  $34
  Ellipse.i                     ;  $38
  FillArea.i                    ;  $3C
  FrontColor.i                  ;  $40
  GrabDrawingImage.i            ;  $44
  LineXY.i                      ;  $48
  Plot.i                        ;  $4C
  Plot2.i                       ;  $50
  Point.i                       ;  $54
  StartDrawing.i                ;  $58
  StopDrawing.i                 ;  $5C
  TextHeight.i                  ;  $60
  TextWidth.i                   ;  $64
  unbekannt0.i[2]
  Release.i                     ;  $70
  unbekannt2.l[17]
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    unbekannt5.l
  CompilerEndIf    
  hwnd.i                        ;  $B8
  unbekannt3.i[4]
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86    
    unbekannt4.i
  CompilerEndIf  
  hdc.i                         ;  $D0
EndStructure             

Procedure ReleaseGadgetOutput(*mem.Struct2DDrawing, b) 
  ProcedureReturn ReleaseDC_(*mem\hwnd, *mem\hdc)  
EndProcedure

Procedure GadgetOutput(gadget)  
  Protected hwnd = GadgetID(gadget) 
  Protected *mem.Struct2DDrawing 
  
  If hwnd    
    *mem         = PB_2DDrawing_PrepareGDI()   
    *mem\Release = @ReleaseGadgetOutput()
    *mem\hwnd    = hwnd
    *mem\hdc     = GetDC_(hwnd)
  EndIf
  
  ProcedureReturn *mem
EndProcedure

Global ContainerOldProc.i

Procedure ContainerProc(hWnd,Msg,wParam,lParam)
  Protected result.i
  Protected x, y, cx, cy
  
  result = CallWindowProc_(ContainerOldProc, hWnd, Msg, wParam, lParam)
  
  If Msg = #WM_PAINT
    
    cx = GadgetWidth(0)
    cy = GadgetHeight(0)
    
    StartDrawing(GadgetOutput(0))
    For X = 0 To cx Step 20
      For Y = 0 To cy Step 20
        Line(0, Y, cx, 1, 0)
        Line(X, 0, 1, cy, 0)
      Next
    Next
    StopDrawing()
  EndIf
  
  ProcedureReturn result
EndProcedure

Procedure Main()
  
  OpenWindow(0,#PB_Ignore, #PB_Ignore, 800, 600,"") 
  
  ContainerGadget(0, 5, 5, 201, 201)
  
  ContainerOldProc = SetWindowLongPtr_(GadgetID(0), #GWLP_WNDPROC, @ContainerProc())
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndProcedure:End Main()

Last edited by hallodri on Fri Mar 27, 2015 9:26 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Draw a grid in a "ContainerGadget"

Post by netmaestro »

Very instructive and strong work hallodri 8) I knew when I saw the author it would be good.

Thanks for sharing it!
BERESHEIT
User avatar
VisualJump3D
User
User
Posts: 75
Joined: Thu Jun 23, 2011 8:32 pm
Location: italy
Contact:

Re: Draw a grid in a "ContainerGadget"

Post by VisualJump3D »

Whow !! Thanks !
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Draw a grid in a "ContainerGadget"

Post by bbanelli »

hallodri wrote:Here is my attempt. An api callback with 2DDrawing
On PB 5.31 x64 (OS is Windows 7) it throws Invalid memory access (read error @256) on line 75.

Code: Select all

Line(0, Y, cx, 1, 0)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Re: Draw a grid in a "ContainerGadget"

Post by hallodri »

Thanks! I have changed it.
mestnyi
Addict
Addict
Posts: 1109
Joined: Mon Nov 25, 2013 6:41 am

Re: Draw a grid in a "ContainerGadget"

Post by mestnyi »

Do not you tell me why is drawn on all containers, whereas I have one?

Code: Select all

CreateImage(0,300,200,24,$FFFFFF)
StartDrawing(ImageOutput(0))
For X = 10 To 300 Step 20
  For Y = 10 To 200 Step 20
     Line(0,Y,300,1,$C0C0C2)
     Line(X,0,1,200,$C0C0C2)
  Next
Next
StopDrawing()


OpenWindow(0,0,0,640,480,"", #PB_Window_SystemMenu|#PB_Window_SizeGadget| #PB_Window_ScreenCentered)
ContainerGadget(1, 10,10,300,200,#PB_Container_Flat) 
  StringGadget(2,10,10,100,20,"") 
  ButtonGadget(3,120,10,50,20,"OK") 
  
  ContainerGadget(22,10,50,100,100,#PB_Container_Flat) 
  CloseGadgetList()
CloseGadgetList()

ContainerGadget(11, 320,10,300,200,#PB_Container_Flat) 
  StringGadget(12,10,10,100,20,"") 
  ButtonGadget(13,120,10,50,20,"OK") 
CloseGadgetList()

SetClassLongPtr_(GadgetID(1),#GCL_HBRBACKGROUND, CreatePatternBrush_(ImageID(0)))
  
 ; SetClassLongPtr_(GadgetID(11),#GCL_HBRBACKGROUND, GetStockObject_(#NULL_BRUSH))


Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
mestnyi
Addict
Addict
Posts: 1109
Joined: Mon Nov 25, 2013 6:41 am

Re: Draw a grid in a "ContainerGadget"

Post by mestnyi »

Explain to me why this is happening?

Code: Select all

OpenWindow(0,0,0,640,480,"", #PB_Window_SystemMenu|#PB_Window_SizeGadget| #PB_Window_ScreenCentered)
SetWindowColor(0, #White) 

ContainerGadget(1, 10,10,300,200,#PB_Container_Flat) 
  StringGadget(2,10,10,100,20,"") 
  ButtonGadget(3,120,10,50,20,"OK") 
  
  ContainerGadget(22,10,50,100,100,#PB_Container_Flat) 
  CloseGadgetList()
CloseGadgetList()

ContainerGadget(11, 320,10,300,200,#PB_Container_Flat) 
  StringGadget(12,10,10,100,20,"") 
  ButtonGadget(13,120,10,50,20,"OK") 
CloseGadgetList()

SetClassLongPtr_(GadgetID(11),#GCL_HBRBACKGROUND, GetStockObject_(#NULL_BRUSH))

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Draw a grid in a "ContainerGadget"

Post by netmaestro »

...because the classname for all three containers is "PureContainer" ..? You are affecting all members of that class with SetClassLongPtr after all.
BERESHEIT
mestnyi
Addict
Addict
Posts: 1109
Joined: Mon Nov 25, 2013 6:41 am

Re: Draw a grid in a "ContainerGadget"

Post by mestnyi »

because the classname for all three containers is "PureContainer"
I knew if someone answers, it netmaestro, because you thunderstorm winapi. :)
So it turns out Fred's fault that did not "PureContainer_idgadget".
Now, how to fix it?
Post Reply