C++ Builder6 编写的程序,实现打印多张图片。运行不稳定,帮忙啊!
本人用C++ Builder6 编写的程序,实现点击按键,一次把四个Image上的图片打印到指定的位置。可是点击第一次,四张图片准确的打印到指定的位置,第二次就不行了,只能打印其中三张或两张,第三次打印也是不行~~~~~~~~~~~奇怪啊!代码如下,还请高手赐教啊!
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
unsigned int BitmapInfoSize, BitmapImageSize;
long DIBWidth, DIBHeight;
PChar BitmapImage;
Windows::PBitmapInfo BitmapInfo;
int width,height;
int x_pos,y_pos;
width=200;
height=200;
x_pos=10;
y_pos=10;
Graphics::TBitmap *bitmap1=new Graphics::TBitmap();
bitmap1-> PixelFormat=pf24bit;
bitmap1-> Assign(invisible1-> Picture-> Graphic);
Printer()-> BeginDoc();
GetDIBSizes(bitmap1-> Handle, BitmapInfoSize, BitmapImageSize);
BitmapInfo = (PBitmapInfo) new char[BitmapInfoSize];
BitmapImage = (PChar) new char [BitmapImageSize];
GetDIB(bitmap1-> Handle, 0, BitmapInfo, BitmapImage);
DIBWidth = BitmapInfo-> bmiHeader.biWidth;
DIBHeight = BitmapInfo-> bmiHeader.biHeight;
StretchDIBits(Printer()-> Canvas-> Handle,
x_pos, y_pos,width, height,
0, 0, DIBWidth, DIBHeight,
BitmapImage, BitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
x_pos=210;
y_pos=210;
bitmap1-> Assign(invisible2-> Picture-> Graphic);
GetDIBSizes(bitmap1-> Handle, BitmapInfoSize, BitmapImageSize);
BitmapInfo = (PBitmapInfo) new char[BitmapInfoSize];
BitmapImage = (PChar) new char [BitmapImageSize];
GetDIB(bitmap1-> Handle, 0, BitmapInfo, BitmapImage);
DIBWidth = BitmapInfo-> bmiHeader.biWidth;
DIBHeight = BitmapInfo-> bmiHeader.biHeight;
StretchDIBits(Printer()-> Canvas-> Handle,
x_pos, y_pos,width, height,
0, 0, DIBWidth, DIBHeight,
BitmapImage, BitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
x_pos=410;
y_pos=410;
bitmap1-> Assign(invisible3-> Picture-> Graphic);
GetDIBSizes(bitmap1-> Handle, BitmapInfoSize, BitmapImageSize);
BitmapInfo = (PBitmapInfo) new char[BitmapInfoSize];
BitmapImage = (PChar) new char [BitmapImageSize];
GetDIB(bitmap1-> Handle, 0, BitmapInfo, BitmapImage);
DIBWidth = BitmapInfo-> bmiHeader.biWidth;
DIBHeight = BitmapInfo-> bmiHeader.biHeight;
StretchDIBits(Printer()-> Canvas-> Handle,
x_pos, y_pos,width, height,
0, 0, DIBWidth, DIBHeight,
BitmapImage, BitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
x_pos=610;
y_pos=610;
bitmap1-> Assign(invisible4-> Picture-> Graphic);
GetDIBSizes(bitmap1-> Handle, BitmapInfoSize, BitmapImageSize);
BitmapInfo = (PBitmapInfo) new char[BitmapInfoSize];
BitmapImage = (PChar) new char [BitmapImageSize];
GetDIB(bitmap1-> Handle, 0, BitmapInfo, BitmapImage);
DIBWidth = BitmapInfo-> bmiHeader.biWidth;
DIBHeight = BitmapInfo-> bmiHeader.biHeight;
StretchDIBits(Printer()-> Canvas-> Handle,
x_pos, y_pos,width, height,
0, 0, DIBWidth, DIBHeight,
BitmapImage, BitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
delete BitmapInfo;
delete BitmapImage;
Printer()-> EndDoc();
delete bitmap1;
}
//---------------------------------------
[解决办法]
这个
一点建议:
你的函数很多重复的代码
完全可以将打印某一张到指定位置的函数
封装成一个单独的函数
这样结构更加清晰
阅读会方便一些!
至于代码中的问题
暂时没有看出来
抱歉
[解决办法]
打印的Task还没完吧。。。打印速度不是由你按键速度决定的。。。