Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
KANSoftWare

Решение проблемы мигания TGroupBoxes и TLabels в Delphi

Delphi , Компоненты и Классы , TGroupBox

TLabel and TGroupBox Captions Flicker on Resize in Delphi

Problem Description: When resizing a form that loads different plugins, each with its own TForm associated, the TLabels inside TGroupBoxes flicker. The TLabels outside TGroupBoxes and the TGroupBoxes themselves do not flicker. Several attempts to resolve this issue, such as using WM_SETREDRAW, setting ParentBackground to False, enabling DoubleBuffer, using LockWindowUpdate, and setting Transparent to False, have not been successful.

Solution:

  1. Using WS_EX_COMPOSITED Window Style:

The most effective solution is to use the WS_EX_COMPOSITED window style. This can be a performance hog, so it's recommended to enable it only during a sizing loop. Here's how to implement it:

```pascal procedure EnableComposited(WinControl: TWinControl); var i: Integer; NewExStyle: DWORD; begin NewExStyle := GetWindowLong(WinControl.Handle, GWL_EXSTYLE) or WS_EX_COMPOSITED; SetWindowLong(WinControl.Handle, GWL_EXSTYLE, NewExStyle);

 for i := 0 to WinControl.ControlCount-1 do
   if WinControl.Controls[i] is TWinControl then
     EnableComposited(TWinControl(WinControl.Controls[i]));

end; ```

Call this procedure in the OnShow event of your form, passing the form instance.

  1. Overriding WM_ERASEBKGND Message:

Another approach is to override the WM_ERASEBKGND message to prevent repeated drawing to the same area, which causes flicker. Here's how to do it:

```pascal type PWndProc = function(hWnd: HWND; Msg: UINT; wParam, lParam: Longint): Longint; stdcall;

procedure SetNonFlickeringWndProc(control: TWinControl; var oldWndProc: TArray; new_proc: PWndProc); var i: Integer; begin if control.Handle = 0 then Exit;

 oldWndProc[OldWndProcCount] := GetWindowLong(control.Handle, GWL_WNDPROC);
 SetWindowLong(control.Handle, GWL_WNDPROC, Longint(new_proc));

 for i := 0 to control.ControlCount - 1 do
   if control.Controls[i] is TWinControl then
     SetNonFlickeringWndProc(TWinControl(control.Controls[i]), oldWndProc, new_proc);

end;

procedure RestoreWndProc(var oldWndProc: TArray); var i: Integer; begin for i := Low(oldWndProc) to High(oldWndProc) do if oldWndProc[i] <> nil then SetWindowLong(i, GWL_WNDPROC, Longint(oldWndProc[i])); end;

var oldWndProc: TArray;

procedure NonFlickeringWndProc(hWnd: HWND; Msg: UINT; wParam, lParam: Longint): Longint; begin if Msg = WM_ERASEBKGND then Exit(1);

 Result := oldWndProc[hWnd](hWnd, Msg, wParam, lParam);

end;

procedure TForm1.FormShow(Sender: TObject); begin SetNonFlickeringWndProc(Self, oldWndProc, @NonFlickeringWndProc); end;

procedure TForm1.FormClose(Sender: TObject; Action: TCloseAction); begin RestoreWndProc(oldWndProc); end; ```

This solution uses a map to store the old window procedures and restores them when the form is closed.

Alternative Solution:

Another alternative is to use TStaticText instead of TLabel when dealing with deep nesting of page controls and panels, as suggested by David Heffernan in the comments.

In conclusion, the best solution for preventing TLabel and TGroupBox captions from flickering on resize in Delphi is to use the WS_EX_COMPOSITED window style. If that's not an option, overriding the WM_ERASEBKGND message can also be effective. Using TStaticText instead of TLabel can also help in some cases.

Создано по материалам из источника по ссылке.

В Delphi при перемещении окна, содержащего разные плагины с собственными TForm, наблюдается мерцание TLabels внутри TGroupBox, которое не удается устранить при помощи различных методов, таких как использование WM_SETREDRAW, DoubleBuffer,


Комментарии и вопросы

Получайте свежие новости и обновления по Object Pascal, Delphi и Lazarus прямо в свой смартфон. Подпишитесь на наш Telegram-канал delphi_kansoftware и будьте в курсе последних тенденций в разработке под Linux, Windows, Android и iOS




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.


:: Главная :: TGroupBox ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-12-22 20:14:06
2025-06-16 16:48:33/0.0047190189361572/1