?
Solved

Posting user defined message - MFC

Posted on 2009-05-08
13
Medium Priority
?
1,592 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:andy06
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 450 total points
ID: 24334343
Hi andy06,

what you wrote is correct - you don't need to take care of WPARAM and LPARAM as long as you don't want to pass some data with the posted message. Just declare a message caller function like this and call the OnSaveImages there:

#define WM_SAVEIMAGES  WM_APP+2

...
ON_MESSAGE( WM_SAVEIMAGES,OnMsgSaveImages )
...

LRESULT
CBitmapDlg::OnMsgSaveImages (WPARAM, LPARAM)
{
 OnSaveImages();
}

Hope that helps,

ZOPPO
0
 
LVL 19

Expert Comment

by:alb66
ID: 24334346
pYourWnd->PostMessage( WM_SAVEIMAGES, 123, 456 );

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

  return 0;
}
0
 

Author Comment

by:andy06
ID: 24335015
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...
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 19

Assisted Solution

by:alb66
alb66 earned 450 total points
ID: 24335143
Keep in mind that using PostMessage, your function Calculate() is executed without waiting for the end of save function.
If you want to perform the operations in sequence you can use SendMessage instead.
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CPP/Q_21325004.html
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24337677
>>>> 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.


0
 

Author Comment

by:andy06
ID: 24362947
>>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?

0
 
LVL 19

Expert Comment

by:alb66
ID: 24362972
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.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 600 total points
ID: 24363016
>>>> why do you mean by: not use shared or global data?
You currently pass 0, 0 with the message. Hence the message handler has no additional information but the window and the message id. If it needs some more information it currently must get them from global or shared data (re)sources. It is better to use the WPARAM and LPARAM arguments to pass individual information with the message rather than to retrieve all in the message handler.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24363039
>>>> 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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24363051
>>>> 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.
0
 

Author Comment

by:andy06
ID: 24363252
>>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 ???

0
 

Author Comment

by:andy06
ID: 24363276
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...
0
 
LVL 19

Expert Comment

by:alb66
ID: 24363615
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question