How to draw a custom border inside the non client area of a control with scroll bars?

前端 未结 2 1345
遥遥无期
遥遥无期 2021-01-12 20:44

I have a custom control with both scroll bars enabled and I want to draw a simple red line border around the client area and the scroll bars, like in the image below. How I

2条回答
  •  心在旅途
    2021-01-12 21:17

    You are trying to paint (partial) in the Nonclient Area.
    You could add WS_DLGFRAME to the Params.Style and handle the message WM_NCPaint to Paint on the HDC of the window.

      TSuperList = class(TCustomControl)
      private
        procedure PaintBorder;
        procedure WMNCActivate(var Msg: TWMNCActivate); message WM_NCActivate;
        procedure WMNCPaint(var Msg: TWMNCPaint);message WM_NCPaint;
      protected
        procedure   Paint; override;
        procedure   CreateParams(var Params: TCreateParams); override;
      public
        constructor Create(AOwner: TComponent); override;
      end;
    
    procedure TSuperList.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      Params.Style:=Params.Style or WS_VSCROLL or WS_HSCROLL or WS_DLGFRAME;
    end;
    
    procedure TSuperList.WMNCActivate(var Msg: TWMNCActivate);
    begin
      inherited;
      PaintBorder;
    end;
    
    procedure TSuperList.WMNCPaint(var Msg: TWMNCPaint);
    begin
      inherited;
      PaintBorder;
    end;
    
    procedure TSuperList.PaintBorder;
    begin
      Canvas.Handle := GetWindowDC(Handle);
      Canvas.Pen.Color := clNavy;
      Canvas.Pen.Width := 2;
      Canvas.Brush.Style := bsClear;
      Canvas.Rectangle( Rect(1,1,Width,Height) );
      ReleaseDC(Handle,Canvas.Handle);
    end;    
    
    constructor TSuperList.Create(AOwner: TComponent);
    begin
     inherited;
     Color:=clBlack;
     Width:=300;
     Height:=250;
    end;
    
    procedure TSuperList.Paint;
    begin
     Canvas.Brush.Color:=clWhite;
     Canvas.Pen.Style := psClear;
     Canvas.Rectangle(ClientRect);
     Canvas.Pen.Style := psSolid;
     Canvas.Ellipse(0,0,20,20);
    end;
    

    enter image description here

提交回复
热议问题