1

I use Windows message WM_SETREDRAW (see MSDN) to lock repainting of certain controls. Normally, it is sent pairwise.
But there are some situations, where I cannot assure this (e.g. when its triggered by a - usually, but not everytime, pairwise - callback event of a third-party visual component).

There already is a counter to globally lock various actions if at least one control has been locked. This counter is violated when the message is not sent pairwise.

Therefore, I am looking for a possibility to check whether a control has been locked. I appreciate alternative ideas to fix this as well.
Thank you in advance.

These are my wrapper routines for sending WM_SETREDRAW:

function IsValidWinControlToUnLock(const WinControl: TWinControl): Boolean;
begin
  Result := Assigned(WinControl) and
           (WinControl.Handle <> 0) and
           ControlIsVisible(WinControl); // assure the control and all its ancestors (via Parent) are visible
end;                        

function LockWinControl(const WinControl: TWinControl): Boolean;
begin
  Result := IsValidWinControlToUnLock(WinControl);
  if not Result then exit;

  Inc(MyGlobalLockCounter);

  WinControl.Perform(WM_SETREDRAW, 0);
end;                         

function UnlockWinControl(const WinControl: TWinControl): Boolean;
begin                        
  Result := IsValidWinControlToUnLock(WinControl);
  if not Result then exit;

  WinControl.Perform(WM_SETREDRAW, 1);

  Dec(MyGlobalLockCounter);
end;

Note, that the result of LockWinControl is required to be True in the calling code for UnlockWinControlto be called at all. I do this, because a control might have become ValidToUnlock but wasn't ValidToLock in the first place.

In case there is no way to get information via the Windows API (beside the one stated by @Sertac Akyuc) - I already assumed there might be none - I am considering to add an additional param ChangeGlobalLockCounter (or something like that). In those nasty maybe-pairwise callback routines this new param would be set to False then, so that the GlobalLockCounter does not get incremented or decremented and therefore not damaged, if not called pairwise. Further ideas?

6
  • There's no way to do what you ask. You are likely misusing WM_SETREDRAW. And even if it is right to use it, you should do it right. Dec 8, 2014 at 17:47
  • 1
    There is no API to query whether a given HWND is currently locked from drawing. You have to keep track of that yourself, or simply write your code to not need to know (lock drawing, do what you need, and then unlock drawing, all in the same function). Otherwise, you will have to subclass the HWND in order to intercept the message itself. Dec 8, 2014 at 17:48
  • I added some additional information. Please review, thanks. Dec 9, 2014 at 10:17
  • RE (edit): I see you have dismissed the solution I provided. Can you tell why? Dec 9, 2014 at 12:09
  • 2
    WM_SETREDRAW isn't implemented by the window manager, it's just a convention that some window classes support and some do not. How a particular window class tracks its own redraw state is entirely application defined. Dec 10, 2014 at 2:30

1 Answer 1

5

You can indirectly find out if a window is locked for painting. Below Delphi example exploits the fact that you cannot invalidate a locked window:

function IsWindowLocked(Wnd: HWND): Boolean;
begin
  Result := not GetUpdateRect(Wnd, nil, False);
  if Result then begin
    InvalidateRect(Wnd, nil, False);
    Result := not GetUpdateRect(Wnd, nil, False);
    if not Result then
      ValidateRect(Wnd, nil);
  end;
end;


Note that the above does not have a painting overhead, but of course it costs more than checking a counter.

2
  • This is kind of useless though. Not your fault. You just answered the question that was asked. The real problem is the broken code that fails to pair the unlock with the lock. Dec 9, 2014 at 7:56
  • 1
    @David - I have truly no idea what the code is doing or trying to achieve - even after the modification. I have no idea why there have to be pairs. As you know locks don't pile up. You unlock drawing on a control once and it is unlocked, regardless of how many times it has been locked, or at all. As such, I have no idea if my solution is useless or not. I trust you have understood the issue though.. Dec 9, 2014 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.