No announcement yet.

drawing icon on static or graphic control

  • Filter
  • Time
  • Show
Clear All
new posts

  • drawing icon on static or graphic control

    What I want to do is to draw a 16x16 icon on a static control. The closest I have got is to draw it on a GRAPHIC control (to save having to write WM_PAINT and WM_ERASEBKGND handlers), but using DrawIconEX appears to be scaling the graphic which makes it too big and centered too far from its origin to make any sense.

    Any suggestions?

  • #2
    Why, the static can show images right away.


    • #3
      Originally posted by Edwin Knoppert View Post
      Why, the static can show images right away.
      I know. I can convert the icon to a bitmap. Just wondered why it doesn't work.


      • #4
        BMP's and ICONs i meant..

        have you read msdn?


        • #5
          Something like this?
          #Compile Exe
          #Dim All
          #Resource "StaticIcon.pbr"
           'ResIcon  ICON   "ResIcon.ico"
          #Include "WIN32API.INC"
          %BTN_Test      = 102
          %IMG_Icon      = 101
          CallBack Function DlgProc()
           Static hIcon As Dword
            Select Case As Long CbMsg
              Case %WM_INITDIALOG
                'Control Set Image CbHndl, %IMG_Icon, "ResIcon"
              Case %WM_COMMAND
                Select Case As Long CbCtl
                  Case %BTN_Test
                    If CbCtlMsg = %BN_CLICKED Or CbCtlMsg = 1 Then
                      hIcon = LoadImage(BYVAL %NULL, "FileIcon.ico", %IMAGE_ICON, 16, 16, %LR_LOADFROMFILE )
                      Control Send CbHndl, %IMG_Icon, %STM_SETIMAGE, %IMAGE_ICON, hIcon
                    End If
                End Select
              Case %WM_SYSCOMMAND
                If (CbWParam AND &HFFF0) = %SC_CLOSE Then
                  Dialog End CbHndl
                End If
              Case %WM_DESTROY
                DestroyIcon hIcon
            End Select
          End Function
          Function PBMain()
           Local hDlg  As Dword
            Dialog New 0, "Test", , , 200, 120, %WS_CAPTION Or %WS_SYSMENU, To hDlg
            ' Load image from resource file
            Control Add Image, hDlg, %IMG_Icon, "ResIcon", 95, 40, 10, 10, %WS_CHILD Or %WS_VISIBLE Or %SS_ICON
            Control Add Button,  hDlg, %BTN_Test, "Test", 75, 60, 50, 15
            Dialog Show Modal hDlg, Call DlgProc
          End Function
          Rgds, Dave


          • #6
            There is the Graphic ImageList staement in PBWin 9. For drawing an icon or bitmap from an ImageList onto a Graphic Control or Window.

              ' Create a new ImageList
              ImageList New Icon 32, 32, 32, 4 To hIL
              ' Add an icon to the ImageList, could be done from a resource
              ImageList Add Icon hIL, "cancel.ico"
              Graphic Attach hGrCtrl, GrCtrlID
              ' Copy the Icon from the ImageList to the attached graphic control/window
              Graphic ImageList (1, 1), hIL, 1, 0, %ILD_NORMAL   

            Steve Rossell
            PowerBASIC Staff


            • #7
              Just let windows do it:

              // myprog.rc 
              MYICON ICON  "filename.ico"
              DLG_SELECT_NEW_JOB_TYPE DIALOG 89, 41, 164, 115
              CAPTION "New Batch Job "
              FONT 10, "MS Sans Serif"
                  CONTROL         "", IDN_JOB_TYPE, "ListBox", LBS_NOTIFY | LBS_SORT |
                                  WS_BORDER | WS_VSCROLL | WS_TABSTOP, 2, 15, 160, 40
                  CONTROL         "Job type ", -1, "Static", WS_GROUP, 5, 5, 31, 8
                  CONTROL         "MYICON", -1, "Static", WS_GROUP|SS_ICON, 2, 66, 47, 8 
                 CONTROL         "", IDN_JOB_NAME, "Edit", ES_AUTOHSCROLL | WS_BORDER |
                                  WS_TABSTOP, 53, 65, 108, 12
                  CONTROL         "OK", IDOK, "Button", WS_TABSTOP, 27, 92, 40, 14
                  CONTROL         "Cancel", IDCANCEL, "Button", WS_TABSTOP, 88, 92, 40, 14
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]


              • #8
                I think using DDT it's even easier, doesn't an IMAGE or IMAGEX control do this for you?

                Or with either SDK or DDT style coding, send STM_SETICON to control with SS_ICON style to change 'on the fly'

                (Darned. Now I am suggesting the 'easy' way!)
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]


                • #9
                  Thanks Edwin, Dave, Steve, Michael. the SS_ICON style does it, though one can draw an icon on the DC without it. I would rather use a LABEL than a GRAPHIC control because the mouse is handled differently on a GRAPHIC - at least, it is on a GRAPHIC WINDOW.

                  Here is a little application which illustrates the problem:

                  The icons are loaded from data statements (BINBAS, Edwin Knoppert).

                  One is used as the application icon - shows in style ICON_SMALL.

                  Both are sent to LABEL controls where they show ICON_BIG.

                  #compile exe
                  #dim all
                      #include "WIN32API.INC"
                  %IDD_DIALOG1 =  101
                  %IDC_LABEL1  = 1001
                  %IDC_LABEL2  = 1002
                  '// Icon loading    BINBAS from Edwin Knoppert
                  type TAGICONDIR
                      idReserved  as word '// Reserved (must be 0)
                      idType      as word '// Resource Type (1 For icons)
                      idCount     as word '// How many images?
                  end type
                  type TAGICONDIRENTRY
                      bWidth          as byte     '// Width, In Pixels, of the Image
                      bHeight         as byte     '// Height, In Pixels, of the Image
                      bColorCount     as byte     '// Number of colors In Image (0 If >=8bpp)
                      bReserved       as byte     '// Reserved ( must be 0)
                      wPlanes         as word     '// Color Planes
                      wBitCount       as word     '// Bits per pixel
                      dwBytesInRes    as dword    '// How many bytes In this resource?
                      dwImageOffset   as dword    '// Where In the file is this image?
                  end type
                  '// Creates an icon using plain filedata, like the 766 Bytes .ICO files.
                  '// Returns a iconhandle.
                  function SetIconFileBits( byval lpMem as long ) as long
                      dim pIconDir        as TAGICONDIR ptr
                      dim IconDirEntry    as TAGICONDIRENTRY ptr
                      pIconDir = lpMem
                      if @pIconDir.idCount < 1 then exit function
                      IconDirEntry = pIconDir + len( @pIconDir )
                      function = CreateIconFromResource( _
                            byval pIconDir + @IconDirEntry.dwImageOffset _
                          , @IconDirEntry.dwBytesInRes _
                          , @pIconDir.idType _
                          , &H30000& _
                  end function
                  macro mBinDataStuff
                      local a as long
                      local t, t2 as string
                      for a = 1 to datacount: T = T & read$( a ): next a
                      for a = 1 to len( T ) step 2
                          T2 = T2 & chr$( val( "&H" & mid$( T, a , 2 ) ) )
                      next a
                      function = strptr(T2)
                  end macro
                  ' dbcolumn data
                  function BinBasTickIcon( ) as dword
                      data 0000010001001010100001000400280100001600000028000000100000002000000001
                      data 0004000000000000000000000000000000000000000000000000000000000000FF0000
                      data 00FF000000000000000000000000000000000000000000000000000000000000000000
                      data 0000000000000000000000000000000000000000000000000000000000000000000000
                      data 0000000000000000000000000000000000000000000000000000000011000000000000
                      data 0011100000000000011011000000000011000110000000000000001100000000000000
                      data 0010000000000000000100000000000000000000000000000000000000000000000000
                      data 000000000000000000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000F3FF0000F1FF
                      data 0000E4FF0000CE7F0000FF3F0000FFDF0000FFEF0000FFFF0000FFFF0000FFFF0000FF
                      data FF0000
                  end function
                  ' dbcolumn data
                  function BinBasCrossIcon( ) as dword
                  data 0000010001001010100001000400280100001600000028000000100000002000000001
                  data 000400000000000000000000000000000000000000000000000000000000000000FF00
                  data 0000000000000000000000000000000000000000000000000000000000000000000000
                  data 0000000000000000000000000000000000000000000000000000000000000000000000
                  data 0000000000000000000000000000000000000000000000000000000010000001000000
                  data 0011000010000000000110010000000000001110000000000000011000000000000110
                  data 1100000000001100011000000000110000110000000000000000000000000000000000
                  data 000000000000000000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000F7EF0000F3DF
                  data 0000F9BF0000FC7F0000FE7F0000F93F0000F39F0000F3CF0000FFFF0000FFFF0000FF
                  data FF0000
                  end function
                  callback function CBProc()
                      static htickicon, hcrossicon as dword
                      select case as long cbmsg
                          case %WM_INITDIALOG
                              hTickIcon = setIconFileBits(BinbasTickIcon)
                              hCrossIcon = setIconFileBits(BinbasCrossIcon)
                              dialog send cbhndl, %WM_SETICON, %icon_big, hCrossIcon
                              control send cbhndl, %IDC_LABEL2, %STM_SETICON, hCrossIcon, 0
                              control send cbhndl, %IDC_LABEL1, %STM_SETICON, hTickIcon, 0
                      end select
                  end function
                  function pbmain()
                      local lRslt as long
                      local hDlg  as dword
                      dialog new 0, "Icons", 167, 116, 90, 30, %WS_POPUP or %WS_BORDER or %WS_DLGFRAME or %WS_SYSMENU or %WS_CLIPSIBLINGS or _
                          %WS_EX_LTRREADING or %WS_EX_RIGHTSCROLLBAR, to hDlg
                      control add label, hDlg, %IDC_LABEL1, "Label1", 15, 10, 11, 11, %ss_icon
                      control add label, hDlg, %IDC_LABEL2, "Label2", 55, 10, 11, 11, %ss_icon
                      dialog show modal hDlg, call CBProc
                  end function


                  • #10
                    To get rid of the structures you may also use:

                    Function SetIconFileBits( ByVal pData As Dword Ptr ) As Long
                        pData = pData + 14
                        Function = CreateIconFromResourceEx( ByVal pData - 14 + @pData[1], @pData[0], 1, &H30000&, 0, 0, %LR_DEFAULTCOLOR )
                    End Function


                    • #11
                      Thank you Edwin.