Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

paintTo() and Win 98 vs win Xp

Status
Not open for further replies.

Kistal

Programmer
Mar 28, 2003
18
BE
Hello, I am having a bit of a disturbing problem that is leaving me clueless...

I have written a component to display a set of "results", consisting of a name (TEdit), a score (TEdit), and a remark (TMemo). Each result is an instance of a self-written class called TEvaluationLine.

TEvaluationRoster is the wrapping class and actual component, containing methods to manipulate the TEvaluationLines it owns.

TEvaluationRoster has a method called "getPrintableVersion()" which returns a bitmap suited to assign to a Printer Canvas. This method basically asks all the TEvaluationRoster's TEvaluationLines to execute their asBitmap() method, which returns a small bitmap of that specific result, using the paintTo() method of the TEdits and the TMemo to create this bitmap. The TEvaluationRoster then assembles these small bitmaps into a large one and returns it.

Now the problem is that this approach works perfectly under winXp, but for some reason just produces a blank page using win98... Could someone tell me what I'm doing wrong here?

Relevant Code excerpts pasted below.

function TEvaluationLine.asBitmap : TBitMap;
var
smallbmp : TBitMap;
begin
smallBmp := TBitMap.Create;
smallBmp.Width:=TotalWidth;
smallBmp.Height:=Height;
smallbmp.Canvas.Lock;
fCaption.PaintTo(smallbmp.canvas.Handle,0,0);
fEvaluation.PaintTo(smallbmp.canvas.Handle,fCaption.Width,0);
fRemark.PaintTo(smallbmp.canvas.Handle,fCaption.Width+fEvaluation.Width,0);
smallbmp.Canvas.Unlock;
asBitmap:=smallBmp;
end;

function TEvaluationRoster.GetPrintableVersion(inWidth : Integer): TBitMap;
var
scale : double;
smallBmp, bigBmp : TBitMap;
index1,index2 : Integer;
topheight : Integer;
orgHeight:Integer;
begin
orgHeight:=self.Height;
self.ResizeToFitCellHeight;
index1:=0;
index2:=0;
topheight:=0;
bigBmp:=TBitMap.Create;
bigBmp.Width:=inWidth;
scale:=RosterWidth/inWidth;
bigbmp.Height:=Round(RosterHeight/scale);
for index1:=0 to fCols-1 do
begin
topheight:=0;
for index2:=0 to fRows-1 do
begin
smallBmp := fCells[index2,index1].asBitmap;
bigBmp.Canvas.StretchDraw(
Rect(Round(bigbmp.Width*index1/fCols),
topheight,
Round(bigbmp.Width*index1/fCols)+Round(bigBmp.Width/fCols),
topheight+Round(smallbmp.Height/(fCols*scale))
)
,smallBmp);
topheight:=topheight+Round(smallbmp.Height/(fCols*scale));
end;
end;
getPrintableVersion:=bigBmp;
self.Height:=orgHeight;
freeandnil(smallbmp);
end;

procedure TFParent.PrintDiscriminationReport(MyCanvas: TCanvas; Scale: Integer);
var i,x,y,y_start: word;
margin, LongestCol1, LongestCol2, LongestCol3: word;
myClientData: TClientMetaData;
bmp : TBitMap;
oldtab : Integer;
begin
myClientData := GetClientMetaData();
// exit if no clientmetadata!
if not myClientData.Complete then exit;
// Fill canvas:
oldtab:=self.Page.ActivePageIndex;
self.Page.ActivePageIndex:=2;
(...)
bmp:=self.DiscrimStrGrd.getPrintableVersion(printer.PageWidth-20);
mycanvas.Draw(10,y,bmp);
Freeandnil(bmp);
self.Page.ActivePageIndex:=oldtab;
end;
 
I'm a little surprised it works at all. I don't use custom components, but normally a function that looks like this...
Code:
  function TEvaluationLine.asBitmap : TBitMap;
  var
    smallbmp : TBitMap;
  begin
    smallBmp := TBitMap.Create;
     :
     :
    asBitmap:=smallBmp;
  end;
... would be returning a pointer a memory location that is valid only while the function is executing. It probably works ok until the memory reserved for
Code:
 smallbmp
gets over-written, but the result would be unpredictable. If it works in XP I would guess that XP handles memory differently than 98.

 
Hmmm that would explain things, lol... Did I mention I'm a Delphi noob? :D

So I guess I'll have to pass some bitmap as parameter rather than creating it in the function itself, huh? I can do that. *nods*

Can't try it out till monday, tho :(
*cries*
Me wanna code!

Oh, and thanks :)
 
The problem seems to be more persistent than I thought.

Win98 still generates blank pages... Could someone check my new version of the code and tell me what else could be the cause, because I am so lost this time...


procedure TEvaluationLine.asBitmap(var smallBmp : TBitMap);
begin
smallBmp.Width:=TotalWidth;
smallBmp.Height:=Height;
smallbmp.Canvas.Lock;
fCaption.PaintTo(smallbmp.canvas.Handle,0,0);
fEvaluation.PaintTo(smallbmp.canvas.Handle,fCaption.Width,0);
fRemark.PaintTo(smallbmp.canvas.Handle,fCaption.Width+fEvaluation.Width,0);
smallbmp.Canvas.Unlock;
end;



procedure TEvaluationRoster.GetPrintableVersion(inWidth : Integer; var bigBmp : TBitMap);
var
scale : double;
smallBmp : TBitMap;
index1,index2 : Integer;
topheight : Integer;
orgHeight:Integer;
begin
smallBmp := TBitMap.Create;
orgHeight:=self.Height;
self.ResizeToFitCellHeight;
bigBmp.Width:=inWidth;
scale:=RosterWidth/inWidth;
bigbmp.Height:=Round(RosterHeight/scale);
for index1:=0 to fCols-1 do
begin
topheight:=0;
for index2:=0 to fRows-1 do
begin
fCells[index2,index1].asBitmap(smallBmp);
bigBmp.Canvas.StretchDraw(
Rect(Round(bigbmp.Width*index1/fCols),
topheight,
Round(bigbmp.Width*index1/fCols)+Round(bigBmp.Width/fCols),
topheight+Round(smallbmp.Height/(fCols*scale))
)
,smallBmp);
topheight:=topheight+Round(smallbmp.Height/(fCols*scale));
end;
end;
self.Height:=orgHeight;
freeandnil(smallbmp);
end;



procedure TFParent.PrintDiscriminationReport(MyCanvas: TCanvas; Scale: Integer);
var i,x,y,y_start: word;
margin, LongestCol1, LongestCol2, LongestCol3: word;
myClientData: TClientMetaData;
bmp : TBitMap;
oldtab : Integer;
begin
myClientData := GetClientMetaData();
// exit if no clientmetadata!
if not myClientData.Complete then exit;
// Fill canvas:
oldtab:=self.Page.ActivePageIndex;
self.Page.ActivePageIndex:=2;
(...)
bmp := TBitMap.Create;
self.DiscrimStrGrd.getPrintableVersion(printer.PageWidth-20, bmp);
mycanvas.Draw(10,y,bmp);
Freeandnil(bmp);
self.Page.ActivePageIndex:=oldtab;
end;
 
As a Newbie (presume thats what noob means) you seem to be jumping in at the deep end somewhat!
The only thing I can pickup is that I cant see where you are setting the parent of your new graphic object, though I dont know why this would make a difference under XP.

Dropping a lot of code on the forum can put some readers off, but not Zathras [medal] thats why he's top dog I suppose.

Steve
 
Risking being branded as a whiner, I'm going to top this and hope someone comes along that can help me :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top