Click here to Skip to main content
15,921,279 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralA question about using COM object. Pin
bin89225-Apr-04 6:50
bin89225-Apr-04 6:50 
GeneralRe: A question about using COM object. Pin
Antony M Kancidrowski6-Apr-04 1:28
Antony M Kancidrowski6-Apr-04 1:28 
GeneralWQL in VC++ Pin
roadragedave5-Apr-04 6:05
roadragedave5-Apr-04 6:05 
GeneralRe: WQL in VC++ Pin
David Crow5-Apr-04 7:42
David Crow5-Apr-04 7:42 
GeneralCookies Pin
Anonymous5-Apr-04 5:42
Anonymous5-Apr-04 5:42 
QuestionHow to Load Bitmap Dynamically Pin
Azghar Hussain5-Apr-04 5:25
professionalAzghar Hussain5-Apr-04 5:25 
AnswerRe: How to Load Bitmap Dynamically Pin
Mike Dimmick5-Apr-04 6:33
Mike Dimmick5-Apr-04 6:33 
AnswerRe: How to Load Bitmap Dynamically Pin
avenger_sb255-Apr-04 6:43
avenger_sb255-Apr-04 6:43 
Well, i am not about MFC, but using API u can do it the following way. Go through the code and i hope you can easily port it to MFC.

If you want to dynamically load a bitmap included as a resouce, use the following code:
<br />
HDC hdc,bdc;<br />
bdc=CreateCompatibleDC(hdc);<br />
hBmp=LoadBitmap(gloInst,"BMP_SYSTRAY");//gloinst is the hInstance parameter of WinMain and "BMP_SYSTRAY" is the string identifying the bitmap resource<br />
SelectObject(bdc,hBmp);<br />
BitBlt(hdc,0,0,200,100,bdc,0,0,SRCCOPY);<br />
DeleteDC(bdc);

The potential problem with this approach is that you cant pick bitmaps from the disk(any file) and bitmaps that have more than 256 colors.


Alternatively if you want to dynamically load any bitmap of any size and any bit-depth, use the following code:

When you process the WM_PAINT message, use the following code to draw the bitmap:
static BITMAPFILEHEADER * pbmfh ;
static BITMAPINFO * pbmi ;
static BYTE * pBits ;
static int cxDib, cyDib ;
static TCHAR szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
HDC hdc ;
PAINTSTRUCT ps ;

WM_PAINT://For MFC use OnPaint(){//insert the following code}
hdc = BeginPaint (hwnd, &ps) ;
pbmfh = DibLoadImage (szFileName) ;//DEFINITION FOR THIS IS GIVEN BELOW, "szFileName is the PATH OF THE ".BMP" FILE e.g."c:\\test.bmp"
if (pbmfh == NULL)
{
MessageBox (hwnd, TEXT ("Cannot load DIB file"),"error",0) ;
return 0 ;
}
// Get pointers to the info structure & the bits
pbmi = (BITMAPINFO *) (pbmfh + 1) ;
pBits = (BYTE *) pbmfh + pbmfh->bfOffBits ;

// Get the DIB width and height
if (pbmi->bmiHeader.biSize == sizeof (BITMAPCOREHEADER))
{
cxDib = ((BITMAPCOREHEADER *) pbmi)->bcWidth ;
cyDib = ((BITMAPCOREHEADER *) pbmi)->bcHeight ;
}
else
{
cxDib = pbmi->bmiHeader.biWidth ;
cyDib = abs (pbmi->bmiHeader.biHeight) ;
}
SetDIBitsToDevice (hdc,
0, // xDst
0, // yDst
cxDib, // cxSrc
cyDib, // cySrc
0, // xSrc
0, // ySrc
0, // first scan line
cyDib, // number of scan lines
pBits,
pbmi,
DIB_RGB_COLORS) ;
EndPaint (hwnd, &ps) ;


BITMAPFILEHEADER * DibLoadImage (PTSTR pstrFileName)<br />
{<br />
     BOOL               bSuccess ;<br />
     DWORD              dwFileSize, dwHighSize, dwBytesRead ;<br />
     HANDLE             hFile ;<br />
     BITMAPFILEHEADER * pbmfh ;<br />
<br />
     hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL,<br />
                         OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;<br />
<br />
     if (hFile == INVALID_HANDLE_VALUE)<br />
          return NULL ;<br />
<br />
     dwFileSize = GetFileSize (hFile, &dwHighSize) ;<br />
<br />
     if (dwHighSize)<br />
     {<br />
          CloseHandle (hFile) ;<br />
          return NULL ;<br />
     }<br />
<br />
     pbmfh = malloc (dwFileSize) ;<br />
     if (!pbmfh)<br />
     {<br />
          CloseHandle (hFile) ;<br />
          return NULL ;<br />
     }<br />
<br />
     bSuccess = ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL) ;<br />
     CloseHandle (hFile) ;<br />
     if (!bSuccess || (dwBytesRead != dwFileSize)         <br />
                   || (pbmfh->bfType != * (WORD *) "BM") <br />
                   || (pbmfh->bfSize != dwFileSize))<br />
     {<br />
          free (pbmfh) ;<br />
          return NULL ;<br />
     }<br />
     return pbmfh ;<br />
}




Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
AnswerRe: How to Load Bitmap Dynamically Pin
GflPower5-Apr-04 16:12
GflPower5-Apr-04 16:12 
QuestionHow to derive a MFC class from CMenu?? Pin
CPAVG5-Apr-04 4:45
CPAVG5-Apr-04 4:45 
AnswerRe: How to derive a MFC class from CMenu?? Pin
Antony M Kancidrowski5-Apr-04 5:03
Antony M Kancidrowski5-Apr-04 5:03 
GeneralRe: How to derive a MFC class from CMenu?? Pin
CPAVG5-Apr-04 21:30
CPAVG5-Apr-04 21:30 
GeneralRe: How to derive a MFC class from CMenu?? Pin
Antony M Kancidrowski6-Apr-04 0:56
Antony M Kancidrowski6-Apr-04 0:56 
GeneralRe: How to derive a MFC class from CMenu?? Pin
CPAVG7-Apr-04 5:11
CPAVG7-Apr-04 5:11 
GeneralLinking a DLL file to MFC application Pin
chemipoo5-Apr-04 3:47
chemipoo5-Apr-04 3:47 
GeneralRe: Linking a DLL file to MFC application Pin
Antony M Kancidrowski5-Apr-04 4:53
Antony M Kancidrowski5-Apr-04 4:53 
GeneralRe: Linking a DLL file to MFC application Pin
george ivanov5-Apr-04 4:54
george ivanov5-Apr-04 4:54 
GeneralRe: Linking a DLL file to MFC application Pin
chemipoo5-Apr-04 5:50
chemipoo5-Apr-04 5:50 
QuestionLoad DLL from other comp? Pin
george ivanov5-Apr-04 3:24
george ivanov5-Apr-04 3:24 
AnswerRe: Load DLL from other comp? Pin
David Crow5-Apr-04 4:42
David Crow5-Apr-04 4:42 
AnswerRe: Load DLL from other comp? Pin
Navin5-Apr-04 9:07
Navin5-Apr-04 9:07 
GeneralVideo Capture Card Pin
Flea Sp995-Apr-04 3:22
Flea Sp995-Apr-04 3:22 
GeneralMachine Idle Time Pin
Anonymous5-Apr-04 3:10
Anonymous5-Apr-04 3:10 
GeneralRe: Machine Idle Time Pin
Michael Dunn5-Apr-04 4:56
sitebuilderMichael Dunn5-Apr-04 4:56 
GeneralRenaming a project Pin
Trollslayer5-Apr-04 2:59
mentorTrollslayer5-Apr-04 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.