Raising ColumnResize event in MSFlexGrid (VB 5.0)

Raising ColumnResize event in MSFlexGrid (VB 5.0)

Knowledge of API programming is required.

While working with MSFlexGrid control the only possibility to enter data is to place another control over it.
When user clicks on a cell of the grid, you should move the control just over the selected cell and give it a focus.
But the main problem with such ‘Flying Dutchman’ is the following:
when grid is resizing no event occurs, and your control may lose fit.
So the task is: How do I catch or emulate the ColumnResize event?



The decision provided is based on the event sequence.
In a few words: when user simply clicks over the grid certain events come in the certain order
which differs from the order of event occurring when user changes the column width.



Download the project end compile it.



Look at the code placed within the form module.
Here we are trying to replace the default event handler routine with our own function
GridMessage which (with all necessary API declarations) is situated in the module.
We are using the AddressOf operator to provide a function pointer,
which is required by SetWindowLong API procedure.



Look at the function GridMessage.
As you can see, the routine traps only 3 events, which are described by constants
WM_LBUTTONDOWN, WM_LBUTTONUP and WM_ERASEBKGND.
(All other windows messages passed through untouched right into the arms of the default message handler.)
The first two are mouse events when you are resizing column you first press the mouse button and then release it.
But to distinguish the resizing from simple mouse click we need to hire the third event,
WM_ERASEBKGND. In general, the
WM_ERASEBKGND message is sent when the window background must be erased
(for example, when a window is resized).
In conjunction with two previous events this allows us to discern the cases
when user resizes column’s width or row’s height.
This is explained by the following table
(note that here we concentrate our attention only upon these three events,
though many other kinds of events are also being sent by the application.)




















What happens

User makes a mouse click

User resizes a column

Sequence of the windows events

WM_LBUTTONDOWN

WM_LBUTTONDOWN

WM_ERASEBKGND

WM_LBUTTONUP

WM_LBUTTONUP

WM_ERASEBKGND






Run the project and make sure that resize events are processed by
GridMessage function.
It prints a line in the debug window each time the resize occurs.
Now you can write your own procedure and place its call instead of Debug.Print.
The Demo project demonstrates how this feature is used to keep all
grid columns always of equal size.





Side effects:



When running in debug mode, never finish
the application with the End button on VB environment panel.
This will cause the termination of VB.




Download Zipped Project Files (3k)

Screen Shot

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read