Snoopy07
November 1st, 2009, 04:07 PM
Hi,
I was wondering if anyone can help me with a Printing problem I am having its Win32 program using GNU gcc compiler and the codeblocks IDE.
I am using TextOut to the printer to try to print mutiple lines of text to the printer
but it just seems to printout two lines of the same text, which is not what i want it to do.
my code is
#include <math.h>
#include <stdlib.h>
#include <Windows.h>
#include <iostream>
#include <string.h>
#include <Winspool.h>
#include <CommDlg.h>
#include <stdio.h>
#include <wingdi.h>
#include <winbase.h>
// Global Variables.
double CV = 1040;
double VALUE1 = 3412;
double Total;
double Total2;
double Total3;
double Rate;
double n ;
double n2;
DIBSECTION ds;
char EditText[256];
char QuoteText[256] = "The Result is ";
char QuoteText2[256] = "The Result is ";
char QuoteText3[256] = "Gas Flow Rate ";
int EditTextLength;
char EditText2[256];
int EditTextLength2;
char szAppName [] = TEXT("GasRates");
int iWidth;
int iHeight;
HBITMAP hbm;
RECT rc;
HDC hdc ;
HDC GetPrinterDC (void)
{
// Initialize a PRINTDLG object's size and set the PD_RETURNDC flag.
// The PD_RETURNDC flag tells the dialog to return a printer device context.
PRINTDLG pd = {0};
pd.lStructSize = sizeof( pd );
pd.Flags = PD_RETURNDC;
// Retrieves the printer DC
PrintDlg(&pd);
hdc =pd.hDC;
return hdc ;
}
LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
// **********
// class CRaster
// - Generic class for BMP raster images.
class CRaster {
public:
int Width,Height; // Dimensions
int BPP; // Bits Per Pixel.
char * Raster; // Bits of the Image.
RGBQUAD * Palette; // RGB Palette for the image.
int BytesPerRow; // Row Width (in bytes).
BITMAPINFO * pbmi; // BITMAPINFO structure
// Member functions (defined later):
int LoadBMP (char * szFile);
int GDIPaint (HDC hdc,int x,int y);
};
// **********
// Windows Main Function.
// - Here starts our demo program
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, IDI_WINLOGO);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass (&wc);
hwnd = CreateWindow (szAppName,"Gas Rate Calculator",WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
CW_USEDEFAULT,CW_USEDEFAULT,218,600,
0,0,hInstance,0);
ShowWindow (hwnd,nCmdShow);
UpdateWindow (hwnd);
while (GetMessage(&msg,0,0,0))
{
if(!IsDialogMessage(hwnd, &msg))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
return msg.wParam;
}
// **********
// Main Window Procedure.
// - Processes Window Messages
LRESULT CALLBACK WindowProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
static HWND hwndEdit2;
static HWND hwndButton;
static HWND hwndButton2;
static HWND hwndButton3;
static HWND hwndLabel;
static HWND hwndLabel2;
static HWND hwndLabel3;
static HWND hwndLabel4;
static HWND hwndLabel5;
static HBITMAP hBitmap;
static int cxSource, cySource;
HDC Hdc, hdcMem;
HINSTANCE hInstance;
PAINTSTRUCT ps;
BITMAP bitmap;
HBRUSH NewBrush;
NewBrush = CreateSolidBrush(RGB(255, 0, 0));
RECT rect ;
rect.bottom = 160;
rect.left = 160;
rect.right = 160;
rect.top = 160;
switch (message)
{
case WM_CREATE:
//load bitmap from resource
hInstance = ((LPCREATESTRUCT) lParam) ->hInstance;
hBitmap = LoadBitmap (hInstance,MAKEINTRESOURCE(3));
GetObject (hBitmap, sizeof (BITMAP), &bitmap);
cxSource = bitmap.bmWidth;
cySource = bitmap.bmHeight;
HICON hICon;
hICon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(2));
SetClassLong(hwnd, // window handle
GCL_HICON, // changes icon
(LONG) hICon); // The new icon
PostMessage (hwnd, WM_SETICON, ICON_BIG, (LPARAM) hICon);
hwndLabel5 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,400,200,30,hwnd,(HMENU) 9,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel4 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,350,200,30,hwnd,(HMENU) 8,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel3 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,300,200,25,hwnd,(HMENU) 7,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel2 = CreateWindow ( TEXT ("static"),"Enter Time Taken in Seconds.",WS_CHILD|WS_VISIBLE|ES_LEFT|1,7,350,200,25,hwnd,(HMENU) 5,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel = CreateWindow ( TEXT ("static"),"Enter Volume in Cubic Feet.",WS_CHILD|WS_VISIBLE|ES_LEFT|1,7,260,180,25,hwnd,(HMENU) 6,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndButton = CreateWindow ( TEXT ("button"),"Calculate",WS_CHILD|WS_VISIBLE|WS_TABSTOP|1,55,500,100,25,hwnd,(HMENU) 3,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndButton2 = CreateWindow ( TEXT ("button"),"Back",WS_CHILD|1,55,500,100,25,hwnd,(HMENU) 4,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndButton3 = CreateWindow ( TEXT ("button"),"Print",WS_CHILD|1,55,465,100,25,hwnd,(HMENU) 10,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndEdit = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| WS_VISIBLE| ES_LEFT|WS_BORDER| WS_TABSTOP,20, 300, 50, 20, hwnd,
(HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
hwndEdit2 = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| WS_VISIBLE| ES_LEFT| WS_BORDER| WS_TABSTOP,20, 370, 75, 20, hwnd,
(HMENU) 2,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
SetFocus (hwndEdit);
return 0;
case WM_CTLCOLORSTATIC:
SetBkMode(hdc,TRANSPARENT);
return (LRESULT)CreateSolidBrush(0xFFFFFF);
case WM_SETFOCUS:
SetFocus (hwndEdit);
return 0 ;
case WM_COMMAND:
switch (wParam)
{
case 3 :
EditTextLength = GetWindowTextLength(hwndEdit) +1;
GetWindowText(hwndEdit, EditText, EditTextLength);
EditTextLength2 = GetWindowTextLength(hwndEdit2) +1;
GetWindowText(hwndEdit2, EditText2, EditTextLength2);
ShowWindow(hwndEdit, SW_HIDE);
ShowWindow(hwndEdit2, SW_HIDE);
ShowWindow(hwndButton, SW_HIDE);
ShowWindow(hwndLabel, SW_HIDE);
ShowWindow(hwndLabel2, SW_HIDE);
ShowWindow(hwndButton2, SW_SHOW);
ShowWindow(hwndButton3, SW_SHOW);
ShowWindow(hwndLabel3, SW_SHOW);
ShowWindow(hwndLabel4, SW_SHOW);
ShowWindow(hwndLabel5, SW_SHOW);
n = atof( EditText );
n2= atof( EditText2 );
if (n <=0) break;
if (n2 <=0) break;
Rate = n2 / n;
Total = (3600*CV)/(VALUE1*Rate);
Total2 = (Total*VALUE1);
Total3 = (Total/10.7);
sprintf (EditText, "%.2f", Total);
strcat (QuoteText, EditText);
strcat (QuoteText, " Kw/h.");
SetWindowText (hwndLabel3, QuoteText);
sprintf (EditText, "%.2f", Total2);
strcat (QuoteText2, EditText);
strcat (QuoteText2, " BTU/h.");
SetWindowText (hwndLabel4, QuoteText2);
sprintf (EditText, "%.2f", Total3);
strcat (QuoteText3, EditText);
strcat (QuoteText3, " m³/h.");
SetWindowText (hwndLabel5, QuoteText3);
break ;
case 4 :
ShowWindow(hwndEdit, SW_SHOW);
ShowWindow(hwndEdit2, SW_SHOW);
ShowWindow(hwndButton, SW_SHOW);
ShowWindow(hwndLabel,SW_SHOW);
ShowWindow(hwndLabel2, SW_SHOW);
SetWindowText (hwndEdit, "");
SetWindowText (hwndEdit2, "");
ShowWindow(hwndButton2, SW_HIDE);
ShowWindow(hwndButton3, SW_HIDE);
strcpy (QuoteText, "The Result is ");
SetWindowText (hwndLabel3, QuoteText);
strcpy (QuoteText2, "The Result is ");
SetWindowText (hwndLabel4, QuoteText2);
strcpy (QuoteText3, "Gas Flow Rate ");
SetWindowText (hwndLabel5, QuoteText3);
ShowWindow(hwndLabel3, SW_HIDE);
ShowWindow(hwndLabel4, SW_HIDE);
ShowWindow(hwndLabel5, SW_HIDE);
break ;
case 10:
ShowWindow(hwndButton3, SW_HIDE);
char szTextStr[]= "";
char szTextStr2[]= "";
HDC prn ;
strcat(szTextStr,QuoteText);
strcat(szTextStr2,QuoteText2);
prn = GetPrinterDC();
// Prepare the DOCINFO.
static DOCINFO di = { sizeof (DOCINFO), TEXT ("Gas Calculator : Printing...")};
StartDoc (prn, &di);
StartPage (prn) ;
TextOut(prn, 650,350,szTextStr, lstrlen (szTextStr));
TextOut(prn, 650,550,szTextStr2, lstrlen (szTextStr2));
EndPage (prn);
EndDoc(prn);
DeleteDC(prn);
ShowWindow(hwndButton3, SW_SHOW);
break;
}
return 0 ;
case WM_PAINT:
Hdc=BeginPaint (hwnd,&ps);
hdcMem = CreateCompatibleDC (Hdc);
SelectObject(hdcMem, hBitmap);
BitBlt(Hdc,10,10,cxSource,cySource,hdcMem,0,0,SRCCOPY);
DeleteDC(hdcMem);
EndPaint (hwnd, &ps) ;
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd,message,wParam,lParam);
}
I was wondering if anyone can help me with a Printing problem I am having its Win32 program using GNU gcc compiler and the codeblocks IDE.
I am using TextOut to the printer to try to print mutiple lines of text to the printer
but it just seems to printout two lines of the same text, which is not what i want it to do.
my code is
#include <math.h>
#include <stdlib.h>
#include <Windows.h>
#include <iostream>
#include <string.h>
#include <Winspool.h>
#include <CommDlg.h>
#include <stdio.h>
#include <wingdi.h>
#include <winbase.h>
// Global Variables.
double CV = 1040;
double VALUE1 = 3412;
double Total;
double Total2;
double Total3;
double Rate;
double n ;
double n2;
DIBSECTION ds;
char EditText[256];
char QuoteText[256] = "The Result is ";
char QuoteText2[256] = "The Result is ";
char QuoteText3[256] = "Gas Flow Rate ";
int EditTextLength;
char EditText2[256];
int EditTextLength2;
char szAppName [] = TEXT("GasRates");
int iWidth;
int iHeight;
HBITMAP hbm;
RECT rc;
HDC hdc ;
HDC GetPrinterDC (void)
{
// Initialize a PRINTDLG object's size and set the PD_RETURNDC flag.
// The PD_RETURNDC flag tells the dialog to return a printer device context.
PRINTDLG pd = {0};
pd.lStructSize = sizeof( pd );
pd.Flags = PD_RETURNDC;
// Retrieves the printer DC
PrintDlg(&pd);
hdc =pd.hDC;
return hdc ;
}
LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
// **********
// class CRaster
// - Generic class for BMP raster images.
class CRaster {
public:
int Width,Height; // Dimensions
int BPP; // Bits Per Pixel.
char * Raster; // Bits of the Image.
RGBQUAD * Palette; // RGB Palette for the image.
int BytesPerRow; // Row Width (in bytes).
BITMAPINFO * pbmi; // BITMAPINFO structure
// Member functions (defined later):
int LoadBMP (char * szFile);
int GDIPaint (HDC hdc,int x,int y);
};
// **********
// Windows Main Function.
// - Here starts our demo program
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, IDI_WINLOGO);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass (&wc);
hwnd = CreateWindow (szAppName,"Gas Rate Calculator",WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
CW_USEDEFAULT,CW_USEDEFAULT,218,600,
0,0,hInstance,0);
ShowWindow (hwnd,nCmdShow);
UpdateWindow (hwnd);
while (GetMessage(&msg,0,0,0))
{
if(!IsDialogMessage(hwnd, &msg))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
return msg.wParam;
}
// **********
// Main Window Procedure.
// - Processes Window Messages
LRESULT CALLBACK WindowProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
static HWND hwndEdit2;
static HWND hwndButton;
static HWND hwndButton2;
static HWND hwndButton3;
static HWND hwndLabel;
static HWND hwndLabel2;
static HWND hwndLabel3;
static HWND hwndLabel4;
static HWND hwndLabel5;
static HBITMAP hBitmap;
static int cxSource, cySource;
HDC Hdc, hdcMem;
HINSTANCE hInstance;
PAINTSTRUCT ps;
BITMAP bitmap;
HBRUSH NewBrush;
NewBrush = CreateSolidBrush(RGB(255, 0, 0));
RECT rect ;
rect.bottom = 160;
rect.left = 160;
rect.right = 160;
rect.top = 160;
switch (message)
{
case WM_CREATE:
//load bitmap from resource
hInstance = ((LPCREATESTRUCT) lParam) ->hInstance;
hBitmap = LoadBitmap (hInstance,MAKEINTRESOURCE(3));
GetObject (hBitmap, sizeof (BITMAP), &bitmap);
cxSource = bitmap.bmWidth;
cySource = bitmap.bmHeight;
HICON hICon;
hICon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(2));
SetClassLong(hwnd, // window handle
GCL_HICON, // changes icon
(LONG) hICon); // The new icon
PostMessage (hwnd, WM_SETICON, ICON_BIG, (LPARAM) hICon);
hwndLabel5 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,400,200,30,hwnd,(HMENU) 9,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel4 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,350,200,30,hwnd,(HMENU) 8,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel3 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,300,200,25,hwnd,(HMENU) 7,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel2 = CreateWindow ( TEXT ("static"),"Enter Time Taken in Seconds.",WS_CHILD|WS_VISIBLE|ES_LEFT|1,7,350,200,25,hwnd,(HMENU) 5,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndLabel = CreateWindow ( TEXT ("static"),"Enter Volume in Cubic Feet.",WS_CHILD|WS_VISIBLE|ES_LEFT|1,7,260,180,25,hwnd,(HMENU) 6,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndButton = CreateWindow ( TEXT ("button"),"Calculate",WS_CHILD|WS_VISIBLE|WS_TABSTOP|1,55,500,100,25,hwnd,(HMENU) 3,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndButton2 = CreateWindow ( TEXT ("button"),"Back",WS_CHILD|1,55,500,100,25,hwnd,(HMENU) 4,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndButton3 = CreateWindow ( TEXT ("button"),"Print",WS_CHILD|1,55,465,100,25,hwnd,(HMENU) 10,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndEdit = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| WS_VISIBLE| ES_LEFT|WS_BORDER| WS_TABSTOP,20, 300, 50, 20, hwnd,
(HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
hwndEdit2 = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| WS_VISIBLE| ES_LEFT| WS_BORDER| WS_TABSTOP,20, 370, 75, 20, hwnd,
(HMENU) 2,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
SetFocus (hwndEdit);
return 0;
case WM_CTLCOLORSTATIC:
SetBkMode(hdc,TRANSPARENT);
return (LRESULT)CreateSolidBrush(0xFFFFFF);
case WM_SETFOCUS:
SetFocus (hwndEdit);
return 0 ;
case WM_COMMAND:
switch (wParam)
{
case 3 :
EditTextLength = GetWindowTextLength(hwndEdit) +1;
GetWindowText(hwndEdit, EditText, EditTextLength);
EditTextLength2 = GetWindowTextLength(hwndEdit2) +1;
GetWindowText(hwndEdit2, EditText2, EditTextLength2);
ShowWindow(hwndEdit, SW_HIDE);
ShowWindow(hwndEdit2, SW_HIDE);
ShowWindow(hwndButton, SW_HIDE);
ShowWindow(hwndLabel, SW_HIDE);
ShowWindow(hwndLabel2, SW_HIDE);
ShowWindow(hwndButton2, SW_SHOW);
ShowWindow(hwndButton3, SW_SHOW);
ShowWindow(hwndLabel3, SW_SHOW);
ShowWindow(hwndLabel4, SW_SHOW);
ShowWindow(hwndLabel5, SW_SHOW);
n = atof( EditText );
n2= atof( EditText2 );
if (n <=0) break;
if (n2 <=0) break;
Rate = n2 / n;
Total = (3600*CV)/(VALUE1*Rate);
Total2 = (Total*VALUE1);
Total3 = (Total/10.7);
sprintf (EditText, "%.2f", Total);
strcat (QuoteText, EditText);
strcat (QuoteText, " Kw/h.");
SetWindowText (hwndLabel3, QuoteText);
sprintf (EditText, "%.2f", Total2);
strcat (QuoteText2, EditText);
strcat (QuoteText2, " BTU/h.");
SetWindowText (hwndLabel4, QuoteText2);
sprintf (EditText, "%.2f", Total3);
strcat (QuoteText3, EditText);
strcat (QuoteText3, " m³/h.");
SetWindowText (hwndLabel5, QuoteText3);
break ;
case 4 :
ShowWindow(hwndEdit, SW_SHOW);
ShowWindow(hwndEdit2, SW_SHOW);
ShowWindow(hwndButton, SW_SHOW);
ShowWindow(hwndLabel,SW_SHOW);
ShowWindow(hwndLabel2, SW_SHOW);
SetWindowText (hwndEdit, "");
SetWindowText (hwndEdit2, "");
ShowWindow(hwndButton2, SW_HIDE);
ShowWindow(hwndButton3, SW_HIDE);
strcpy (QuoteText, "The Result is ");
SetWindowText (hwndLabel3, QuoteText);
strcpy (QuoteText2, "The Result is ");
SetWindowText (hwndLabel4, QuoteText2);
strcpy (QuoteText3, "Gas Flow Rate ");
SetWindowText (hwndLabel5, QuoteText3);
ShowWindow(hwndLabel3, SW_HIDE);
ShowWindow(hwndLabel4, SW_HIDE);
ShowWindow(hwndLabel5, SW_HIDE);
break ;
case 10:
ShowWindow(hwndButton3, SW_HIDE);
char szTextStr[]= "";
char szTextStr2[]= "";
HDC prn ;
strcat(szTextStr,QuoteText);
strcat(szTextStr2,QuoteText2);
prn = GetPrinterDC();
// Prepare the DOCINFO.
static DOCINFO di = { sizeof (DOCINFO), TEXT ("Gas Calculator : Printing...")};
StartDoc (prn, &di);
StartPage (prn) ;
TextOut(prn, 650,350,szTextStr, lstrlen (szTextStr));
TextOut(prn, 650,550,szTextStr2, lstrlen (szTextStr2));
EndPage (prn);
EndDoc(prn);
DeleteDC(prn);
ShowWindow(hwndButton3, SW_SHOW);
break;
}
return 0 ;
case WM_PAINT:
Hdc=BeginPaint (hwnd,&ps);
hdcMem = CreateCompatibleDC (Hdc);
SelectObject(hdcMem, hBitmap);
BitBlt(Hdc,10,10,cxSource,cySource,hdcMem,0,0,SRCCOPY);
DeleteDC(hdcMem);
EndPaint (hwnd, &ps) ;
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd,message,wParam,lParam);
}