定义和创建:
TWinControl = class(TControl) private FBrush: TBrush; end; constructor TWinControl.Create(AOwner: TComponent); begin inherited Create(AOwner); FObjectInstance := Classes.MakeObjectInstance(MainWndProc); FBrush := TBrush.Create; FBrush.Color := FColor; FParentCtl3D := True; FTabOrder := -1; end;
三处使用:
procedure TWinControl.DefaultHandler(var Message); begin if FHandle <> 0 then begin with TMessage(Message) do begin if (Msg = WM_CONTEXTMENU) and (Parent <> nil) then begin Result := Parent.Perform(Msg, WParam, LParam); if Result <> 0 then Exit; end; case Msg of WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC: Result := SendMessage(LParam, CN_BASE + Msg, WParam, LParam); CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC: begin SetTextColor(WParam, ColorToRGB(FFont.Color)); SetBkColor(WParam, ColorToRGB(FBrush.Color)); Result := FBrush.Handle; // 使用不算,还要返回 end; else if Msg = RM_GetObjectInstance then Result := Integer(Self) else begin if Msg <> WM_PAINT then Result := CallWindowProc(FDefWndProc, FHandle, Msg, WParam, LParam); end; end; if Msg = WM_SETTEXT then SendDockNotification(Msg, WParam, LParam); end; end else inherited DefaultHandler(Message); end; procedure TWinControl.CMColorChanged(var Message: TMessage); begin inherited; FBrush.Color := FColor; // 改变刷子的颜色 NotifyControls(CM_PARENTCOLORCHANGED); end; procedure TWinControl.WMNCPaint(var Message: TMessage); const InnerStyles: array[TBevelCut] of Integer = (0, BDR_SUNKENINNER, BDR_RAISEDINNER, 0); OuterStyles: array[TBevelCut] of Integer = (0, BDR_SUNKENOUTER, BDR_RAISEDOUTER, 0); EdgeStyles: array[TBevelKind] of Integer = (0, 0, BF_SOFT, BF_FLAT); Ctl3DStyles: array[Boolean] of Integer = (BF_MONO, 0); var DC: HDC; RC, RW, SaveRW: TRect; EdgeSize: Integer; WinStyle: Longint; begin { Get window DC that is clipped to the non-client area } if (BevelKind <> bkNone) or (BorderWidth > 0) then begin DC := GetWindowDC(Handle); try Windows.GetClientRect(Handle, RC); GetWindowRect(Handle, RW); MapWindowPoints(0, Handle, RW, 2); OffsetRect(RC, -RW.Left, -RW.Top); ExcludeClipRect(DC, RC.Left, RC.Top, RC.Right, RC.Bottom); { Draw borders in non-client area } SaveRW := RW; InflateRect(RC, BorderWidth, BorderWidth); RW := RC; if BevelKind <> bkNone then begin EdgeSize := 0; if BevelInner <> bvNone then Inc(EdgeSize, BevelWidth); if BevelOuter <> bvNone then Inc(EdgeSize, BevelWidth); with RW do begin WinStyle := GetWindowLong(Handle, GWL_STYLE); if beLeft in BevelEdges then Dec(Left, EdgeSize); if beTop in BevelEdges then Dec(Top, EdgeSize); if beRight in BevelEdges then Inc(Right, EdgeSize); if (WinStyle and WS_VSCROLL) <> 0 then Inc(Right, GetSystemMetrics(SM_CYVSCROLL)); if beBottom in BevelEdges then Inc(Bottom, EdgeSize); if (WinStyle and WS_HSCROLL) <> 0 then Inc(Bottom, GetSystemMetrics(SM_CXHSCROLL)); end; DrawEdge(DC, RW, InnerStyles[BevelInner] or OuterStyles[BevelOuter], Byte(BevelEdges) or EdgeStyles[BevelKind] or Ctl3DStyles[Ctl3D] or BF_ADJUST); end; IntersectClipRect(DC, RW.Left, RW.Top, RW.Right, RW.Bottom); RW := SaveRW; { Erase parts not drawn } OffsetRect(RW, -RW.Left, -RW.Top); Windows.FillRect(DC, RW, Brush.Handle); // 使用刷子绘制非客户区 finally ReleaseDC(Handle, DC); end; end; inherited; if ThemeServices.ThemesEnabled and (csNeedsBorderPaint in ControlStyle) then ThemeServices.PaintBorder(Self, False); end;
在TForm里也有应用:
procedure TCustomForm.WMIconEraseBkgnd(var Message: TWMIconEraseBkgnd); begin if FormStyle = fsMDIChild then if (FormStyle = fsMDIChild) and not (csDesigning in ComponentState) then FillRect(Message.DC, ClientRect, Application.MainForm.Brush.Handle) else inherited; end; procedure TCustomForm.CMColorChanged(var Message: TMessage); begin inherited; if FCanvas <> nil then FCanvas.Brush.Color := Color; end;
颜色改变之后,重设Brush的颜色是靠TWinControl.CMColorChanged。但唯一不明白的是,Brush第一次正确设置颜色是在何处(不是指创建TWinControl时的-16777201值)?我估计是在读取dfm的时候,颜色被改变了,就会触发TWinControl.CMColorChanged
研究一下FBrush,它是从TWinControl才有的属性(可能是因为需要句柄)
原文:http://www.cnblogs.com/findumars/p/5218682.html