Running AMD XP SP1 - Bitmap can be any bitmap
with a bitcount of 24. Compiling with Borland using .cpp
extentions.
I'm still having trouble getting CreateDIBitmap to
work for me. The hex bits from my bitmap are saved
in memory. I can see them.
I've read that Windows just sets lpvBits to zero
and I must fill lpvBits with the data from the
bitmap. Since memory is holding the bit information
and lpvBits appears to be pointing to it, I can't
see why CreateDIBitmap won't paint the bits to
the screen. What am I doing wrong?
Incidently, my screen is being painted black. This
tells me lpvBits is pointing to zero but I don't
know why and what to do about it.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <commctrl.h>
//***** START Prototypes
int WINAPI WinMain (HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow);
LRESULT CALLBACK MainWndProc(HWND hwndMainFrame, UINT uMsg , WPARAM wParam , LPARAM lParam);
//*******END Prototypes
//----------------------------------- GENERAL VARIABLES
HWND hwndMainFrame;
WPARAM wParam;
LPARAM lParam;
HINSTANCE hInstance;
WNDCLASSEX wc;
UINT uMsg;
MSG msg;
MSG message;
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
//--------------------------------- BITMAP VARIABLES
HANDLE hfile;
LPCTSTR lpstrFile = "C:\\MyPrograms\\BCC55\\bin\\D1.bmp";
//LPCTSTR lpstrFile = "C:\\MyPrograms\\BCC55\\bin\\Bitmap1.bmp";
LPDWORD dwRead1 = new DWORD;
LPDWORD dwRead2 = new DWORD;
LPDWORD dwRead3 = new DWORD;
LPVOID lpvBits;
HBITMAP hbitmap;
HDC hdcMem;
HGLOBAL hmem0;
HGLOBAL hmem1;
HGLOBAL hmem2;
HGLOBAL hmem3;
BITMAPFILEHEADER *fileHdr;
BITMAPINFOHEADER *infoHdr;
BITMAPINFO *lpbmi;
RGBQUAD bmiColors[1];
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{ // ************************* START WINMAIN
(void)UNREFERENCED_PARAMETER(hPrevInstance);
(void)UNREFERENCED_PARAMETER(lpCmdLine);
(void)UNREFERENCED_PARAMETER(nCmdShow);
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName="MyClass";
wc.hIconSm=LoadIcon(NULL,IDI_WINLOGO);
if(!RegisterClassEx(&wc))
return 0;
hwndMainFrame = CreateWindowEx(NULL,"MyClass","MAINFRAME",WS_VSCROLL |
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
0,0,900,700,NULL,NULL,hInstance,NULL);
if (!hwndMainFrame)
return 0;
ShowWindow(hwndMainFrame, nCmdShow);
UpdateWindow(hwndMainFrame);
while (GetMessage(&message,NULL,0,0)>0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return msg.wParam;
} // ***************** END OF WINMAIN
LRESULT CALLBACK MainWndProc(HWND hwndMainFrame, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ // ****************************** START WIND PROCEDURE 1
switch(uMsg)
{ // ********************** START SWITCH 2
case WM_CREATE:
{ // ********************** START WM_CREATE 3
hfile = CreateFile(lpstrFile, GENERIC_READ,FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_READONLY,(HANDLE) NULL);
fileHdr = (BITMAPFILEHEADER*)GlobalAlloc(GHND,sizeof(BITMAPFILEHEADER));
hmem0 = GlobalAlloc(GHND,sizeof(BITMAPFILEHEADER));
fileHdr = (BITMAPFILEHEADER*)GlobalLock(hmem0);
ReadFile(hfile,fileHdr,sizeof(BITMAPFILEHEADER),dwRead1,(LPOVERLAPPED)NULL);
hmem1 = GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER));
infoHdr = (BITMAPINFOHEADER*)GlobalLock(hmem1);
ReadFile(hfile,infoHdr,sizeof(BITMAPINFOHEADER),dwRead2,(LPOVERLAPPED)NULL);
hmem2 = GlobalAlloc(GHND,sizeof(BITMAPINFO));
lpbmi = (BITMAPINFO*)GlobalLock(hmem2);
lpbmi->bmiHeader.biSize = infoHdr->biSize;
lpbmi->bmiHeader.biWidth = infoHdr->biWidth;
lpbmi->bmiHeader.biHeight = infoHdr->biHeight;
lpbmi->bmiHeader.biPlanes = infoHdr->biPlanes;
lpbmi->bmiHeader.biBitCount = infoHdr->biBitCount;
lpbmi->bmiHeader.biCompression = infoHdr->biCompression;
lpbmi->bmiHeader.biSizeImage = infoHdr->biSizeImage;
lpbmi->bmiHeader.biXPelsPerMeter = infoHdr->biXPelsPerMeter;
lpbmi->bmiHeader.biYPelsPerMeter = infoHdr->biYPelsPerMeter;
lpbmi->bmiHeader.biClrUsed = infoHdr->biClrUsed;
lpbmi->bmiHeader.biClrImportant = infoHdr->biClrImportant;
lpbmi->bmiColors[1].rgbBlue = 0;
lpbmi->bmiColors[1].rgbGreen = 0;
lpbmi->bmiColors[1].rgbRed = 0;
lpbmi->bmiColors[1].rgbReserved = 0;
hmem3 = GlobalAlloc(GHND,(fileHdr->bfSize) - (fileHdr->bfOffBits));
lpvBits = (LPVOID)GlobalLock(hmem3);
ReadFile(hfile,&lpvBits,((fileHdr->bfSize) - (fileHdr->bfOffBits)),dwRead3,(LPOVERLAPPED)NULL);
hbitmap = CreateDIBitmap(hdc,infoHdr,CBM_INIT,lpvBits,lpbmi,DIB_RGB_COLORS);
GetClientRect(hwndMainFrame, &rect);
InvalidateRect(hwndMainFrame,NULL,TRUE);
return (0);
} // ************************** END WM_CREATE 3
case WM_PAINT:
BeginPaint(hwndMainFrame, &ps);
{
hdc = GetDC(hwndMainFrame);
char Mystring0[10];
_snprintf (Mystring0,10,"%s","Bitcount=");
TextOut(hdc,10,640,Mystring0,9);
char Mystring1[10];
_snprintf (Mystring1,10,"%X",infoHdr->biBitCount);
TextOut(hdc,90,640,Mystring1,2);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hbitmap);
BitBlt(hdc,0,0,(infoHdr->biWidth)-1200,(infoHdr->biHeight)-900,hdcMem,0,0,SRCCOPY);
DeleteDC(hdcMem);
GlobalUnlock(hmem0);
GlobalUnlock(hmem1);
GlobalUnlock(hmem2);
GlobalUnlock(hmem3);
}
EndPaint(hwndMainFrame, &ps);
break;
case WM_DESTROY:
ReleaseDC(hwndMainFrame,hdcMem);
ReleaseDC(hwndMainFrame,hdc);
DeleteObject(hbitmap);
GlobalFree(fileHdr);
GlobalFree(infoHdr);
GlobalFree(lpbmi);
GlobalFree(lpvBits);
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hwndMainFrame, uMsg, wParam, lParam));
} // ************************* END SWITCH 2
return 0;
} // ********************************* END WNDPROC 1