Date: 02-16-2022
Return to Index
created by gbSnippets
'In some cases, you might need to know whether a particular Bitmap
'handle references a DIB section Or a device-dependent bitmap.
'To discern between the two Bitmap types, call GetObject() on the
'handle and get it to fill out a Bitmap structure. The bmBits field
'of the Bitmap structure will contain a pointer to the surface of
'the DIB section if the handle references a DIB section and NULL
'if the handle references a device-dependent Bitmap (DDB).
'Primary Code:
'Supply a bitmap handle to the GetObject API will direct it to fill
'out a BITMAP variable. Use the .bmBits member to determine if the
'handle was a DIB or DDB. A value of zero indicates a DDB. All other
'values indicates a DIB.
Local bm As Bitmap
GetObject hBMP, SizeOf(bm), bm
If bm.bmBits Then MsgBox ("DIB") Else MsgBox ("DDB")
'If you want to test whether a device context has a DIB or a DDB, then
'use the GetCurrentObject to return a handle to the bitmap currently selected
'into the device context. Then use GetObject as above to test whether
'returned handle is a DIB or DDB
hBMP = GetCurrentObject (hDC, %Obj_Bitmap)
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg, hDC, hBMP As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,250, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Control Add Button, hDlg, 300,"Push", 50,40,100,20
Control Add Graphic, hDlg, 200,"", 50,70,100,100, %WS_Border
Graphic Attach hDlg, 200
Graphic Render "cowgirl.bmp", (0,0)-(99,99)
Graphic Get DC To hDC
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local bm As Bitmap, hDIBSection As DWord, P As DWord, BI as BitmapInfo
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
'get bitmap handle from device context, test for DIB/DDB
hBMP = GetCurrentObject (hDC, %Obj_Bitmap)
GetObject hBMP, SizeOf(bm), ByVal VarPTR(bm)
If bm.bmBits Then MsgBox ("DIB") Else MsgBox ("DDB")
End If
If CB.Msg = %WM_Command AND CB.Ctl = 300 AND CB.Ctlmsg = %BN_Clicked Then
'Create DIBSection, put into hDC
BI.bmiHeader.biSize = 40 : BI.bmiHeader.biWidth = 100
BI.bmiHeader.biHeight = 100 : BI.bmiHeader.biPlanes = 1
BI.bmiHeader.biBitCount = 32 : BI.bmiHeader.biCompression = %BI_RGB
hDIBSection = CreateDIBSection(hDC, BI, %DIB_RGB_COLORS, VarPTR(P), 0, 0)
SelectObject hDC, hDIBSection
'get bitmap handle from device context, test for DIB/DDB
hBMP = GetCurrentObject (hDC, %Obj_Bitmap)
GetObject hBMP, SizeOf(bm), ByVal VarPTR(bm)
If bm.bmBits Then MsgBox ("DIB") Else MsgBox ("DDB")
End If
End Function
'gbs_00471
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm