For an eBook or printed book on using the API with Liberty BASIC, see: APIs for Liberty BASIC
SelectObject
In earlier lessons we discussed the loading of bitmaps. We also discussed device contexts, both for a window and in memory.
Memory device contexts contain a default, monochrome bitmaps that is 1 pixel wide and 1 pixel high. We can select another bitmap into a memory device context with SelectObject. It looks like this:
CallDLL#gdi32,"SelectObject",_
memoryDC asuLong,_ 'handle to memory device context
hObject asuLong,_ 'handle of bitmap
oldObject asuLong'returns handle to previously selected object
The bitmap object can be a bitmap that was loaded into memory with Liberty BASIC's LOADBMP command, or with the API call to LoadImageA.
hBmp
We see that the SelectObject function requires a handle to the object to be selected. If we used the LoadImageA API call to load the image, we already have the handle. If we used Liberty BASIC's ability to load a bitmap and give it a "name" we must use the HBMP() function to retrieve the Windows handle to the bitmap. It looks like this:
It is possible to create a bitmap in memory. This is done with CreateCompatibleBitmap. It is important to remember that the memory bitmap must be created to be compatible with a display or window device context, not with a memory device context.
calldll#gdi32,"CreateCompatibleBitmap",_
hDC ASulong,_ 'window DC, NOT memory DC
nWidth ASlong,_ 'width of created bitmap
nHeight ASlong,_ 'height of created bitmap
handleBMP ASulong'returns handle if successful
The memory bitmap must be selected into the device context that was created in memory with
CreateCompatibleDC. All graphics commands to the memory device context will affect the memory bitmap that is currently selected into it.
CallDll#gdi32,"SelectObject",_
hdcMem asulong,_ 'memory device context
handleBMP asulong,_ 'handle of memory bitmap
oldBmp asulong'returns handle of previous object
A program can have many memory bitmaps. Each device context can hold only one memory bitmap at a time. A bitmap that is selected into a memory device context replaces the previous bitmap in that context. The handle to the previous bitmap is returned by the SelectObject call.
DeleteObject
When a memory bitmap is no longer needed, delete it with DeleteObject:
calldll#gdi32,"DeleteObject",_
handleBMP asulong,_ 'handle of memory bitmap
r asboolean'nonzero if successfull
Do not attempt to delete an object that is currently selected into a device context. Retain the handle of the original, default bmp from the DC and use SelectObject to select it back into the DC. When the memory bmp is not longer selected into a device context it can be deleted.
'select default bmp back into DCCallDLL#gdi32,"SelectObject",_
hMemDC asuLong,_ 'memory DC
oldBmp asuLong,_ 'handle of original, default bmp
handle asuLong'returns previously selected bitmap
Demo
The following demonstration program does not display any graphics. It is presented as a framework for working with graphics in memory. Further lessons will build on this framework.
nomainwin
winWide=700:winHigh=500WindowWidth=winWide+50:WindowHeight=winHigh+50UpperLeftX=1:UpperLeftY=1graphicbox#1.g,0,0,winWide,winHigh
open"GDI Demo"forwindowas#1#1"trapclose [quit]"#1.g "down"
h=hwnd(#1.g)'graphicbox handle'get device context for window:calldll#user32,"GetDC",_
h asulong,_ 'graphicbox handle
hdc asulong'returns handle to device contextcalldll#gdi32,"CreateCompatibleDC",_
hdc asulong,_ 'graphicbox DC
hMemDC asulong'memory DC
nWidth=100: nHeight=200calldll#gdi32,"CreateCompatibleBitmap",_
hdc ASulong,_ 'window DC, NOT memory DC
nWidth ASlong,_ 'width of created bitmap
nHeight ASlong,_ 'height of created bitmap
handleBmp ASulong'returns handle if successfulCallDLL#gdi32,"SelectObject",_
hMemDC asuLong,_ 'memory DC
handleBmp asuLong,_ 'handle of bmp
oldBmp asuLong'returns previously selected bitmapwait[quit]'select default bmp back into DCCallDLL#gdi32,"SelectObject",_
hMemDC asuLong,_ 'memory DC
oldBmp asuLong,_ 'handle of original, default bmp
handle asuLong'returns previously selected bitmapCallDLL#gdi32,"DeleteObject",_
handleBmp asuLong,_ 'handle of bmp
r AsBooleancalldll#gdi32,"DeleteDC",_
hMemDC asulong,_ 'DC to delete
re aslong'nonzero=successcalldll#user32,"ReleaseDC",_
h asulong,_ 'window handle
hdc asulong,_ 'device context
ret aslongclose#1:end
Transferring Bits
We now know how to create a device context in memory. It functions as a canvas for graphics. It contains a memory bitmap. This bitmap can be displayed on the program window with GDI API calls. We'll discuss that subject in the next lesson.
-
SelectObject | hBmp | CreateCompatibleBmp | DeleteObject | Demo | Transferring Bits
Some text below is copied from the Microsoft Developers Network Library.
For an eBook or printed book on using the API with Liberty BASIC, see:
APIs for Liberty BASIC
SelectObject
In earlier lessons we discussed the loading of bitmaps. We also discussed device contexts, both for a window and in memory.Memory device contexts contain a default, monochrome bitmaps that is 1 pixel wide and 1 pixel high. We can select another bitmap into a memory device context with SelectObject. It looks like this:
The bitmap object can be a bitmap that was loaded into memory with Liberty BASIC's LOADBMP command, or with the API call to LoadImageA.
hBmp
We see that the SelectObject function requires a handle to the object to be selected. If we used the LoadImageA API call to load the image, we already have the handle. If we used Liberty BASIC's ability to load a bitmap and give it a "name" we must use the HBMP() function to retrieve the Windows handle to the bitmap. It looks like this:CreateCompatibleBmp
It is possible to create a bitmap in memory. This is done with CreateCompatibleBitmap. It is important to remember that the memory bitmap must be created to be compatible with a display or window device context, not with a memory device context.The memory bitmap must be selected into the device context that was created in memory with
CreateCompatibleDC. All graphics commands to the memory device context will affect the memory bitmap that is currently selected into it.
A program can have many memory bitmaps. Each device context can hold only one memory bitmap at a time. A bitmap that is selected into a memory device context replaces the previous bitmap in that context. The handle to the previous bitmap is returned by the SelectObject call.
DeleteObject
When a memory bitmap is no longer needed, delete it with DeleteObject:Do not attempt to delete an object that is currently selected into a device context. Retain the handle of the original, default bmp from the DC and use SelectObject to select it back into the DC. When the memory bmp is not longer selected into a device context it can be deleted.
Demo
The following demonstration program does not display any graphics. It is presented as a framework for working with graphics in memory. Further lessons will build on this framework.Transferring Bits
We now know how to create a device context in memory. It functions as a canvas for graphics. It contains a memory bitmap. This bitmap can be displayed on the program window with GDI API calls. We'll discuss that subject in the next lesson.Transferring Bits
GDI Tutorials Home