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.

Comments

Improved column resize event

Posted by Sam
on 12/19/2013 11:36am

I found that the routine in the zip file wasn't reliable enough.
The following changes worked better for me. I'm sorry that the code is not directly applicable to the code in the zip, but better than nothing.
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private currentMouseColumn As Integer
Private lastCalled As Long
Case WM_LBUTTONDOWN
setMouseCol hWnd 'sub that sets currentMouseColumn to the current mouse column
Case WM_LBUTTONUP
lastCalled = GetTickCount()
Case WM_ERASEBKGND
' WM_ERASEBKGND happens right after WM_LBUTTONUP only on column resize
If Abs(GetTickCount() - lastCalled)

The problem of above code is , those controls are not stick to cells. If I am moving colums in flexgrid. The controls are not moving along with columns. Can you give me suggestion to do this. If you provide code for this, that will help me alot.

Top White Papers and Webcasts

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.