Pelles C forum

C language => Tips & tricks => Topic started by: czerny on December 17, 2014, 05:47:04 pm

Title: ImgPreview
Post by: czerny on December 17, 2014, 05:47:04 pm
I have attached a small project.

In the file 'ImgPrev.c' is a function named 'IPDrawItem()'.

I have long tried to get this example working using the second way in the #else part. I am shure, it should be possible not to delete the graphivs object. But I could not solve this problem. 'IPDrawItem' works then only in the first call, in the second I get a GpStatus == 1.
Title: Re: ImgPreview
Post by: JohnF on December 17, 2014, 06:00:58 pm
When messing around with GDIPLUS I noticed that one had to delete the previous image before the new one could be accommodated.

John
Title: Re: ImgPreview
Post by: czerny on December 17, 2014, 06:29:52 pm
When messing around with GDIPLUS I noticed that one had to delete the previous image before the new one could be accommodated.

I tried this:
Code: [Select]
void IPDrawItem(IP I, LPDRAWITEMSTRUCT dis)
{
RECT rect;
GpUnit  unit;
RectF src, dst;
    GpImage *image;

if (I && I->image && wcslen(I->fn))
{
#if 0
if (I->graphics)
GdipDeleteGraphics(I->graphics);
GdipCreateFromHDC(dis->hDC, &I->graphics);
#else
// shouldn't this work?
if (!I->graphics)
GdipCreateFromHDC(dis->hDC, &I->graphics);
#endif

GdipCloneImage(I->image, &image);
GetClientRect(dis->hwndItem, &rect);
dst.X = dst.Y = 0;
dst.Width = rect.right;
dst.Height = rect.bottom;

GdipGetImageBounds(image, &src, &unit);
GdiPlusDrawImageScaled(I->graphics, image, &dst, &src, UnitPixel, NULL);
GdipDisposeImage(image);
}
}

But the result is the same.
Title: Re: ImgPreview
Post by: JohnF on December 17, 2014, 07:20:18 pm
I don't see what you are saying.

You still need to delete

GdipDeleteGraphics(I->graphics);

John
Title: Re: ImgPreview
Post by: czerny on December 17, 2014, 10:23:41 pm
I don't see what you are saying.
I have deleted the image object in the last line. Haven't you said that I have to delete the image object?
Title: Re: ImgPreview
Post by: laurro on December 18, 2014, 12:29:34 am
czerny, when you build a graphics object you associate that graphics with one, single, particular hdc.
It is a parent/child relation, the hdc is the parent, and the graphics is the "smart" child. When the
parent (dis->hDC) go out of scope in the first WM_DRAWITEM, the child remains orphan and more importantly
he can play only in his parent house. The next WM_DRAWITEM: ( I assume you have WS_THICKFRAME for your
dialog ) the child is ok, alive, but the hdc (parent) is a totaly stranger. The child try to play in the
new house ( because you say so ), but, he doesn't recognise the new parent so he fail.
The solution: everytime the parent die, kill the children too,  everytime a new parent appear create a new child.

There is nothing wrong whith the Image, the code is a little redundant, no need for cloning the image.

Laur
Title: Re: ImgPreview
Post by: czerny on December 18, 2014, 08:54:14 am
laurro: So the short answer is, that my posted variant with the #if 1 section is right, isn't it?
Title: Re: ImgPreview
Post by: laurro on December 18, 2014, 09:50:11 am
very short...
Yes

Laur