Welcome!

PowerBuilder Authors: Dan Joe Barry, Carmen Gonzalez, Ian Thain, Yakov Werde, Paul Slater

Related Topics: PowerBuilder

PowerBuilder: Article

Custom Common Dialogs Using SetWindowsHookEx

There's No End To The Customization You Can Do


hThreadID = GetCurrentThreadId() ;

Finally, we call the SetWindowsHookEx method, indicating that we are implementing the WH_CALLWNDPROCRET hook. There are a number of different types of hooks documented for SetWindowsHookEx on Microsoft's MSDN site. The WH_CALLWNDPROCRET hook means that our callback will be fired each time the dialog finishes processing a SendMessage message. The lParam argument that is passed to our callback contains a structure that lets us know more specifically what information was included in the original SendMessage call. I'll discuss a little later why we used this particular hook.


hHook = SetWindowsHookEx
( WH_CALLWNDPROCRET, DialogHook, (HINSTANCE)hInstance, hThreadID ) ;

Before we get there, let's look at the remaining functions. The FilterFileTypes function simply takes the PowerBuilder string array argument with the file types the developer wants to filter out of the dialog and copies the values to the member array we declared earlier:


int pArrayItemCount ;
pblong dim[1] ;
IPB_Value* ipv ;
int i ;

m_filetypes = m_pSession->NewUnbounded SimpleArray( pbvalue_string ) ;
pbarray pbaFileTypes = ci->pArgs->GetAt(0)->GetArray() ;
pArrayItemCount = m_pSession->GetArrayLength(pbaFileTypes) ;
for( i=1; i <= pArrayItemCount; i++)
{
dim[0]= i;
ipv = m_pSession -> AcquireArrayItemValue ( pbaFileTypes, dim ) ;
m_pSession -> SetArrayItemValue ( m_filetypes, dim, ipv ) ;
m_pSession -> ReleaseValue ( ipv ) ;
}

The SetDefaultFileType function is a bit simpler - just copying the value that was passed to the member:


pbDefault = ci->pArgs->GetAt(0)->GetString() ;
m_default = (LPSTR)m_pSession->GetString(pbDefault);

The destructor for our class calls the UnhookWindowsHookEx to tell the Windows API we're no longer interested in receiving messages.


UnhookWindowsHookEx ( hHook ) ;
delete this;

The DialogHook callback function is where the bulk of the processing occurs. The first thing we do is examine the value of the nCode argument that was passed to us. If the value of that argument is less than 0, we are being instructed by the Windows API that we need to exit our own callback without further processing and allow the next callback in line to have the message and then return back the value returned by that callback.


if ( nCode < 0 )
{
DWORD lResult ;
lResult = CallNextHookEx ( hHook, nCode, wParam, lParam ) ;
return lResult ;
}

Otherwise, we declare a structure to receive information about exactly what the dialog was doing when we were called and cast the lParam argument into that structure. Once we have that, we check to see if the message the dialog received was the CB_ADDSTRING message (the message sent by the original creator of the dialog to add an item to a dropdown list box). If it was, we also check to see if the m_filetypes array is populated. That's because the developer may not have called the FilterFileTypes method; they may simply be using the SetDefaultFileType to set the default.

More Stories By Bruce Armstrong

Bruce Armstrong is a development lead with Integrated Data Services (www.get-integrated.com). A charter member of TeamSybase, he has been using PowerBuilder since version 1.0.B. He was a contributing author to SYS-CON's PowerBuilder 4.0 Secrets of the Masters and the editor of SAMs' PowerBuilder 9: Advanced Client/Server Development.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.