You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
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.
#Compile Exe
#Dim All
#Resource "StaticIcon.pbr"
'//StaticIcon.rc
'
'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
'------------------/DlgProc
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
'------------------/PbMain
There is the Graphic ImageList staement in PBWin 9. For drawing an icon or bitmap from an ImageList onto a Graphic Control or Window.
Code:
...
' 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
...
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.
Code:
#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
mBinDataStuff
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
mBinDataStuff
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_VISIBLE or %DS_MODALFRAME or %DS_3DLOOK or %DS_NOFAILCREATE or %DS_SETFONT, %WS_EX_CONTROLPARENT or %WS_EX_LEFT 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
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
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment