Click here to Skip to main content
15,914,070 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralHelp me please!!! Deconvolution Pin
Angeluna4-Dec-03 3:57
sussAngeluna4-Dec-03 3:57 
GeneralRe: Help me please!!! Deconvolution Pin
BaldwinMartin4-Dec-03 5:44
BaldwinMartin4-Dec-03 5:44 
Generalbest place to delete "this" Pin
ns4-Dec-03 3:21
ns4-Dec-03 3:21 
GeneralRe: best place to delete "this" Pin
David Crow4-Dec-03 4:03
David Crow4-Dec-03 4:03 
GeneralRe: best place to delete "this" Pin
BaldwinMartin4-Dec-03 5:20
BaldwinMartin4-Dec-03 5:20 
GeneralMAPI Pin
Jump_Around4-Dec-03 2:33
Jump_Around4-Dec-03 2:33 
GeneralRe: MAPI Pin
David Crow4-Dec-03 4:05
David Crow4-Dec-03 4:05 
GeneralURGENT !! Please Need help printing bitmap Pin
crazydave224-Dec-03 1:34
crazydave224-Dec-03 1:34 
Sigh | :sigh: I have been trying for the last week to print a bitmap in colour unsuccessfully. If anyone can help I would be eternally grateful. The bitmap prints but only in black and white. Below is the code I am using:

BOOL CControlImpresion::ImprimeAImpresora()
{
HDC hdcPrinter;
HDC hdcBmp;
HDC hdc2Bmp;
HBITMAP hOldBitmap;
HBITMAP hOldBitmap2;
HBITMAP hBMP;
HANDLE hPRN;
LONG dpBytesNeeded;
LPDEVMODE lpDevMode;
DOCINFO DocInfo;
SIZE PrinterDimensions;
SIZE BitmapDimensions;
POINT PrinterPosition;
BITMAP Bitmap;

RECT BMPSize;
HDC hDC;
HDC hMemDC;
HGLOBAL hMem;
HBITMAP hNewBitmap;
HBITMAP hBitmap;
BITMAPINFOHEADER BitmapInfoHeader;
LPBITMAPINFOHEADER lpBitmapInfo;
BITMAPINFO BitmapInfo;
LPVOID lpBitBuffer;

/********************************************************************************************/
// ABRIMOS LA IMPRESORA Y OBTENEMOS SU DC
/********************************************************************************************/
// Obtenemos un handle a la impresora:
if (!OpenPrinter((LPTSTR)m_szPrinterName,(LPHANDLE)&hPRN,NULL))
return FALSE;

// Hacer que imprima apaisado y el número de copias establecido
dpBytesNeeded = DocumentProperties(NULL,hPRN,(LPSTR)m_szPrinterName,NULL,NULL,0);
if (!(lpDevMode = (LPDEVMODE)LocalAlloc(LPTR,dpBytesNeeded)))
return FALSE;
if (DocumentProperties(NULL,hPRN,(LPSTR)m_szPrinterName,(PDEVMODE)lpDevMode,NULL,DM_OUT_BUFFER) != IDOK)
return FALSE;
lpDevMode->dmOrientation = DMORIENT_LANDSCAPE;
lpDevMode->dmCopies = m_iNumeroCopias;
lpDevMode->dmFields = DM_ORIENTATION | DM_COPIES;
if (DocumentProperties(NULL,hPRN,(LPSTR)m_szPrinterName,(PDEVMODE)lpDevMode,(PDEVMODE)lpDevMode,DM_IN_BUFFER|DM_OUT_BUFFER) != IDOK)
return FALSE;

// Creamos el DC para la impresora:
if (!(hdcPrinter = CreateDC(m_szPrinterDriver,m_szPrinterName,NULL,(CONST DEVMODE *)lpDevMode)))
return FALSE;
/*********************************************************************************************/

// Obtenemos las dimensiones de la impresora
PrinterDimensions.cx = GetDeviceCaps(hdcPrinter, HORZRES);
PrinterDimensions.cy = GetDeviceCaps(hdcPrinter, VERTRES);

/*********************************************************************************************/
// OBTENEMOS LA CAPTURA DE LA PANTALLA
/********************************************************************************************/
hDC = CreateDC("DISPLAY",0,0,0);
BMPSize.left = BMPSize.top = 0;
BMPSize.right = GetDeviceCaps(hDC, HORZRES);
BMPSize.bottom = GetDeviceCaps(hDC, VERTRES);

hMemDC = CreateCompatibleDC(hDC);

hNewBitmap = CreateCompatibleBitmap(hDC,BMPSize.right,BMPSize.bottom);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hNewBitmap);
if (!BitBlt(hMemDC,0,0,BMPSize.right,BMPSize.bottom,hDC,jmx_xstartcoord,0,SRCCOPY))
return FALSE;
/********************************************************************************************/

/*********************************************************************************************/
// CREAMOS UN BITMAP COMPATIBLE CON EL DC DE LA IMPRESORA A PARTIR DE LA CAPTURA
/*********************************************************************************************/
BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfoHeader.biWidth = BMPSize.right;
BitmapInfoHeader.biHeight = BMPSize.bottom;
BitmapInfoHeader.biPlanes = 1;
BitmapInfoHeader.biBitCount = 16;
BitmapInfoHeader.biCompression = BI_RGB;
BitmapInfoHeader.biSizeImage = 0;
BitmapInfoHeader.biXPelsPerMeter = 0;
BitmapInfoHeader.biYPelsPerMeter = 0;
BitmapInfoHeader.biClrUsed = 0;
BitmapInfoHeader.biClrImportant = 0;

BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = BMPSize.right;
BitmapInfo.bmiHeader.biHeight = BMPSize.bottom;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 16;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
BitmapInfo.bmiHeader.biSizeImage = 0;
BitmapInfo.bmiHeader.biXPelsPerMeter = 0;
BitmapInfo.bmiHeader.biYPelsPerMeter = 0;
BitmapInfo.bmiHeader.biClrUsed = 0;
BitmapInfo.bmiHeader.biClrImportant = 0;

// Establecemos la memoria para el bitmap
hMem = GlobalAlloc(GHND,BitmapInfo.bmiHeader.biSize);

// Bloqueamos la memoria y obtenemos un puntero
lpBitmapInfo = (LPBITMAPINFOHEADER)GlobalLock(hMem);

// Guardamos la info del bitmap que hemos preparado
*lpBitmapInfo = BitmapInfo.bmiHeader;

// Calculamos la dimension del bloque de memoria requerido por la imagen
GetDIBits(hMemDC,hNewBitmap,0,(UINT)BitmapInfo.bmiHeader.biHeight,NULL,(LPBITMAPINFO)lpBitmapInfo,DIB_RGB_COLORS);

BitmapInfo.bmiHeader = *lpBitmapInfo;
GlobalUnlock(hMem);

GlobalFree(hMem);

hMem = GlobalAlloc(GHND,BitmapInfo.bmiHeader.biSizeImage);

lpBitBuffer = (LPVOID)GlobalLock(hMem);

// Obtenemos los bits del bitmap
if (GetDIBits(hMemDC,hNewBitmap,0,(UINT)BitmapInfo.bmiHeader.biHeight,lpBitBuffer,(LPBITMAPINFO)&BitmapInfo.bmiHeader,DIB_RGB_COLORS) == 0)
{
// Nos salimos si hay errores
GlobalUnlock(hMem);

DeleteDC(hMemDC);
DeleteDC(hDC);

return FALSE;
}

if ((hBitmap = CreateDIBitmap(hdcPrinter,&BitmapInfoHeader,CBM_INIT,lpBitBuffer,&BitmapInfo,DIB_RGB_COLORS)) == NULL)
{
// Nos salimos si hay errores
GlobalUnlock(hMem);

DeleteDC(hMemDC);
DeleteDC(hDC);

return FALSE;
}

GlobalUnlock(hMem);

/* PBITMAPINFO pBitmapInfo;
PBITMAPINFOHEADER pBitmapInfoHeader;
pBitmapInfo = CreaBitmapInfo(hBitmap);

CreaBMP(".\\bmpFile.bmp",pBitmapInfo,hBitmap,hMemDC);

LPBYTE lpBits;
PBITMAPINFO pbi;
pbi = pBitmapInfo;
pBitmapInfoHeader = (PBITMAPINFOHEADER)pbi;

// Reservamos memoria para los bytes del bitmap
if ((lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED,pBitmapInfoHeader->biSizeImage)) == NULL)
return FALSE;

// Obtenemos el color table (RGBQUAD array) y los bits
if (!GetDIBits(hdcPrinter,hBitmap,0,(WORD)pBitmapInfoHeader->biHeight,lpBits,pbi,DIB_RGB_COLORS))
{
GlobalFree(lpBits);

return FALSE;
}*/

DeleteDC(hMemDC);
DeleteDC(hDC);

// Recuperamos la info del bitmap
if (!GetObject(hBitmap,sizeof(Bitmap),(LPSTR)&Bitmap))
return FALSE;
/*********************************************************************************************/

/*********************************************************************************************/
// AMPLIAMOS LA IMAGEN A LAS DIMENSIONES ADECUADAS A LA IMPRESORA E IMPRIMIMOS
/*********************************************************************************************/
// Calculamos las dimensiones de ampliacion para evitar que la imagen se achate
BitmapDimensions.cy = PrinterDimensions.cy;
BitmapDimensions.cx = Bitmap.bmWidth * (PrinterDimensions.cy / Bitmap.bmHeight);

// Calculamos la posicion de impresion, para que salga la imagen centrada sobre el papel
PrinterPosition.y = 0;
PrinterPosition.x = (PrinterDimensions.cx / 2) - (BitmapDimensions.cx / 2);

// DC origen con la captura
hdcBmp = CreateCompatibleDC(hdcPrinter);
hOldBitmap = (HBITMAP)SelectObject(hdcBmp,hBitmap);


/* PBITMAPINFO pBitmapInfo;
pBitmapInfo = CreaBitmapInfo(hBitmap);

CreaBMP(".\\bmpFile.bmp",pBitmapInfo,hBitmap,hdcPrinter);*/

// DC destino con la imagen ampliada
hdc2Bmp = CreateCompatibleDC(hdcBmp);
hBMP = CreateCompatibleBitmap(hdc2Bmp,BitmapDimensions.cx,BitmapDimensions.cy);
hOldBitmap2 = (HBITMAP)SelectObject(hdc2Bmp,hBMP);

// Ampliamos la captura para ajustarla al tamaño de la impresora
SetMapMode(hdc2Bmp,MM_TEXT);

if (!SetStretchBltMode(hdc2Bmp,HALFTONE))
return FALSE;

if (!SetBrushOrgEx(hdc2Bmp,0,0,NULL))
return FALSE;

if (!StretchBlt(hdc2Bmp,0,0,BitmapDimensions.cx,BitmapDimensions.cy,hdcBmp,0,0,Bitmap.bmWidth,Bitmap.bmHeight,SRCCOPY))
return FALSE;

// Establecemos la info del documento a imprimir
DocInfo.cbSize = sizeof(DOCINFO);
DocInfo.lpszDocName = "Impresión de P.I.: Captura de pantalla";
DocInfo.lpszOutput = NULL;

/* PBITMAPINFO pBitmapInfo;
pBitmapInfo = CreaBitmapInfo(hBitmap);

CreaBMP(".\\bmpFile.bmp",pBitmapInfo,hBitmap,hdcPrinter);*/

// Imprimimos
StartDoc (hdcPrinter, &DocInfo);
StartPage (hdcPrinter);

// El BitBlt funciona
if (!BitBlt(hdcPrinter,PrinterPosition.x,PrinterPosition.y,BitmapDimensions.cx,BitmapDimensions.cy,hdc2Bmp,0,0,SRCCOPY))
return FALSE;

/* PBITMAPINFO pBitmapInfo;
pBitmapInfo = CreaBitmapInfo(hBitmap);

CreaBMP(".\\bmpFile.bmp",pBitmapInfo,hBitmap,hdcPrinter);*/

// Finalizamos la impresion
EndPage (hdcPrinter);
EndDoc (hdcPrinter);

hBitmap = (HBITMAP)SelectObject(hdcBmp,hOldBitmap);
hBMP = (HBITMAP)SelectObject(hdc2Bmp,hOldBitmap2);

// Liberamos los DC
DeleteDC(hdcPrinter);
DeleteDC(hdcBmp);
DeleteDC(hdc2Bmp);

// Liberamos la memoria
LocalFree(lpDevMode);

// Cerramos la impresora
ClosePrinter(hPRN);

return TRUE;
}
Generalneed help on setting state flags Pin
coda_x4-Dec-03 1:07
coda_x4-Dec-03 1:07 
GeneralRe: need help on setting state flags Pin
David Crow4-Dec-03 4:08
David Crow4-Dec-03 4:08 
GeneralRe: need help on setting state flags Pin
coda_x4-Dec-03 20:04
coda_x4-Dec-03 20:04 
GeneralMajor bug in COleDateTime in MFC7 Pin
Anonymous4-Dec-03 0:48
Anonymous4-Dec-03 0:48 
GeneralRe: Major bug in COleDateTime in MFC7 Pin
Steve S4-Dec-03 1:26
Steve S4-Dec-03 1:26 
GeneralRe: Major bug in COleDateTime in MFC7 Pin
Mike Dimmick4-Dec-03 2:45
Mike Dimmick4-Dec-03 2:45 
GeneralSorry, it's really FindFirstFile that has the bug! Pin
Anonymous4-Dec-03 2:53
Anonymous4-Dec-03 2:53 
GeneralRe: Sorry, it's really FindFirstFile that has the bug! Pin
David Crow4-Dec-03 6:03
David Crow4-Dec-03 6:03 
GeneralRe: Sorry, it's really FindFirstFile that has the bug! Pin
Anonymous4-Dec-03 7:07
Anonymous4-Dec-03 7:07 
GeneralRe: Sorry, it's really FindFirstFile that has the bug! Pin
David Crow4-Dec-03 7:52
David Crow4-Dec-03 7:52 
GeneralRe: Sorry, it's really FindFirstFile that has the bug! Pin
Joe Woodbury4-Dec-03 15:15
professionalJoe Woodbury4-Dec-03 15:15 
GeneralRe: Sorry, it's really FindFirstFile that has the bug! Pin
Anonymous8-Dec-03 22:00
Anonymous8-Dec-03 22:00 
GeneralDDX control access in dialogs Pin
J.B.4-Dec-03 0:29
J.B.4-Dec-03 0:29 
GeneralRe: DDX control access in dialogs Pin
Steve S4-Dec-03 0:34
Steve S4-Dec-03 0:34 
GeneralRe: DDX control access in dialogs Pin
J.B.4-Dec-03 0:56
J.B.4-Dec-03 0:56 
GeneralRe: DDX control access in dialogs Pin
J.B.4-Dec-03 1:16
J.B.4-Dec-03 1:16 
GeneralRe: DDX control access in dialogs Pin
Steve S4-Dec-03 1:25
Steve S4-Dec-03 1:25 

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.