用图片给VCL Form做背景的方法…

开始我在Form的Paint里直接Canvas画图,发现只要一repaint就会闪烁,很明显是先画了背景再画图片造成的,但我发现就算我截下WM_ERASEBKGND直接返回true也不能阻止其重画背景颜色,后来搜到csdn上也有人问到,最后给出的可用解决方法让我觉得很奇怪:首先截下WM_ERASEBKGND返回false,然后子类化整个form的window,处理窗口消息处理函数中的WM_ERASEBKGND和WM_HSCROLL,WM_VSCROLL…


xTiNt    Pointer ptrOriginalClientProc;
    Pointer ptrClientObjectInstance;
    [color=#0000D0]virtual[/color] [color=#0000D0]void[/color] [color=#0000D0]__fastcall[/color] ClientProc(TMessage &aMsg);
    [color=#0000D0]virtual[/color] [color=#0000D0]void[/color] [color=#0000D0]__fastcall[/color] DrawClientWindow([color=#0000D0]HDC[/color] aHDc);
[color=#0000D0]void[/color] [color=#0000D0]__fastcall[/color] TMainForm::ClientProc(TMessage &aMsg)
{
    [color=#0000D0]switch[/color](aMsg.Msg)
    {
        [color=#0000D0]case[/color] WM_ERASEBKGND:
            DrawClientWindow(([color=#0000D0]HDC[/color])aMsg.WParam);
            aMsg.Result = [color=#0000D0]true[/color];
            [color=#0000D0]return[/color];
        [color=#0000D0]case[/color] WM_HSCROLL:
        [color=#0000D0]case[/color] WM_VSCROLL:
            aMsg.Result = [b][color=#000080]CallWindowProc[/color][/b](
                ([color=#0000D0]WNDPROC[/color])ptrOriginalClientProc,
                ClientHandle,aMsg.Msg,aMsg.WParam,aMsg.LParam);
            [b][color=#000080]InvalidateRect[/color][/b](ClientHandle,0,[color=#0000D0]true[/color]);
            [color=#0000D0]break[/color];
        [color=#0000D0]default[/color]:
            aMsg.Result = [b][color=#000080]CallWindowProc[/color][/b](
                ([color=#0000D0]WNDPROC[/color])ptrOriginalClientProc,
                ClientHandle,aMsg.Msg,aMsg.WParam,aMsg.LParam);
    }
}
[color=#0000D0]void[/color] [color=#0000D0]__fastcall[/color] TMainForm::OnEraseBkgnd(TWMEraseBkgnd &aMsg)
{
    aMsg.Result = [color=#0000D0]false[/color];
}
[color=#0000D0]void[/color] [color=#0000D0]__fastcall[/color] TMainForm::FormCreate(TObject *Sender)
{
    ptrClientObjectInstance = MakeObjectInstance(ClientProc);
    ptrOriginalClientProc =
        (Pointer)[b][color=#000080]SetWindowLong[/color][/b](ClientHandle,GWL_WNDPROC,
            ([color=#0000D0]long[/color])ptrClientObjectInstance);
}
[color=#0000D0]void[/color] [color=#0000D0]__fastcall[/color] TMainForm::FormDestroy(TObject *Sender)
{
    [b][color=#000080]SetWindowLong[/color][/b](ClientHandle,GWL_WNDPROC,([color=#0000D0]long[/color])ptrOriginalClientProc);
    FreeObjectInstance(ptrClientObjectInstance);
}
本代码由xTiNt自动着色 http://kbadboy.yeah.net

博主友情提示:

如在您的评论中需要提及如QQ号、微信号、电子邮件地址或其他隐私敏感信息,欢迎通过>>博主专用加密工具v2<<加密后发布,原文只有博主可以看到。