views:

26

answers:

1

We have a VB6 app that has implemented a handler for WM_TCARD messages from an HTML Help file. It navigates within the application and opens the relevant screens etc.

How does one do this in a WinForms app?

The only way I can immediately see is to handle the window procedure and process the WM_TCARD message in exactly the same way we were doing for the VB6 app.

Is there a more .net way to do this?

+1  A: 

You'll have to handle the messages yourself. However, it's pretty easy to make a class which does this for you. E.g.:

public class HelpCards : IMessageFilter {
    const int WM_TCARD = 0x52;
    const int ID_NOTIFICATION = 4242;

    public enum TCardAction: int {
        IDOK = 1,
        IDCANCEL = 2,
        IDABORT = 3,
        IDRETRY = 4,
        IDIGNORE = 5,
        IDYES = 6,
        IDNO = 7,
        IDCLOSE = 8,
        IDHELP = 9,

        HELP_TCARD = 0x8000,
        HELP_TCARD_DATA = 0x0010,
        HELP_TCARD_OTHER_CALLER = 0x0011,
        HELP_TCARD_OTHER_NEXT = 0x0011,
    }

    public HelpCards() {
        Application.AddMessageFilter(this);
    }

    public delegate void OKReceived();
    public event OKReceived OnOKReceived; 

    public bool PreFilterMessage(ref Message m) {
        if (m.Msg == WM_TCARD && (int)m.WParam == ID_NOTIFICATION) {
            switch ((TCardAction)m.LParam) {
                case TCardAction.IDOK:
                    if (OnOKReceived != null) {
                        OnOKReceived();
                    }
                    break;
                    // etc.
            }

            return true; // true means message was handled
        }

        return false;
    }
}

Then, in the place where you want to subscribe (probably in your form somewhere) use

hc = new HelpCards();
hc.OnOKReceived += new OKReceived(hc_OnOKReceived);

And have hc defined in the class definition of the form. Like so

HelpCards hc;

And somewhere have the handling function:

void hc_OnOKReceived() {
    throw new NotImplementedException();
}

Obviously you will need to do the rest yourself. But that should get you started.

Vincent McNabb
Thanks for that. I thought that would be the case but was hoping I'd missed some trickery with the HelpProvider or something that would mean I didn't need to handle the message
Hamish Smith
Oh, and you probably want to remove the message filter when the class gets disposed. And you probably want to use EventHandler, not void() for the event delegate.
Vincent McNabb