noob question about toolbarimagebutton

Just starting out? Need help? Post your questions and find answers here.
BluesInA
User
User
Posts: 11
Joined: Mon Mar 07, 2005 3:20 pm

noob question about toolbarimagebutton

Post by BluesInA »

i have a working toolbar image button doing what i want it to do. but can i change the image when a user clicks on the button? i'm using it to cycle through font sizes for text being displayed in string gadets. i have ico files i'd like to display on the toolbar image button for the different font sizes.

sorry if you don't understand my english - i went to school in california. :)

thanks for any help,

mike
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Mike, something like this?

Code: Select all

Enumeration
  #hWnd
EndEnumeration
Enumeration
  #ButtonImage_0
  #ButtonImage_1
  #ButtonImage_2
  #Frame3D_0
EndEnumeration

SystemPath.s = Space(255) 
Result = GetSystemDirectory_(SystemPath.s, 255) 

Global Image0,Image1,Image2,Image3,Image4,Image5

Image0 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 10)
Image1 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 11)
Image2 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 21)
Image3 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 15)
Image4 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 18)
Image5 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 61)

Procedure Open_hWnd()
  If OpenWindow(#hWnd, 290, 255, 261, 106,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "Icon Example")
    If CreateGadgetList(WindowID())
      ButtonImageGadget(#ButtonImage_0, 30, 20, 60, 60, Image0)
      ButtonImageGadget(#ButtonImage_1, 100, 20, 60, 60, Image1)
      ButtonImageGadget(#ButtonImage_2, 170, 20, 60, 60, Image2)
      Frame3DGadget(#Frame3D_0, 10, 10, 240, 80, "")
      
    EndIf
  EndIf
EndProcedure

Open_hWnd()
Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    
    GadgetID = EventGadgetID()
    
    If GadgetID = #ButtonImage_0 
      ; we will change it only once..
      SetGadgetState(#ButtonImage_0,Image3)
      
    ElseIf GadgetID = #ButtonImage_1
      ; we will change it on every click from one to the other
      
      enable=1-enable 
      If enable = 1
        SetGadgetState(#ButtonImage_1,Image4)
      Else
        SetGadgetState(#ButtonImage_1,Image1)
      EndIf
      
      
    ElseIf GadgetID = #ButtonImage_2
      ; we will loop through some of the icons
      ; the lazy-guy way :)
      
      Img + 1

      If Img = 1
        SetGadgetState(#ButtonImage_2,Image0)
      ElseIf Img = 2
        SetGadgetState(#ButtonImage_2,Image1)
      ElseIf Img = 3
        SetGadgetState(#ButtonImage_2,Image2)
      ElseIf Img = 4 
        SetGadgetState(#ButtonImage_2,Image3)
      ElseIf Img > 4 
        SetGadgetState(#ButtonImage_2,Image4)
        Img = 0
      EndIf
      
    EndIf
    
  EndIf
Until Event = #PB_EventCloseWindow
End

;change icons example by gushh...
Its pretty simple but it might be what you want to do?
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

Post by Jan2004 »

Could you exactly comment this part of source code (particulary SystemPath):

Code: Select all

SystemPath.s = Space(255) 
Result = GetSystemDirectory_(SystemPath.s, 255) 


Image0 =  ExtractIcon_(0, SystemPath + "\Shell32.dll", 10) 
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Yes I could, however I dont save lines just because somethings supported or not. In a project im working on, I dont even use systempath, you're right. But its quite lazy not to use it. as you might need the systempath later (not in this case). But this is an old code of mine which I slapped the dust and posted.

Code: Select all

Image0 =  ExtractIcon_(0, "Shell32.dll", 10)
that is "good" to work indeed, however I already told you I like to do things even if it makes you write more.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi Jan2004,

Did you mean comment out (as in remove) or comment on (as in explain)?
@}--`--,-- A rose by any other name ..
BluesInA
User
User
Posts: 11
Joined: Mon Mar 07, 2005 3:20 pm

Post by BluesInA »

all,

thanks for your replies. your example, dagcrack, does just what i want, but when i get the button into the toolbar, the button won't respond to mouse clicks. i tried using a spin gadget but the same thing happened. this is how i got the gadget into the toolbar (code from the forum):

hToolBar = CreateToolBar(#ToolBar, WindowID(#Window_Main))
If hToolBar
ToolBarStandardButton(#ToolBar_New, #PB_ToolBarIcon_New)
.
.
.
If CreateGadgetList(WindowID(#Window_Main))
hComboBox1 = ComboBoxGadget(#Gadget_ComboBox_1,180,0,250,100,#PB_ComboBox_editable)
SetParent_(hComboBox1, hToolBar)


the combobox seems to work ok, but when i try other gadgets, they just sit there and do nothing.

anyway, thanks again,
mike
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Mike, maybe it has something to do with the gadget list... sometimes if you create wrongly your gadgets, they might be unusable.. But this is just a thought of what could be happening, you should post a code that resembles this problem you have, so we can check it out and find a solution.
BluesInA
User
User
Posts: 11
Joined: Mon Mar 07, 2005 3:20 pm

Post by BluesInA »

hey, cool, i got the buttons to work. i included a spin gadget in my example to show you what i was talking about - i still can't get it to work. but now that i can get the buttons to work, i'm just curious about the spin gadget.

thanks a lot,

mike




Enumeration
#Window_0
#ToolBarStandard_1
#ToolBarStandard_2
#ToolBarStandard_3
#ToolBarImage_1
#ButtonImage_0
#SpinGadget_0
EndEnumeration

Procedure Open_Window_0()
If OpenWindow(#Window_0, 216, 0, 150, 100, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar , "ToolBar Test")
toolbar = CreateToolBar(0, WindowID(#Window_0))
If toolbar
ToolBarStandardButton(#ToolBarStandard_1, #PB_ToolBarIcon_New)
ToolBarStandardButton(#ToolBarStandard_2, #PB_ToolBarIcon_Open)
ToolBarStandardButton(#ToolBarStandard_3, #PB_ToolBarIcon_Save)
CreateImage(tbImage1,16,16)
LoadImage(tbImage1, "C:\Program Files\PureBasic\Examples\Sources\Data\Drive.bmp")
ToolBarImageButton(#ToolBarImage_1, UseImage(tbImage1)) ; can i change this image when user clicks?
EndIf

If CreateGadgetList(WindowID())
img = LoadImage(tbImage2, "C:\Program Files\PureBasic\Examples\Sources\Data\File.bmp")
button = ButtonImageGadget(#ButtonImage_0, 95, 0, 18, 18, img)
SetParent_(button, toolbar)
spin = SpinGadget(#SpinGadget_0, 120, 0, 30, 20, 5, 15)
SetGadgetState (#SpinGadget_0,5) : SetGadgetText(#SpinGadget_0,"5")
SetParent_(spin, toolbar)
EndIf
EndIf
EndProcedure

swap = 1

Open_Window_0()

Repeat
Event = WaitWindowEvent()
Select Event
; Case #PB_Event_Menu
; Select EventMenuID()
; Case #ToolBarStandard_1
; Case #ToolBarStandard_2
; Case #ToolBarStandard_3
; Case #ToolBarImage_1
; EndSelect

Case #PB_Event_Gadget
Select EventGadgetID()
Case #ButtonImage_0
If swap
img = LoadImage(tbImage2, "C:\Program Files\PureBasic\Examples\Sources\Data\Drive.bmp")
SetGadgetState(#ButtonImage_0, img)
swap = 0
Else
img = LoadImage(tbImage2, "C:\Program Files\PureBasic\Examples\Sources\Data\CDPlayer.ico")
SetGadgetState(#ButtonImage_0, img)
swap = 1
EndIf

Case #SpinGadget_0
SetGadgetText(#SpinGadget_0,Str(GetGadgetState(#SpinGadget_0))) ; doesn't spin
;WindowEvent() ; absolutely needed to avoid endless event-loops

EndSelect
EndSelect
Until Event = #PB_EventCloseWindow
End
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

The SpinGadget is actually 2 gadgets. A "buddy" gadget (the Edit control/StringGadget) and the the "spinner" (those little up/down buttons).

Code: Select all

spin = SpinGadget(#SpinGadget_0, 120, 0, 30, 20, 5, 15)
^That^ code was returning the handle to the "buddy" portion of the SpinGadget.

Code: Select all

SetParent_(spin, toolbar)
^That^ code was setting the parent for the "buddy" portion of the SpinGadget, and leaving the "spinner" behind. You need to set the parent for the "spinner" and the "buddy", individually. ;)

Here's a working SpinGadget with ToolBar as parent. Take a look at lines 27 - 33 for comments.

Code: Select all

Enumeration 
  #Window_0 
  #ToolBarStandard_1 
  #ToolBarStandard_2 
  #ToolBarStandard_3 
  #ToolBarImage_1 
  #ButtonImage_0 
  #SpinGadget_0 
EndEnumeration 

Procedure Open_Window_0() 
  If OpenWindow(#Window_0, 216, 0, 150, 100, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar , "ToolBar Test") 
    toolbar = CreateToolBar(0, WindowID(#Window_0)) 
    If toolbar 
      ToolBarStandardButton(#ToolBarStandard_1, #PB_ToolBarIcon_New) 
      ToolBarStandardButton(#ToolBarStandard_2, #PB_ToolBarIcon_Open) 
      ToolBarStandardButton(#ToolBarStandard_3, #PB_ToolBarIcon_Save) 
      CreateImage(tbImage1,16,16) 
      LoadImage(tbImage1, "C:\PureBasic\Examples\Sources\Data\Drive.bmp") 
      ToolBarImageButton(#ToolBarImage_1, UseImage(tbImage1)) ; can i change this image when user clicks? 
    EndIf 
    
    If CreateGadgetList(WindowID()) 
      img = LoadImage(tbImage2, "C:\PureBasic\Examples\Sources\Data\File.bmp") 
      button = ButtonImageGadget(#ButtonImage_0, 95, 0, 18, 18, img) 
      SetParent_(button, toolbar) 
      ; --> SpinGadget returns a handle to the "buddy" (Edit control/StringGadget)
      spinBuddy = SpinGadget(#SpinGadget_0, 120, 0, 30, 20, 5, 15)
      ; --> Find the "spinner" (up/down buttons)
      spinner = FindWindowEx_(WindowID(), spinBuddy, "msctls_updown32", #Null)
      ; --> Set parent for "buddy" and "spinner"
      SetParent_(spinBuddy, toolbar) 
      SetParent_(spinner, toolbar) 
      SetGadgetState (#SpinGadget_0,5) : SetGadgetText(#SpinGadget_0,"5") 
    EndIf 
  EndIf 
EndProcedure 

swap = 1 

Open_Window_0() 

Repeat 
  event = WaitWindowEvent() 
  Select event 
    Case #PB_Event_Gadget 
      Select EventGadgetID() 
        Case #ButtonImage_0 
          If swap 
            img = LoadImage(tbImage2, "C:\PureBasic\Examples\Sources\Data\Drive.bmp") 
            SetGadgetState(#ButtonImage_0, img) 
            swap = 0 
          Else 
            img = LoadImage(tbImage2, "C:\PureBasic\Examples\Sources\Data\CDPlayer.ico") 
            SetGadgetState(#ButtonImage_0, img) 
            swap = 1 
          EndIf 
          
        Case #SpinGadget_0 
          SetGadgetText(#SpinGadget_0,Str(GetGadgetState(#SpinGadget_0))) ; doesn't spin 
                    
      EndSelect 
  EndSelect 
Until event = #PB_EventCloseWindow 
End
As a side note, you should use code tags when posting code snippets. It makes for easier reading and it helps with copy/paste operations. ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
BluesInA
User
User
Posts: 11
Joined: Mon Mar 07, 2005 3:20 pm

Post by BluesInA »

thanks for your reply sparkie. that was a great explanation, especially for a noob like me. and i'll use the code tag next time.

mike
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

You're welcome mike. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply