Date: 02-16-2022
Return to Index
created by gbSnippets
'PowerBASIC has no intrinsic support for JPG/GIF files, but many programmers
'need at least the ability to load non-.bmp files, if not to save them!
'This example shows how to use the ListView control to load the images, but
'does not take it one step further - to put the image into a memory bitmap
'or other graphic control.
'Primary Code:
'Compilable Example: (Jose Includes)
'Credit: Chris Boss
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "commctrl.inc"
%ID_Timer = 500
Global hDeskTopDC, hDlg, hMemDC, hBMP, hMemOld, hListView, hDC_ListView, hDC_Graphic As DWord
Global ColorMarker As Long
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Graphic, hDlg, 300, "", 0,0,200,200
Graphic Attach hDlg, 300
Graphic Get DC To hDC_Graphic
Control Add ListView, hDlg, 200, "", 510,70,200,200, %LVS_SmallIcon 'ListView at top of dialog
Control Handle hDlg, 200 To hListView
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_Timer
ChrisInstructions
KillTimer CB.Hndl, %ID_Timer 'Don't need it any more
Case %WM_InitDialog
ColorMarker = Rgb(255,254,253)
LoadBackgroundImage
SetTimer(hDlg, %ID_Timer, 0&, %NULL) 'sends %WM_Timer to dialog callback
Case %WM_Destroy
ReleaseDC %Null, hDesktopDC
DeleteDC hMemDC
DeleteObject hBMP
End Select
End Function
Sub ChrisInstructions
Local x,y, iResult As Long
hDeskTopDC = GetDC(%Null)
hMemDC = CreateCompatibleDC(hDeskTopDC) 'memory DC
hBMP = CreateCompatibleBitmap(hDeskTopDC, 200,200) 'memory bitmap same size as ListView
hMemOld = SelectObject(hMemDC, hBMP) 'select bitmap into memory DC
SendMessage hListView, %WM_Print, hMemDC, %PRF_Client Or %PRF_EraseBkGnd
Do : iResult = GetPixel(hMemDC,x,0) : Incr x : Loop Until iResult = ColorMarker
Do : iResult = GetPixel(hMemDC,0,y) : Incr y : Loop Until iResult = ColorMarker
BitBlt hDC_Graphic, 0,0,x-2,y-2, hMemDC, 0,0, %SRCCopy 'copy memory DC to Grpahic Control DC
hBMP = SelectObject(hMemDC, hMemOld) 'unselect bitmap out of memory dc
Graphic Redraw
End Sub
Sub LoadBackgroundImage
Local lvbi As LVBKIMAGE, szFile As Asciiz*%MAX_PATH, lvi As LVITEM
Control Send hDlg, 200, %LVM_SetBkColor, 0, ColorMarker
szFile = Exe.Path$ + "garyface.gif"
lvbi.ulFlags = %LVBKIF_STYLE_NORMAL Or %LVBKIF_SOURCE_URL
lvbi.pszImage = VarPTR(szFile)
lvbi.cchImageMax = Len(szFile)
Control Send hDlg, 200, %LVM_SetBkImage, 0, VarPTR(lvbi)
End Sub
'gbs_00527
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm