GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2008, 11:12
toolmanx toolmanx is offline
New Member
 
Join Date: Jan 2008
Posts: 3
toolmanx is on a distinguished road

CreateDIBitmap won't paint


I am running AMD XP SP1. I compile with Borland using .cpp extentions. I've added info on my extension to explain why I posted in a C++ forum. If you complile to test my code, "Bitmap" can be any bitmap with a bitcount of 24.

I'm having trouble getting CreateDIBitmap to work for me. The hex bits from my bitmap are saved in memory after my 3rd ReadFile. I can see them using SoftIce.

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.

I've read that Windows just sets lpvBits to zero and I must fill lpvBits with the data from the bitmap. I'm guessing I have filled it, but maybe not.

Incidentally, 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.

If I can successfully paint this new bitmap, I want to manipulate the bits before the paint. I have not included the code I've written for that to keep this size down. I have included all the basic program in case my declarations ect. may be my problem.

CPP / C++ / C Code:
#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";
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);

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);

} 
EndPaint(hwndMainFrame, &ps); 
break;
 
case WM_DESTROY:

DeleteDC(hdcMem);
GlobalUnlock(hmem0);
GlobalUnlock(hmem1);
GlobalUnlock(hmem2);
GlobalUnlock(hmem3);
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
Last edited by admin : 06-Jan-2008 at 02:09. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 05-Jan-2008, 15:23
toolmanx toolmanx is offline
New Member
 
Join Date: Jan 2008
Posts: 3
toolmanx is on a distinguished road

Re: CreateDIBitmap won't paint


I finally got my bitmap to paint using StrechDIBits. Now all I have to do is figure out why that works and none of the other API's I tried, did.

Consider this thread solved.
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Paint Simulated Mouse Click Fromethius CPP / C++ Forum 1 23-Aug-2007 19:03
Paint help bilbo123 Java Forum 1 11-Jun-2006 19:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.