Click here to Skip to main content
15,891,372 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Splash screen stays more than programmed timer value Pin
Nibu babu thomas24-Oct-07 18:06
Nibu babu thomas24-Oct-07 18:06 
QuestionHICON -> *.ico Pin
Lord Kixdemp24-Oct-07 16:21
Lord Kixdemp24-Oct-07 16:21 
QuestionRe: HICON -> *.ico Pin
Hamid_RT24-Oct-07 19:41
Hamid_RT24-Oct-07 19:41 
AnswerRe: HICON -> *.ico Pin
ThatsAlok25-Oct-07 1:10
ThatsAlok25-Oct-07 1:10 
GeneralRe: HICON -> *.ico Pin
Lord Kixdemp25-Oct-07 1:38
Lord Kixdemp25-Oct-07 1:38 
GeneralRe: HICON -> *.ico Pin
ThatsAlok25-Oct-07 1:52
ThatsAlok25-Oct-07 1:52 
GeneralRe: HICON -> *.ico Pin
ThatsAlok25-Oct-07 1:59
ThatsAlok25-Oct-07 1:59 
GeneralRe: HICON -> *.ico Pin
mathievp24-Mar-09 8:40
mathievp24-Mar-09 8:40 
BMP approach HICON -> BMP




// IconTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void WriteBMPFile(HBITMAP bitmap, LPTSTR filename, HDC hDC)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp;

// create the bitmapinfo header information

if (!GetObject( bitmap, sizeof(BITMAP), (LPSTR)&bmp)){
printf("Could not retrieve bitmap info");
return;
}

// Convert the color format to a count of bits.
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;


// Allocate memory for the BITMAPINFO structure.
if (cClrBits != 24)
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits));
else
pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));

// Initialize the fields in the BITMAPINFO structure.

pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1<<cClrBits);

// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;

// Compute the number of bytes in the array of color
// indices and store the result in biSizeImage.
pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8 * pbmi->bmiHeader.biHeight * cClrBits;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbmi->bmiHeader.biClrImportant = 0;

// now open file and save the data
pbih = (PBITMAPINFOHEADER) pbmi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

if (!lpBits) {
printf("writeBMP::Could not allocate memory");
return;
}

// Retrieve the color table (RGBQUAD array) and the bits
if (!GetDIBits(hDC, HBITMAP(bitmap), 0, (WORD) pbih->biHeight, lpBits, pbmi,
DIB_RGB_COLORS)) {
printf("writeBMP::GetDIB error");
return;
}

// Create the .BMP file.
hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE){
printf("Could not create file for writing");
return;
}
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;

// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);

// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, NULL)) {
printf("Could not write in to file");
return;
}

// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
+ pbih->biClrUsed * sizeof (RGBQUAD),
(LPDWORD) &dwTmp, ( NULL))){
printf("Could not write in to file");
return;
}


// Copy the array of color indices into the .BMP file.
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)){
printf("Could not write in to file");
return;
}

// Close the .BMP file.
if (!CloseHandle(hf)){
printf("Could not close file");
return;
}

// Free memory.
GlobalFree((HGLOBAL)lpBits);
}

int _tmain(int argc, _TCHAR* argv[])
{
HBITMAP hBitmap;
HICON hIcon = LoadIcon( NULL,IDI_EXCLAMATION);

ICONINFO IconInfo;
GetIconInfo(hIcon, &IconInfo);
BITMAP bm;
GetObject(IconInfo.hbmColor, sizeof(bm), &bm);

HDC hDC= NULL;
HDC dcScreen = ::GetDC(NULL);
hDC = CreateCompatibleDC(dcScreen);

hBitmap = CreateCompatibleBitmap(dcScreen, bm.bmWidth,bm.bmHeight);

::ReleaseDC(NULL, dcScreen);
HBITMAP bmOld = (HBITMAP)SelectObject(hDC, hBitmap);
DrawIcon(hDC, 0, 0, hIcon);
SelectObject(hDC, bmOld);

WriteBMPFile(hBitmap, _T("C:\\bitmaptest.bmp"), hDC );

}



- MATT
GeneralRe: HICON -&gt; *.ico Pin
nenfa15-Dec-09 22:26
nenfa15-Dec-09 22:26 
AnswerRe: HICON -> *.ico Pin
Rakafon2-Sep-10 0:28
Rakafon2-Sep-10 0:28 
QuestionFind Original Language Pin
Eli Nurman24-Oct-07 13:17
Eli Nurman24-Oct-07 13:17 
QuestionRe: Find Original Language Pin
Hamid_RT24-Oct-07 19:41
Hamid_RT24-Oct-07 19:41 
AnswerRe: Find Original Language Pin
ThatsAlok25-Oct-07 2:18
ThatsAlok25-Oct-07 2:18 
GeneralRe: Find Original Language Pin
Hamid_RT25-Oct-07 4:03
Hamid_RT25-Oct-07 4:03 
QuestionAny Window inside a specified region Pin
Identity Undisclosed24-Oct-07 13:10
Identity Undisclosed24-Oct-07 13:10 
AnswerRe: Any Window inside a specified region [modified] Pin
Hadi Dayvary24-Oct-07 20:13
professionalHadi Dayvary24-Oct-07 20:13 
QuestionVisual Studio 2005 (C/C++) Pin
MikeC_124-Oct-07 12:03
MikeC_124-Oct-07 12:03 
AnswerRe: Visual Studio 2005 (C/C++) Pin
Mark Salsbery24-Oct-07 12:30
Mark Salsbery24-Oct-07 12:30 
GeneralRe: Visual Studio 2005 (C/C++) Pin
George L. Jackson24-Oct-07 12:57
George L. Jackson24-Oct-07 12:57 
AnswerRe: Visual Studio 2005 (C/C++) Pin
Peter Weyzen24-Oct-07 13:30
Peter Weyzen24-Oct-07 13:30 
QuestionCannot debug with PDBs Pin
indigox324-Oct-07 11:50
indigox324-Oct-07 11:50 
AnswerRe: Cannot debug with PDBs Pin
Andy Moore25-Oct-07 6:26
Andy Moore25-Oct-07 6:26 
GeneralRe: Cannot debug with PDBs Pin
indigox326-Oct-07 3:32
indigox326-Oct-07 3:32 
QuestionCStdioFile save location to wrong path [modified] Pin
acerunner31624-Oct-07 9:40
acerunner31624-Oct-07 9:40 
AnswerRe: CStdioFile save location to wrong path Pin
Peter Weyzen24-Oct-07 9:45
Peter Weyzen24-Oct-07 9:45 

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.