2011-11-08 38 views
20
  • 所以,我有一个加载不同的插件,并创建一个TPageControl为每一个 新标签的应用程序。
  • 每个DLL都有一个与之关联的TForm的。
  • 的形式与父母的hWnd作为新TTabSheet创建。
  • 由于Vab是关于TTabSheets不是表单的父级(不想使用动态RTL,并且使用其他语言编写的插件)我必须手动处理调整大小。我这样做象下面这样:的TLabel和TGroupbox字幕闪烁在调整大小

    var 
        ChildHandle : DWORD; 
    begin 
        If Assigned(pcMain.ActivePage) Then 
        begin 
        ChildHandle := FindWindowEx(pcMain.ActivePage.Handle, 0, 'TfrmPluginForm', nil); 
        If ChildHandle > 0 Then 
         begin 
         SetWindowPos(ChildHandle, 0, 0, 0, pcMain.ActivePage.Width, pcMain.ActivePage.Height, SWP_NOZORDER + SWP_NOACTIVATE + SWP_NOCOPYBITS); 
        end; 
        end; 
    

现在,我的问题是,当应用程序的大小时,所有的TGroupBoxes和TGroupBoxes闪烁内TLabels。不在TGroupbox内的TLabel很好,不会闪烁。

事情我已经尝试:

  • WM_SETREDRAW随后在TGroupBoxes和TLabels一个RedrawWindow
  • ParentBackground设置为False
  • 双缓冲:=真
  • LockWindowUpdate(是,尽管我知道这是非常非常错误的
  • 透明:=假(甚至重写创建编辑了ControlState-

任何想法?

+0

这个问题有答案中和评论一些额外的想法:http://stackoverflow.com/q uestions/4031147 – Argalatyr

回答

25

我已经发现工作得很好,唯一的事情就是使用WS_EX_COMPOSITED窗口风格。这是一个表演猪,所以我只在启用循环时启用它。根据我的经验,使用内置控件,在我的应用程序中,只有在调整窗体大小时才会发生闪烁。

首先要进行一个快速测试,看看这种方法会帮助你通过简单地将WS_EX_COMPOSITED窗口样式到所有窗口的控件。如果这样的作品,你可以考虑下面的更先进的方法:

快速劈

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; 

调用该方法,例如,在OnShowTForm,传递形式的实例。如果这有帮助,那么你真的应该更聪明地实施它。我给你从我的代码的相关摘录来说明我是如何做到这一点的。

的完整代码

procedure TMyForm.WMEnterSizeMove(var Message: TMessage); 
begin 
    inherited; 
    BeginSizing; 
end; 

procedure TMyForm.WMExitSizeMove(var Message: TMessage); 
begin 
    EndSizing; 
    inherited; 
end; 

procedure SetComposited(WinControl: TWinControl; Value: Boolean); 
var 
    ExStyle, NewExStyle: DWORD; 
begin 
    ExStyle := GetWindowLong(WinControl.Handle, GWL_EXSTYLE); 
    if Value then begin 
    NewExStyle := ExStyle or WS_EX_COMPOSITED; 
    end else begin 
    NewExStyle := ExStyle and not WS_EX_COMPOSITED; 
    end; 
    if NewExStyle<>ExStyle then begin 
    SetWindowLong(WinControl.Handle, GWL_EXSTYLE, NewExStyle); 
    end; 
end; 

function TMyForm.SizingCompositionIsPerformed: Boolean; 
begin 
    //see The Old New Thing, Taxes: Remote Desktop Connection and painting 
    Result := not InRemoteSession; 
end; 
procedure TMyForm.BeginSizing; 
var 
    UseCompositedWindowStyleExclusively: Boolean; 
    Control: TControl; 
    WinControl: TWinControl; 
begin 
    if SizingCompositionIsPerformed then begin 
    UseCompositedWindowStyleExclusively := Win32MajorVersion>=6;//XP can't handle too many windows with WS_EX_COMPOSITED 
    for Control in ControlEnumerator(TWinControl) do begin 
     WinControl := TWinControl(Control); 
     if UseCompositedWindowStyleExclusively then begin 
     SetComposited(WinControl, True); 
     end else begin 
     if WinControl is TPanel then begin 
      TPanel(WinControl).FullRepaint := False; 
     end; 
     if (WinControl is TCustomGroupBox) or (WinControl is TCustomRadioGroup) or (WinControl is TCustomGrid) then begin 
      //can't find another way to make these awkward customers stop flickering 
      SetComposited(WinControl, True); 
     end else if ControlSupportsDoubleBuffered(WinControl) then begin 
      WinControl.DoubleBuffered := True; 
     end; 
     end; 
    end; 
    end; 
end; 

procedure TMyForm.EndSizing; 
var 
    Control: TControl; 
    WinControl: TWinControl; 
begin 
    if SizingCompositionIsPerformed then begin 
    for Control in ControlEnumerator(TWinControl) do begin 
     WinControl := TWinControl(Control); 
     if WinControl is TPanel then begin 
     TPanel(WinControl).FullRepaint := True; 
     end; 
     UpdateDoubleBuffered(WinControl); 
     SetComposited(WinControl, False); 
    end; 
    end; 
end; 

function TMyForm.ControlSupportsDoubleBuffered(Control: TWinControl): Boolean; 
const 
    NotSupportedClasses: array [0..1] of TControlClass = (
    TCustomForm,//general policy is not to double buffer forms 
    TCustomRichEdit//simply fails to draw if double buffered 
); 
var 
    i: Integer; 
begin 
    for i := low(NotSupportedClasses) to high(NotSupportedClasses) do begin 
    if Control is NotSupportedClasses[i] then begin 
     Result := False; 
     exit; 
    end; 
    end; 
    Result := True; 
end; 

procedure TMyForm.UpdateDoubleBuffered(Control: TWinControl); 

    function ControlIsDoubleBuffered: Boolean; 
    const 
    DoubleBufferedClasses: array [0..2] of TControlClass = (
     TMyCustomGrid,//flickers when updating 
     TCustomListView,//flickers when updating 
     TCustomStatusBar//drawing infidelities , e.g. my main form status bar during file loading 
    ); 
    var 
    i: Integer; 
    begin 
    if not InRemoteSession then begin 
     //see The Old New Thing, Taxes: Remote Desktop Connection and painting 
     for i := low(DoubleBufferedClasses) to high(DoubleBufferedClasses) do begin 
     if Control is DoubleBufferedClasses[i] then begin 
      Result := True; 
      exit; 
     end; 
     end; 
    end; 
    Result := False; 
    end; 

var 
    DoubleBuffered: Boolean; 

begin 
    if ControlSupportsDoubleBuffered(Control) then begin 
    DoubleBuffered := ControlIsDoubleBuffered; 
    end else begin 
    DoubleBuffered := False; 
    end; 
    Control.DoubleBuffered := DoubleBuffered; 
end; 

procedure TMyForm.UpdateDoubleBuffered; 
var 
    Control: TControl; 
begin 
    for Control in ControlEnumerator(TWinControl) do begin 
    UpdateDoubleBuffered(TWinControl(Control)); 
    end; 
end; 

这不会编译你,但它应该包含了一些有益的思路。 ControlEnumerator是我的工具翻子控件的递归走进平坦for循环。请注意,我还使用一个自定义分隔符,它在活动时调用BeginSizing/EndSizing。

另一个有用的技巧是使用TStaticText而不是TLabel,当您有很多页面控件和面板嵌套时,您偶尔需要执行此操作。

我用这个代码,以使我的应用程序100%,无闪烁,但我花了年龄和年龄试验,以得到它全部到位的。希望其他人可以在这里找到有用的东西。

+3

+1,当使用面板和页面控件代替TLabel时,TStaticText节省您的一天。 –

+0

哦,是的,我肯定能找到一些有用的东西在这里:-)感谢和+1 –

+2

很不错的资料,并已解决了我的问题 – ThievingSix

10

使用VCL Fix PackAndreas Hausladen

此外:不指定SWP_NOCOPYBITS标志,并设置的PageControl的DoubleBuffered

uses 
    VCLFixPack; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    PageControl1.DoubleBuffered := True; 

    //Setup test conditions: 
    FForm2 := TForm2.Create(Self); 
    FForm2.BorderStyle := bsNone; 
    FForm2.BoundsRect := TabSheet1.ClientRect; 
    Windows.SetParent(FForm2.Handle, TabSheet1.Handle); 
    FForm2.Show; 
    PageControl1.Anchors := [akLeft, akTop, akRight, akBottom]; 
    PageControl1.OnResize := PageControl1Resize; 
end; 

procedure TForm1.PageControl1Resize(Sender: TObject); 
begin 
    SetWindowPos(FForm2.Handle, 0, 0, 0, TabSheet1.ClientWidth, 
    TabSheet1.ClientHeight, SWP_NOZORDER + SWP_NOACTIVATE); 
end; 
+1

我还没有听说过VCL Fix Pack,我会试试看。 – ThievingSix

1

这是解决方案,我的成功在我的项目在许多形式使用。它有点脏,因为它使用winapi函数。与大卫回答相比,它不包括表现的惩罚。重点是为表单及其所有子窗口覆盖WM_ERASEBKGND消息的消息处理程序。

typedef LRESULT CALLBACK(*PWndProc)(HWND, UINT, WPARAM, LPARAM); 

void SetNonFlickeringWndProc(TWinControl &control, std::map<HWND,PWndProc> &list, PWndProc new_proc) 
{ 
    if (control.Handle == 0) 
    { 
     return; 
    } 

    PWndProc oldWndProc = (PWndProc)SetWindowLong(control.Handle, GWL_WNDPROC, (LONG)new_proc); 
    list[control.Handle] = oldWndProc; 

    int count = control.ControlCount; 
    for (int i = 0; i < count; i++) 
    { 
     TControl *child_control = control.Controls[i]; 
     TWinControl *child_wnd_control = dynamic_cast<TWinControl*>(child_control); 
     if (child_wnd_control == NULL) 
     { 
     continue; 
     } 

     SetNonFlickeringWndProc(*child_wnd_control, list, new_proc); 
    } 
} 

void RestoreWndProc(std::map<HWND,PWndProc> &old_wnd_proc) 
{ 
    std::map<HWND,PWndProc>::iterator it; 
    for (it = old_wnd_proc.begin(); it != old_wnd_proc.end(); it++) 
    { 
     LONG res = SetWindowLong(it->first, GWL_WNDPROC, (LONG)it->second); 
    } 
    old_wnd_proc.clear(); 
} 

std::map<HWND,PWndProc> oldwndproc; // addresses for window procedures for all components in form 

LRESULT CALLBACK NonFlickeringWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    if (uMsg == WM_ERASEBKGND) 
    { 
     return 1; 
    } 
    return ((PWndProc)oldwndproc[hwnd])(hwnd, uMsg, wParam, lParam); 
} 

void __fastcall TForm1::FormShow(TObject *Sender) 
{ 
    oldwndproc.clear(); 
    SetNonFlickeringWndProc(*this, oldwndproc, &NonFlickeringWndProc); 
} 

void __fastcall TForm1::FormClose(TObject* Sender, TCloseAction& Action) 
{ 
    RestoreWndProc(oldwndproc_etype); 
} 

重要提示:如果您不想在边上看到黑色条纹,必须设置表单的DoubleBufferd属性!

0

把你的形式(接口)以上或把它们都放在一个新的最后一个单元,包括:

TLabel = class(stdCtrls.TLabel) 
    protected 
    procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND; 
    end; 

实施部分

procedure TLabel.WMEraseBkgnd(var Message: TWmEraseBkgnd); 
begin 
Message.Result:=1; // Fake erase 
end; 

重复此步骤将这个TGroupBox

相关问题