Link to home
Start Free TrialLog in
Avatar of andy06
andy06Flag for Germany

asked on

Posting user defined message - MFC

Hi Experts,
I have in my MFC dialog application (VS 2008) a function named OnSaveImages which is defined as follow:

void CBitmap::OnSaveImages()
{
   CString x1;
  x1.Format (_T("C:\\Images\\08-05-09\\Img%d.bmp"), ImageNumber);
  m_Img.SaveImg(x1);
}
where the function SaveImg is an operation of the (.OCX) Image Control declared as:
BOOL SaveImg(LPCTSTR Filename)

My question is how could I define a user defined message WM_APP that will call OnSaveImages and how could I post that message so that I could call the function OnSaveImages?
I've already read similar questions here so that I know I need to do the followings:

//In the Bitmap.h file
afx_msg LRESULT OnSaveImages (WPARAM, LPARAM);

//In te CBitmap.cpp file
#define WM_SAVEIMAGES  WM_APP+2
//BEGIN_MESSAGE_MAP(CBitmapDlg, CDialog)
...
ON_MESSAGE(WM_SAVEIMAGES,OnSaveImages)
...

but I still don't get how I could handle with LPARAM and WPARAM in the definition of:
LRESULT OnSaveImages (WPARAM, LPARAM);
and in PostMessage(WM_SAVEIMAGES,?,?)

Thank you

SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pYourWnd->PostMessage( WM_SAVEIMAGES, 123, 456 );

LRESULT CYourWnd::OnSaveImages( WPARAM wParam, LPARAM lParam )
{
  ASSERT( wParam == 123 );
  ASSERT( lParam == 456 );

  return 0;
}
Avatar of andy06

ASKER

Unfortunatelly I can not  managed to save the image...
I have a function called Calculate():
void CBitmap::Calculate(Image Img)
{    .....
   // do something  
   ...
 //display the image
 PostMessage(WM_DISPLAYIMAGE, 0, 0);
//save the image
PostMessage(WM_SAVEIMAGES, 0, 0);
//and then I should do some calculating with the image
...
}
 In Calculate(Image Img) before saving the image with  PostMessage(WM_SAVEIMAGES,0,0) I displayed it and after saving it I proceed some calculations with the image.This process should occur many times...

Before adding the saving message every thing was fine and but now my application just crashes...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> PostMessage(WM_SAVEIMAGES, 0, 0);

PostMessage puts the message at end of message queue. Hence, the saving would occur some time *after* CBitmap::Calculate(Image Img) returned.

You could call SendMessage instead of PostMessage to have the message processed immediately. But, you should pass a handle or pointer with the message and not use shared or global data.


Avatar of andy06

ASKER

>>you should pass a handle or pointer with the message and not use shared or global data
why do you mean by: not use shared or global data?
As handle to passed with  the message should I use hWnd=GetSafeHwnd(); or is that unnecessary?
SendMessage will still  be called with 3 parameters because it is used within a function of a class derived from CWnd right?

The first parameter is the message code and, of course, it is mandatory.
The second and the third are optional parameters and they depends by the message; in the case of user defined message you can decide to use one, both or none of them.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> As handle to passed with  the message should I use hWnd=GetSafeHwnd();
You also could use CWnd::PostMessage as in most samples above. The CWnd::PostMessage makes the call GetSafeHwnd() before it calls the ::PostMessage(hwnd, ...) from WINAPI.
>>>> SendMessage will still  be called with 3 parameters because it is used within a function of a class derived from CWnd right?


Yes, the same applies for PostMessage. But SendMessage is dangerous cause it could deadlock. Better always use PostMessage if you don't need immediately return.
Avatar of andy06

ASKER

>>Better always use PostMessage if you don't need immediately return.
After saving the image I have to perform some calculations with the image.Hence it's maybe better to use SendMessage.
There's something strange. ..I'm calling SendMessage many times in the void CBitmap::Calculate(Image Img) (see above). For example to refresh the image I will be using  SendMessage(WM_DISPLAYIMAGE, 0, 0);and since I  don't write afterwards  the following satements:

if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
            ::TranslateMessage(&message);
            ::DispatchMessage(&message);
      }
my image won't be refreshed at all... Is  that normal ???

Avatar of andy06

ASKER

I wanted to say it doesn't matter if I use SendMessage(WM_DISPLAYIMAGE, 0, 0) or  PostMessage(WM_DISPLAYIMAGE, 0, 0) I will still have to add the statements above...
Yes, it is normal. Windows need to handle the WM_PAINT message in order to draw something.
http://msdn.microsoft.com/en-us/library/dd145193(VS.85).aspx

If you must do some heavy functions you should use a worker thread
http://www.codeproject.com/KB/threads/usingworkerthreads.aspx