ShaunN
ShaunN

Reputation:

CDialog not Receiving Windows Messages in ActiveX Control

I have an ActiveX control in MFC that manipulates images and I am trying to add TWAIN scanning functionality to it.

I need to be able to receive a Windows Message back from the TWAIN driver that tells my control when an image has been scanned, so I have created a CDialog and I pass the HWND of the Dialog to the driver.

ALl the sample code I have seen on the net then uses PreTranslateMessage to capture the message from TWAIN, but in my ActiveX control this method is never being called.

Does anyone know how I can get the messages for that Dialog? I have also tried using PeekMessage with no success.

Many Thanks

Upvotes: 0

Views: 1371

Answers (1)

adzm
adzm

Reputation: 4136

You don't need to create a CDialog. You just need any window to process the messages. Anything dealing with TWAIN is best handled in its own thread. So, create a new thread for MFC (via CWinThread or AfxBeginThread). In that thread, create a CWnd. The HWND of this CWnd is the one you will pass with all the calls to the DSM, etc. Each thread has its own message queue, so set one up in there. Communicate with that thread via PostMessage, SendMessage, PostThreadMessage, etc. Assuming you post a message MY_SPECIAL_MESSAGE to signal to being acquiring an image, your message loop should look something like this:

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    if (msg.message == MY_SPECIAL_MESSAGE)
    {   
        GetImageFromTWAIN();
    }
    else if (!ProcessTWAINMessage(&msg)) {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}

Definitely look at the source code in the TWAIN development kit to see how this all works in detail. TWAIN is a tricky creature.

Trust me, this is the best approach. You can do it all in a single thread using your main thread's message queue, but it's to be avoided.

Upvotes: 1

Related Questions