Message loop (WndProc) in CommandBar?

B

Bradley Plett

Is it possible to have a message loop (like WndProc) in a CommandBar?
If so, how? (Sample code would be much appreciated. :) Is there
some other alternative?

I assume I could go the full-blown route using a lot of API calls (as
per
http://www.google.com/groups?hl=en&lr=&ie=UTF-8&[email protected]&rnum=1),
but I'm just hoping there's a better/easier way.

I have some code in my one of my CommandBarButtons that needs to
receive a Windows message. I would like to be able to handle it
properly.

Thanks!
Brad.
 
K

Ken Slovak - [MVP - Outlook]

You have to set up a callback using the Win32 API's to handle what you want.
You don't say what programming language you're using but I've done similar
things to handle callbacks using specially set up class modules in VB 6. I
haven't done it for command bar button clicks but for things like setting up
system tray icons and handling clicks for them in my addins.
 
B

Bradley Plett

Thank-you for that quick reply. I'm using VB.NET. I've programmed
for callbacks before, but not for a scenario like this. In this case,
I have two problems I don't know how to deal with: 1) the handle to
provide the routine as the callback, and 2) how to trap the callback
if/when it happens.

I would assume that the handle would be that of the application. I'm
sure there must be an easy way to get that, though I don't know how.
Even if I can get the application's handle, the message loop is in the
application. How do I add an event handler to the application when
all I know is what the "m.Msg" will be that is returned?

Any help, especially code snippets, would be much appreciated!

Thanks!
Brad.
 
B

Bradley Plett

Very simple: I want to play a sound without waiting for it to be
complete. However, I do want to be notified when the playing is
complete. The statement of interest is:

mciSendString("play myWaveDevice notify", lstrReturn, 128, handle)

In case you're unfamiliar with that API call, it will callback
whatever is supplied as the "handle" and provide a MM_MCINOTIFY.

In an application with a message loop (e.g. one with a Windows form),
this is absolutely trivial. However, I don't know how to do this in a
CommandBar.

Thanks!
Brad.
 
D

Dmitry Streblechenko \(MVP\)

CommandBar itself does not have a message loop, but the whole application
(Outlook) does. No extra effort on your part required.
You simply need to create a (hidden) control with a handle, and the window
proc of that control will be called with the MM_MCINOTIFY.message.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
B

Bradley Plett

Here's some code I came up with that works. However, I'd love to have
someone review it and confirm that what I've got here is a) reasonable
(a simple sanity check), and b) as good as it could be. Is there a
better way?

--------------------------------------------------
' a helper class
Public Class clsMyForm
Inherits System.Windows.Forms.Form

Private Const MM_MCINOTIFY = &H3B9 ' MCI

Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
Dim lstrReturn As String = Space(128)
Select Case m.Msg
Case MM_MCINOTIFY
mciSendString("close myWaveaudioDevice", lstrReturn, 128,
Nothing) ' send "Nothing" as the callback here since I don't require
notification. If I needed notification, this routine would need to be
more sophisticated, and then I'd use "Me.Handle" instead
End Select
MyBase.WndProc(m)
End Sub

End Class

' code snippet from inside a command button
Dim lstrReturn As String = Space(128)
Dim frmForLoop As New clsMyForm
mciSendString("play myWaveaudioDevice notify", lstrReturn, 128,
frmForLoop.Handle)
--------------------------------------------------

Please note that this is just a snippet, and it may not work perfectly
for you without some tweaking.

Thanks!
Brad.
 
D

Dmitry Streblechenko \(MVP\)

Looks good to me. Just make sure frmForLoop is a global (class) variable so
it never goes out of scope and does not becomesa subject to the GC.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top