Announcement

Collapse
No announcement yet.

setting a image to a image box in PB win 8

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • setting a image to a image box in PB win 8

    I am trying to set a image that i have in a folder to pull into a image box went to veribles are =. the images are BMP. please help. i have never done this before.

  • #2
    Hi David,

    Please let us know if this is the kind of thing you had in mind?
    Code:
     
    #Dim All
    #Compile Exe
    #Include "WIN32API.INC"
    #Resource "ImageTest.pbr"
     
    %IDC_IMAGEX1  = 101
    '------------------/
     
    CallBack Function DlgProc() As Long
     Static A, B As Long
      Select Case CbMsg
        Case %WM_InitDialog
          A = 1 : B = 1
     
        Case %WM_Command
          Select Case CbCtl
            Case %IdOk
              If CbCtlMsg = %BN_Clicked Or CbCtlMsg = 1 Then
                If A = B Then         ' Change Bitmap
                  Control Set ImageX CbHndl, %IDC_IMAGEX1, "IMGFILE2"
                  B = B - 1
                ElseIf A = B + 1 Then ' Change Bitmap back
                  Control Set ImageX CbHndl, %IDC_IMAGEX1, "IMGFILE1"
                  B = B + 1
                End If
              End If
          End Select
     
      End Select
    End Function
    '------------------/DlgProc
     
    Function PBMain() As Long
     Local hDlg As Dword
     
      Dialog New 0, "Test", , , 240, 150, %WS_Caption Or %WS_SysMenu, 0 To hDlg
        Control Add Button, hDlg, %IdOk, "&Test", 185, 132, 50, 14
        Control Add ImageX, hDlg, %IDC_IMAGEX1, "IMGFILE1", 20, 20, 150, 105
     
      Dialog Show Modal hDlg, Call DlgProc
     
    End Function
    '------------------/PBMain
     
    #If 0   ' start comment block
    Contents of resource file
     - compile with RC.exe to make IMAGETEST.RES then use PBRes.exe to create ImageTest.pbr
     
    //ImageTest.rc
     
                     // Use your own files here
    IMGFILE1  Bitmap "C:\WINDOWS\Gone Fishing.bmp"  //Image no.1
    IMGFILE2  Bitmap "C:\WINDOWS\Coffee Bean.bmp"   //Image no.2
    //..etc
     
    #EndIf  ' end comment block
    Rgds, Dave

    Comment


    • #3
      I have no problem changing the imagex from some code resulting from either another buitton being pushed or some other action. I have troublem seting when the imagex button is pressed and released to change the imagex.

      I need to make a button icon change on the click of the mouse OR button selected and space bar being set. Then change the button back. I can't get it to do this. any Ideas using your code in the previous answer as a basis?
      Barry

      Comment


      • #4
        I need to make a button icon change on the click of the mouse OR button selected and space bar being set. Then change the button back. I can't get it to do this. any Ideas using your code in the previous answer as a basis?
        You mean you want a different image while the button is down?

        I don't know how to do that with IMAGEX controls but with owner-drawn buttons it's pretty straightforward... on WM_DRAWITEM you can query the state using the BM_GETSTATE message looking for BST_PUSHED and select your image accordingly.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Barry,

          Seems you need either a IMGBUTTON or IMGBUTTONX control..
          Code:
          #Compile Exe
          #Dim All
           
          #Include "WIN32API.INC"
          #Resource "ImageTest.pbr"
           
          %IDC_IMGBUTTON1 = 101
          %IDC_IMGBUTTONX = 102
           
          CallBack Function DlgProc()
           Static ImgBtnFlag As Long
            Select Case As Long CbMsg
              Case %WM_InitDialog
           
              Case %WM_Command
                Select Case As Long CbCtl
           
                  Case %IDC_IMGBUTTON1
                    If CbCtlMsg = %BN_Clicked Then
                     Local UpDwnFlag As Long
                      Control Get Check CbHndl, %IDC_IMGBUTTON1 To UpDwnFlag
                      Control Set Imgbutton CbHndl, %IDC_IMGBUTTON1, IIF$(UpDwnFlag, "IMGFILE2", "IMGFILE1")
                    End If
           
                  Case %IDC_IMGBUTTONX
                    If CbCtlMsg = %BN_Clicked Then
                      Control Set ImgbuttonX CbHndl, %IDC_IMGBUTTONX, IIF$(ImgBtnFlag, "IMGFILE2", "IMGFILE1")
                      ImgBtnFlag = IsFalse ImgBtnFlag
                    End If
           
                  Case %IdCancel
                    If CbCtlMsg = %BN_Clicked Then
                      Dialog End CbHndl, 0
                    End If
                End Select
            End Select
          End Function
           
          Function PbMain() As Long
            Local hDlg  As Dword
           
            Dialog New 0, "ImageTest", , , 240, 150, %WS_Caption Or %WS_SysMenu To hDlg
            Control Add Imgbutton,  hDlg, %IDC_IMGBUTTON1, "IMGFILE1", 40, 40, 50, 50, %WS_Tabstop Or %BS_PushLike Or %BS_AutoCheckbox
            Control Add ImgbuttonX, hDlg, %IDC_IMGBUTTONX, "IMGFILE2", 140, 40, 50, 50
            Control Add Button,     hDlg, %IdCancel, "Exit", 180, 130, 50, 15
           
            Dialog Show Modal hDlg, Call DlgProc
           
          End Function
          '------------------/
          #IF 0
           
          //ImageTest.rc
           
                           // Use your own files here
          IMGFILE1  Bitmap "C:\WINDOWS\Gone Fishing.bmp"  //Image no.1   could be icons..
          //IMGFILE1  ICON   "help2.ico"
          IMGFILE2  Bitmap "C:\WINDOWS\Coffee Bean.bmp"   //Image no.2
          //IMGFILE2  ICON   "tips.ico"
          //..etc
           
          #ENDIF
          Rgds, Dave

          Comment


          • #6
            Originally posted by Michael Mattias View Post
            You mean you want a different image while the button is down?

            I don't know how to do that with IMAGEX controls but with owner-drawn buttons it's pretty straightforward... on WM_DRAWITEM you can query the state using the BM_GETSTATE message looking for BST_PUSHED and select your image accordingly.

            MCM
            I alread do that on thestate of a varablle in a timer. If a certan time is met, the image changes in the imagex box. I'm tring to chage it when the button is pressed, but can't. Using setcursor, I can fire a beep, but can't change te image (even with control redraw)
            Barry

            Comment


            • #7
              I don't know the standard controls (IMGBUTTON, IMGBUTTONX) are really suitable for doing this, although you might be able to subclass the control and change the image on WM_KEYDOWN (space) and/or or WM_LBUTTONDOWN. (and back on "up").

              If you want to stay in DDT-Land, well, that's beyond my pay grade....unless.....

              I think Borje Hagsten's RRBUTTON can do this out of the can... CONTROL ADD "rrbutton" etc etc. ... that RRBUTTON #INCLUDE file is around here somewhere.

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Dave,
                Not exactly what I want. I want one image when the button is not clicked. Then the Image changes while the Left button is down. When it is released, I want the original image back.I've already tried what you show. I just can't trigger on %WM_LBUTTONUP
                Barry

                Comment


                • #9
                  Barry, I have something that works as you describe- here's the callback that does it. It's probably not best practice, but the same trick works great for my motor control programs where button-down runs the motor and button-up stops it:

                  Code:
                  CALLBACK FUNCTION ShowDIALOG1Proc()
                      LOCAL MyButton AS LONG
                      SELECT CASE AS LONG CB.MSG
                          CASE %WM_INITDIALOG
                              ' Initialization handler
                  
                          CASE %WM_NCACTIVATE
                              STATIC hWndSaveFocus AS DWORD
                              IF ISFALSE CB.WPARAM THEN
                                  ' Save control focus
                                  hWndSaveFocus = GetFocus()
                              ELSEIF hWndSaveFocus THEN
                                  ' Restore control focus
                                  SetFocus(hWndSaveFocus)
                                  hWndSaveFocus = 0
                              END IF
                  
                          CASE %WM_SETCURSOR
                              MyButton = GetDlgCtrlId(CB.WPARAM)
                              SELECT CASE MyButton
                                  CASE %IDC_IMGBUTTON1
                                      SELECT CASE HI(INTEGER, CB.LPARAM)
                                          CASE %WM_LBUTTONDOWN                         '&H201
                                              'put in new image here
                                              CONTROL SET IMGBUTTON CB.HNDL, %IDC_IMGBUTTON1, "#" + FORMAT$(%IDR_IMGFILE2)
                                              CONTROL REDRAW CB.HNDL, %IDC_IMGBUTTON1
                                              FUNCTION = 1
                                      END SELECT
                              END SELECT
                  
                          CASE %WM_COMMAND
                              ' Process control notifications
                              SELECT CASE AS LONG CB.CTL
                                  CASE %IDC_IMGBUTTON1
                                      IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                                          'put original image in here
                                          CONTROL SET IMGBUTTON CB.HNDL, %IDC_IMGBUTTON1, "#" + FORMAT$(%IDR_IMGFILE1)
                                          CONTROL REDRAW CB.HNDL, %IDC_IMGBUTTON1
                                          FUNCTION = 1
                                      END IF
                              END SELECT
                      END SELECT
                  END FUNCTION
                  I don't know if the redraws are necessary, just stuck 'em in there.

                  Conrad
                  Attached Files
                  Last edited by Conrad Hoffman; 3 Mar 2010, 01:58 PM.

                  Comment


                  • #10
                    That looks exactly like what I have tried, but you zip file does show it works. Only difference I see is that WM_SETCURSOR is higher up in the SELECT, but that should not matter. Must be a little typo in mine. Thanks.
                    Barry

                    Comment


                    • #11
                      Found my problem. Even though the documention in the help file is wrong, I should have caught it.
                      For CONTROL SET IMGBUTTONX
                      Purpose
                      Change the icon or bitmap displayed in an IMAGEX control. The new image is re-sized to fit the size of the control.
                      For CONTROL SET IMAGEX
                      Purpose
                      Change the icon or bitmap displayed in an IMAGEX control. The new image is re-sized to fit the size of the control.
                      So I was using CONTROL SET IMAGEX instead of CONTROL SET IMGBUTTONX
                      If I was looking for how to change a IMGBUTTONX then I would have found the correct one, but both said the same thing on purpose and I did not read futher.
                      Barry

                      Comment


                      • #12
                        Cool! Obviously I got this from you or someone else on the board, as I'm not clever enough to figure out the button down trap myself. I would have posted complete code but it was done with PBForms and would have been cluttered up for the non-forms users. PBForms is quite smart about images, but I still had to manually edit the resource file for the second image and let PBForms rebuild the resource. IMO, in this case it would have been easier without PBForms, but I use it almost exclusively now.

                        Best,
                        Conrad

                        Comment


                        • #13
                          This compilable code uses Conrad's suggestion plus shows how to get the same effect with subclassing.
                          - Subclassing allows you to respond to the space bar too.
                          Code:
                          #Compile Exe
                          #Dim All
                           
                          #Include "WIN32API.INC"
                          #Resource "ImageTest.pbr"
                           
                          %IDC_IMGBUTTON1 = 101
                          %IDC_IMGBUTTON2 = 102
                           
                          Function SubClassProc(ByVal hWnd As Dword, ByVal wMsg As Dword, _
                                                ByVal wParam As Dword, ByVal lParam As Long) As Long
                           
                            Select Case As Long wMsg
                              Case %WM_LButtonDown
                                Control Set ImgButton GetParent(hWnd), %IDC_IMGBUTTON2, "IMGFILE1"
                              Case %WM_LButtonUp
                                Control Set ImgButton GetParent(hWnd), %IDC_IMGBUTTON2, "IMGFILE2"
                           
                              Case %WM_KeyDown
                                If wParam = %VK_SPACE Then
                                  Control Set ImgButton GetParent(hWnd), %IDC_IMGBUTTON2, "IMGFILE1"
                                End If
                              Case %WM_KeyUp
                                If wParam = %VK_SPACE Then
                                  Control Set ImgButton GetParent(hWnd), %IDC_IMGBUTTON2, "IMGFILE2"
                                End If
                           
                            End Select
                           Function = CallWindowProc(GetProp (hWnd, "OldProc"), hWnd, wMsg, wParam, lParam)
                          End Function
                          '------------------/SubClassProc
                           
                          CallBack Function DlgProc()
                            Select Case As Long CbMsg
                             Static hBtn, oldProc As Dword
                              Case %WM_InitDialog                               ' Subclass button2
                                Control Handle CbHndl, %IDC_IMGBUTTON2 To hBtn
                                OldProc = SetWindowLong(hBtn, %GWL_WNDPROC, CodePtr(SubClassProc))
                                SetProp (hBtn, "OldProc", OldProc)              ' SetProp stores OldProc address
                           
                              Case %WM_SetCursor
                                Select Case GetDlgCtrlId(CbWParam)              ' ID from handle of 'window' with cursor
                                  Case %IDC_IMGBUTTON1
                                    Static Flag As Long
                                    Select Case Hi(Integer, CbLParam)           ' mouse message
                                      Case %WM_LButtonDown
                                        Flag = %True
                                        'put in new image here
                                        Control Set ImgButton CbHndl, %IDC_IMGBUTTON1, "IMGFILE2"
                                    End Select
                                  Case Else                                     ' Cursor no longer over our button 
                                    If Flag = %True Then                        ' (which is waiting for release!)
                                      Flag = %False
                                      Control Send CbHndl, %IDC_IMGBUTTON1, %BM_CLICK, 0, 0
                                    End If
                                    Function = 0 : Exit Function
                                End Select
                           
                              Case %WM_Command
                                Select Case As Long CbCtl
                                  Case %IDC_IMGBUTTON1
                                    If CbCtlMsg = %BN_Clicked Or CbCtlMsg = 1 Then
                                      'put original image in here
                                      Control Set ImgButton CbHndl, %IDC_IMGBUTTON1, "IMGFILE1"
                                    End If
                           
                                  Case %IdCancel
                                    If CbCtlMsg = %BN_Clicked Then
                                      Dialog End CbHndl, 0
                                    End If
                                End Select
                           
                              Case %WM_Close
                                SetWindowLong(hBtn, %GWL_WNDPROC, oldProc)
                            End Select
                          End Function
                          '------------------/DlgProc
                           
                          Function PBMain() As Long
                            Local hDlg  As Dword
                           
                            Dialog New 0, "ImageTest", , , 240, 150, %WS_Caption Or %WS_SysMenu To hDlg
                            Control Add ImgButton,  hDlg, %IDC_IMGBUTTON1, "IMGFILE1", 40, 40, 50, 50
                            Control Add ImgButton,  hDlg, %IDC_IMGBUTTON2, "IMGFILE2", 140, 40, 50, 50
                            Control Add Button,     hDlg, %IdCancel, "Exit", 180, 130, 50, 15
                           
                            Dialog Show Modal hDlg, Call DlgProc
                           
                          End Function
                          '------------------/PBMain
                          #If 0
                           
                          //ImageTest.rc
                           
                                           // Use your own files here
                          IMGFILE1  Bitmap "C:\WINDOWS\Gone Fishing.bmp"  //Image no.1   could be icons..
                          //IMGFILE1  Icon   "help2.ico"
                          IMGFILE2  Bitmap "C:\WINDOWS\Coffee Bean.bmp"   //Image no.2
                          //IMGFILE2  Icon   "tips.ico"
                          //..etc
                           
                          #EndIf
                          (see also http://www.powerbasic.com/support/pb...ad.php?t=40747 )
                          Rgds, Dave

                          Comment


                          • #14
                            Dave, you've nicely trapped the "slid off the button" condition that was discussed in the original post. In this application that's certainly desirable. In my motor control app, the ability to purposely slide off a depressed control and keep the motor running hands free turned out to be a useful feature. I'd never go back and "fix" it!

                            "um... er, yeah, I meant for it to work that way"

                            Best,
                            Conrad

                            Comment


                            • #15
                              Originally posted by Dave Biggs View Post
                              This compilable code uses Conrad's suggestion plus shows how to get the same effect with subclassing.
                              - Subclassing allows you to respond to the space bar too.

                              (see also http://www.powerbasic.com/support/pb...ad.php?t=40747 )
                              Your code shows exactly what I need to do. Thanks.
                              Barry

                              Comment

                              Working...
                              X