Mario.Tapilouw

Sunday, April 11, 2010

Using Message Map in Borland C++ Builder

If we want to pass information from a thread to the windows, we need to pass it safely. There are several ways that I know on how to perform this. One is by using a Message Map, which is a default message passing method in MFC(Microsoft Foundation Classes), another one is by overriding the Windows Process, i.e. the WndProc function.

There is another way of doing this in Borland C++ builder, but I found it is not stable. I don't know the reason, so I decided to change the other method. Ok, let's start.

First, you need to define the name of the windows message that you want to pass, in order to simplify, give an intuitive name e.g. UPDATE_TIMER

const DWORD UPDATE_TIMER = WM_USER + 1;

then, create a handler function that will process this message, for my application, I use something like this:

void updateTimerProc(CMMTimer &Timer);

CMMTimer is a multimedia timer class wrapper that I used, I downloaded it from this website in codeproject.

then begin delaring the message map

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(UPDATE_TIMER, CMMTimer , updateTimerProc)
END_MESSAGE_MAP(TForm)


and finally, we have to pass the message to the message map system, this can be performed as follow:

SendMessage(this->Handle, UPDATE_MMTIMER, 0, 0);

Then, one more thing that you have to do is to declare the function to perform some tasks such as updating the UI, etc.

void TFormMain::updateTimerProc(CMMTimer &Timer)
{
//put your code here

}

Call this function from your callback function or your thread. And, Voila! you will get your windows UI updated from the thread.

Labels: , , ,

1 Comments:

Post a Comment

<< Home