Using the SetWindowCallback()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Using the SetWindowCallback()

Post by Columbo »

I am trying to create a window with 4 boxes with rounded corners that will contain a TextGadget() in them. I guess my question is,… can you have more than one callback in a window?

For example, If I created a box with rounded corners with a text gadget in it with the following code:

Code: Select all


Procedure CallbackStoreBox(hwnd, msg, wparam, lparam)
  
  result1 = #PB_ProcessPureBasicEvents
  Select msg
    Case #WM_PAINT
      hdc = BeginPaint_(hwnd, @ps.PAINTSTRUCT)
      RoundRect_(hdc,600, 20, 780, 75, 20, 20)      
      EndPaint_(hwnd,@ps)
  EndSelect
  ProcedureReturn result1
EndProcedure

And then I call it with:

Code: Select all


wFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered ;Window flags
If OpenWindow(#MainWindow, 0, 0, 800, 600, "Main Window", wFlags)   SetWindowColor(#MainWindow, RGB(235,235,235))

SetWindowCallback(@CallbackStoreBox())

That works fine! No problems!

Now if I add a second box with rounded corners with the following code.

Code: Select all



Procedure CallbackStoreBox(hwnd, msg, wparam, lparam)
  
  result1 = #PB_ProcessPureBasicEvents
  Select msg
    Case #WM_PAINT
      hdc = BeginPaint_(hwnd, @ps.PAINTSTRUCT)
      RoundRect_(hdc,600, 20, 780, 75, 20, 20)      
      EndPaint_(hwnd,@ps)
  EndSelect
  ProcedureReturn result1
EndProcedure

Procedure CallbackCustBox(hwnd, msg, wparam, lparam)
  result2 = #PB_ProcessPureBasicEvents
  Select msg
    Case #WM_PAINT
      hdc = BeginPaint_(hwnd, @ps.PAINTSTRUCT)
      RoundRect_(hdc,15, 100, 700, 200, 20, 20)      
      EndPaint_(hwnd,@ps)
  EndSelect
  ProcedureReturn result2
EndProcedure


And add the call to it like this:

Code: Select all


wFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered ;Window flags
If OpenWindow(#MainWindow, 0, 0, 800, 600, "Main Window", wFlags)   SetWindowColor(#MainWindow, RGB(235,235,235))

SetWindowCallback(@CallbackStoreBox())
SetWindowCallback(@CallbackCustBox())

Then the second box, (CallbackCustBox), will display ok but the first box, (CallbackStoreBox) , disappears leaving only the TextGadget() being displayed.

Is there a problem using the SetWindowCallback more than once in the same window or am I coding it incorrectly?

I’m pretty new at all this.

Thanks
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Using the SetWindowCallback()

Post by infratec »

Hi,

there is only on WindowCallBack().
But you can decide inside the callback which gadget is actually processed.

And if you provide a 'working' code snippet, than I try my best to resolve the problem :wink:

Bernd
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Using the SetWindowCallback()

Post by Columbo »

I can give you the code but I'm not sure how to attach a .zip file to this phpBB forum.

It is not the TextGadgets that I am concerned about. What I need to know is how to create more than one box with rounded corners in the same window. I can create one box but I can't get it to create two boxes. If I create a second box, then the first box disappears. Maybe there is a better way of doing this rather than the code I am using.

Thanks
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Using the SetWindowCallback()

Post by Thunder93 »

Later you could make a command to add another box, for as many as you want on the window.

Code: Select all

Procedure CallbackStoreBox(hwnd, msg, wparam, lparam)
  
  result1 = #PB_ProcessPureBasicEvents
  Select msg
    Case #WM_PAINT
      hdc = BeginPaint_(hwnd, @ps.PAINTSTRUCT)
      
      RoundRect_(hdc, 10, 20, 280, 75, 20, 20)
      
      RoundRect_(hdc, 300, 20, 580, 75, 20, 20)
      
      RoundRect_(hdc, 600, 20, 780, 75, 20, 20)
      
      EndPaint_(hwnd,@ps)
  EndSelect
  
  ProcedureReturn result1
EndProcedure
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Using the SetWindowCallback()

Post by netmaestro »

Using SetWindowCallback a second time removes the first callback. Anyway I wouldn't do it like that. I'd let the string gadgets draw themownselves so that they can be moved/resized and will still show correctly:

Code: Select all

Procedure StringProc(hwnd, msg, wParam, lParam)
  oldproc = GetProp_(hwnd, "oldproc") 
  
  Select msg
    Case #WM_NCDESTROY
      RemoveProp_(hwnd, "oldproc")
      
    Case #WM_NCPAINT
      hdc = GetDC_(hwnd)
      pen = CreatePen_(#PS_SOLID, 1, RGB(180,180,255))
      DeleteObject_(SelectObject_(hdc, pen))
      Select hwnd
        Case GadgetID(0)
          RoundRect_(hdc, -3, -3, GadgetWidth(0), GadgetHeight(0), 16, 16) 
          
        Case GadgetID(1)
          RoundRect_(hdc, -3, -3, GadgetWidth(1), GadgetHeight(1), 16, 16)
      EndSelect
      ReleaseDC_(hwnd, hdc)
      DeleteObject_(pen)
      ProcedureReturn 0
      
  EndSelect
  
  ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
EndProcedure

OpenWindow(0,0,0,820,600,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
StringGadget(0, 20,40,780,20,"")
StringGadget(1, 20,80,480,20,"")
For i=0 To 1
  SetProp_(GadgetID(i), "oldproc", SetWindowLongPtr_(GadgetID(i), #GWLP_WNDPROC, @StringProc()))
Next

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
BERESHEIT
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Using the SetWindowCallback()

Post by Thunder93 »

netmaestro has a nice approach here. :mrgreen:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Using the SetWindowCallback()

Post by Columbo »

Thanks netmaestro. I really appreciate the help. As I mentioned in my post, I'm pretty new at this. While this seems to work as far as setting up stringGadgets, what I am trying to do is to is set up 4 boxes with rounded corners in a window and then place TextGadgets, or in some cases, EditorGadgets in them. One box may have 3 or 4 TextGadgets in it. If I could figure out how to upload an image to this post I could show you exactly what I am trying to do.

Actually, you can see what I'm trying to do here:
http://www.codeinbasic.com/index.php?topic=171.0

The boxes will have a different color background than the Window background and are really just a means to divide the window up into the various topics for easier reading and appearance.

Thank you so much.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Using the SetWindowCallback()

Post by Thunder93 »

I guess you need to have an account over at codeinbasic and logged in to see posted screenshots..

btw; did you see how I giving multi support to your existing code? :?
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using the SetWindowCallback()

Post by TI-994A »

Columbo wrote:...I am trying to do is to is set up 4 boxes with rounded corners in a window and then place TextGadgets, or in some cases, EditorGadgets in them. One box may have 3 or 4 TextGadgets in it...

...The boxes will have a different color background than the Window background and are really just a means to divide the window up into the various topics for easier reading and appearance.
Hi John. If I understand correctly, all you would need is a background image with such divisions pre-drawn. Since you're already using a background image (the spring binder image with drop shadows), you could easily prepare different images with the required divisions for each screen.

However, if you'd prefer to do this dynamically, here's one approach:

Code: Select all

UseJPEGImageDecoder()

Enumeration
  #MainWindow
  #show1
  #show2
  #show3
  #bgImg
  #label1
  #label2
  #input1
  #input2
  #editor
  #bg
EndEnumeration

LoadImage(#bg, "bgPOS.jpg")
ResizeImage(#bg, 600, 400)

Procedure displayScreen1()
  For refresh = #show1 To #editor
    HideGadget(refresh, 1)
  Next refresh
  
  SetGadgetAttribute(#bgImg, #PB_Canvas_Image, ImageID(#bg))
  StartDrawing(CanvasOutput(#bgImg))
    DrawingMode(#PB_2DDrawing_Default)
    RoundBox(47, 67, 504, 71, 10, 10, #White)
    RoundBox(47, 150, 393, 219, 10, 10, #White)
    RoundBox(447, 265, 122, 103, 10, 10)
    Box(454, 150, 95, 106, #White)
    DrawingMode(#PB_2DDrawing_Outlined)
    RoundBox(47, 67, 504, 71, 10, 10, #Black)
    RoundBox(47, 150, 393, 219, 10, 10, #Black)
    RoundBox(447, 265, 125, 103, 10, 10, #Black)
    Box(454, 150, 95, 106, #Black)
  StopDrawing()
  
  For refresh = #show1 To #input2
    HideGadget(refresh, 0)
  Next refresh
EndProcedure    

Procedure displayScreen2()
  For refresh = #show1 To #editor
    HideGadget(refresh, 1)
  Next refresh

  SetGadgetAttribute(#bgImg, #PB_Canvas_Image, ImageID(#bg))
  StartDrawing(CanvasOutput(#bgImg))
    DrawingMode(#PB_2DDrawing_Default)
    RoundBox(47, 67, 504, 71, 10, 10, #White)
    RoundBox(47, 150, 502, 219, 10, 10, #White)
    DrawingMode(#PB_2DDrawing_Outlined)
    RoundBox(47, 67, 504, 71, 10, 10, #Black)
    RoundBox(47, 150, 502, 219, 10, 10, #Black)
  StopDrawing()
  
  For refresh = #show1 To #input2
    HideGadget(refresh, 0)
  Next refresh
EndProcedure    

Procedure displayScreen3()
  For refresh = #show1 To #editor
    HideGadget(refresh, 1)
  Next refresh

  SetGadgetAttribute(#bgImg, #PB_Canvas_Image, ImageID(#bg))
  StartDrawing(CanvasOutput(#bgImg))
    DrawingMode(#PB_2DDrawing_Default)
    RoundBox(47, 67, 504, 300, 10, 10, #White)
    DrawingMode(#PB_2DDrawing_Outlined)
    RoundBox(47, 67, 504, 300, 10, 10, #Black)
  StopDrawing()
  
  For refresh = #show1 To #bgImg
    HideGadget(refresh, 0)
  Next refresh
  HideGadget(#editor, 0)
EndProcedure 

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 600, 400, "Background Image", wFlags)

CanvasGadget(#bgImg, 0, 0, 600, 400)
DisableGadget(#bgImg, 1)

TextGadget(#label1, 66, 80, 80, 20, "Last Name:")
TextGadget(#label2, 66, 105, 80, 20, "First Name:")
StringGadget(#input1, 146, 80, 150, 20, "")
StringGadget(#input2, 146, 105, 150, 20, "")
EditorGadget(#editor, 60, 80, 390, 270)
ButtonGadget(#show1, 465, 160, 75, 25, "screen 1")
ButtonGadget(#show2, 465, 190, 75, 25, "screen 2")
ButtonGadget(#show3, 465, 220, 75, 25, "screen 3")

SetGadgetColor(#editor, #PB_Gadget_BackColor, #Yellow)
For gadgetColor = #label1 To #label2
  SetGadgetColor(gadgetColor, #PB_Gadget_BackColor, #White)
  SetGadgetColor(gadgetColor, #PB_Gadget_FrontColor, #Blue)
Next gadgetColor

displayScreen1()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #show1
          displayScreen1()
        Case #show2
          displayScreen2()
        Case #show3
          displayScreen3()
      EndSelect
  EndSelect
Until appQuit = 1
The source code, along with the sample image file, can be downloaded here: PureBasic Backgrounds

Hope it helps. :)


PS: This forum does not accept uploads, so you'd have to use external image or file links.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Using the SetWindowCallback()

Post by Columbo »

Thanks Thunder93 and yes, I did see how you gave multi support to my code. I've kept that code snippet for future use. Thanks again.

Thanks Syed for your code as well. Actually, the image that I showed as an example is not my image. It is just one I found that I thought would show what I was trying to do. To be honest, I never thought of using a background image but it sounds like the easiest solution. I have grabbed your code and I'll take a peek at it tomorrow. It is just after 3:00 a.m. here and I'm heading to bed to catch a few winks.

Thanks again and I'll let you know how it turns out.

Cheers,
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Post Reply