VB CDK: Example of Subclassing a Visual Basic Form

ID Number: Q83806

1.00

WINDOWS

Summary:

The subclass procedure is a message filter that performs non-default

processing for a few key messages, and passes other messages to a

control's default window procedure using CallWindowProc. The

CallWindowProc function passes a message to Windows, which in turn

sends the message to the target window procedure. The target window

procedure cannot be called directly by the subclass procedure because

the target procedure is exported.

This information applies to Microsoft Professional Toolkit for

Microsoft Visual Basic programming system version 1.0 for Windows.

More Information:

The following code example demonstrates how to subclass a form using

the Visual Basic Custom Control Development Kit (CDK). The CDK, which

was first sold as Microsoft Visual Basic add-on kit number

046-050-022, is now supplied with Microsoft Professional Toolkit for

Microsoft Visual Basic version 1.0 for Windows.

This example is developed using the CIRCLE.C source file from the

CIRCLE1 project supplied with the CDK package. Only the file(s) that

have changed from this project are included, and it is assumed that

you have the additional CDK files.

//=================== CIRCLE1 ==================

// CIRCLE.C

// An example of subclassing a Visual Basic Form

//==============================================

#define NOCOMM

#include <windows.h>

#include <vbapi.h>

#include "circle.h"

//declare the subclass procedure

LONG FAR PASCAL _export SbClsProc(HWND,USHORT,USHORT,LONG);

//far pointer to the default procedure

FARPROC lpfnOldProc = (FARPROC) NULL ;

//get the controls parent handle(form1)

HWND hParent ;

//----------------------------------------------------------

// Circle Control Procedure

//----------------------------------------------------------

LONG FAR PASCAL _export CircleCtlProc (HCTL hctl, HWND hwnd,

USHORT msg, USHORT wp, LONG lp)

{

LONG lResult ;

switch (msg)

{

case WM_CREATE:

switch (VBGetMode())

{

//this will only be processed during run mode

case MODE_RUN:

{

hParent = GetParent (hwnd) ;

//get the address instance to normal proc

lpfnOldProc = (FARPROC) GetWindowLong

(hParent, GWL_WNDPROC) ;

//reset the address instance to the new proc

SetWindowLong (hParent,

GWL_WNDPROC, (LONG) SbClsProc) ;

}

break ;

}

break ;

}

// call the default VB proc

lResult = VBDefControlProc(hctl, hwnd, msg, wp, lp);

return lResult;

}

LONG FAR PASCAL _export SbClsProc (HWND hwnd, USHORT msg,

USHORT wp, LONG lp)

{

switch (msg)

{

case WM_SIZE:

{

//place size event here for example...

}

break;

case WM_DESTROY:

SetWindowLong (hwnd, GWL_WNDPROC,

(LONG) lpfnOldProc) ;

break ;

}

// call CircleCtlProc to process any other messages

return (CallWindowProc(lpfnOldProc, hwnd, msg, wp, lp));

}

;==========================================================

;Circle.def - module definition file for CIRCLE3.VBX control

;==========================================================

LIBRARY CIRCLE

EXETYPE WINDOWS

DESCRIPTION 'Visual Basic Circle Custom Control'

CODE MOVEABLE

DATA MOVEABLE SINGLE

HEAPSIZE 1024

EXPORTS

WEP @1 RESIDENTNAME

SbClsProc @2

;------------------------------------------------------

Additional reference words: 1.00