From: k.sahici on
Hello everyone,

I found a library on CodeProject, named CPictureEx, to be able to load
jpeg images in an MFC application. I created a control of type CStatic
and then converted to this type(CPictureEx) to be able to associate
this control to an image.

I'm using this library in a dialog based project. No SDI/MDI.

I have a user-defined message to update the region of this CPictureEx
variable. When the main dialog receives my user-defined message it
successfully updates the region and displays the jpeg file. However,
it fails when it comes to unloading the jpeg file. When I minimized
and then maximized this window, this jpeg file diappeared as I
expected it to be. After that, I realized the WM_PAINT message has to
be generated explicitly to be able to unload the image.

I have used various combinations of InvalidateRgn, UpdateWindow, etc
and even invalidating this CPictureEx variable, itself. None of them
worked. The only one that worked was "AfxGetMainWnd()->Invalidate();".
As you might guess, this causes an annoying flickering.

I guess this is a simple problem but unfortunately I'm not experienced
with MFC.

I'd be grateful if anyone could give me an idea to solve it.
From: David Webber on


"k.sahici" <k.sahici(a)gmail.com> wrote in message
news:a4724ea6-a887-4718-a323-3157621e7238(a)y4g2000yqy.googlegroups.com...

> I have used various combinations of InvalidateRgn, UpdateWindow, etc
> and even invalidating this CPictureEx variable, itself. None of them
> worked. The only one that worked was "AfxGetMainWnd()->Invalidate();".
> As you might guess, this causes an annoying flickering.
>
> I guess this is a simple problem but unfortunately I'm not experienced
> with MFC.
>
> I'd be grateful if anyone could give me an idea to solve it.

The general principle is that if you invalidate a window's client area, a
WM_PAINT message is generated and the code which draws it is called in
response.

If your control has a CWnd derived variable associated with it, then just
invalidating that whole window *should* do the job. If it isn't then
perhaps looking at the messages with the Spy tool might help.

Sometimes when you SendMessage(), there are things which are not fully
updated as you would like them to be. For example:

SendMessage( ... );
DoVariousStuff();

The 'various stuff' has not been done when the message is received and acted
upon. Whereas, if you do

PostMessage( ... );
DoVariousStuff();

then the 'various stuff' *has* been done when when the message is received
and acted upon. This may be worth trying.

In your case, it isn't completely clear if it will help, because your
user-message causes another message (WM_PAINT) to be invoked and the update
only occurs when *that* is received. But it may be worth a try - just
substitute PostMessage() for SendMessage() for your user message.

Dave

--
David Webber
Mozart Music Software
http://www.mozart.co.uk
For discussion and support see
http://www.mozart.co.uk/mozartists/mailinglist.htm






From: Joseph M. Newcomer on
See below..,
On Fri, 11 Jun 2010 22:04:09 -0700 (PDT), "k.sahici" <k.sahici(a)gmail.com> wrote:

>Hello everyone,
>
>I found a library on CodeProject, named CPictureEx, to be able to load
>jpeg images in an MFC application. I created a control of type CStatic
>and then converted to this type(CPictureEx) to be able to associate
>this control to an image.
>
>I'm using this library in a dialog based project. No SDI/MDI.
>
>I have a user-defined message to update the region of this CPictureEx
>variable.
****
I have no idea what you mean by this. Do you mean that you generate a CRgn which is
defined in the dialog, and overlaps the CStatic/CPictureEx? That wouldn;t make sense,
because you want to update the IMAGE, not the blank space under it on the dialog.
****
>When the main dialog receives my user-defined message it
>successfully updates the region and displays the jpeg file.
****
This doesn't make any sense, either. Presumably, you want to display a JPEG file, which
means you want the CPictureEx control to load it. THe dialog has NOTHING to do with this
action, or the displaying of the contents of the CPictureEx control!
****
>However,
>it fails when it comes to unloading the jpeg file. When I minimized
>and then maximized this window, this jpeg file diappeared as I
>expected it to be. After that, I realized the WM_PAINT message has to
>be generated explicitly to be able to unload the image.
****
The JPEG must be displayed by the CPictureEx control, and the dialog must have NOTHING to
do with this area of its surface!

Actually, the only reason I would have expected the picture to disappear if the dialog is
minimized and then restored is a serious coding error in the program! Which you obviously
have.
*****
>
>I have used various combinations of InvalidateRgn, UpdateWindow, etc
>and even invalidating this CPictureEx variable, itself. None of them
>worked. The only one that worked was "AfxGetMainWnd()->Invalidate();".
>As you might guess, this causes an annoying flickering.
****
There's something seriously wrong with how you are thinking about this. THere is never a
reason to invalidate the main dialog window. And you don't need to invalidate the
CPictureEx if you coded it correctly! But it looks like your whole design is deeply
flawed and must be recoded.
****
>
>I guess this is a simple problem but unfortunately I'm not experienced
>with MFC.
****
It has nothing to do with MFC, and everything to do with understanding how windows work.
It is not even *vaguely* related to MFC; you are not writing correct code. The code would
be wrong if it were coded in the raw Win32 API with no MFC in sight.
****
>
>I'd be grateful if anyone could give me an idea to solve it.
****
THe OnPaint handler of CPictureEx draws the bitmap. Since I have no idea what CPictureEx
does, I can't tell you how to do this. But what I would do is have a SetBitmap method I
exported, which took the name of a JPEG file. Or, more likely, I'd give it the ID of a
JPEG resource which I would load. And once set, that bitmap would be redrawn by the
OnPaint handler. Or, I'd just require the CStatic control have the SS_BITMAP style and
call CStatic::SetBitmap to set the bitmap once I'd gotten it loaded, and let the regular
superclass (CStatic) handle its own painting.

Note that none of this is really related to MFC. MFC just makes it easier to get right.

The dialog remains totally clueless about that this control does or how it does it, and
there would NEVER be a CRgn involved anywhere in the coding.
joe
*****
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm