user1580348
user1580348

Reputation: 6051

How to make disabled image from control?

I've put together this code which creates a grayscale Bitmap from a control:

procedure TForm1.PseudoDisableControl(const AWinControl: TWinControl; const AImage: TImage);
var
  Bmp: TBitmap;
  function Control2Bitmap(Control_: TWinControl): TBitmap;
  // http://delphidabbler.com/tips/24
  begin
    Result := TBitmap.Create;
    with Result do
    begin
      Height := Control_.Height;
      Width := Control_.Width;
      Canvas.Handle := CreateDC(nil, nil, nil, nil);
      Canvas.Lock;
      Control_.PaintTo(Canvas.Handle, 0, 0);
      Canvas.Unlock;
      DeleteDC(Canvas.Handle);
    end;
  end;
type
  PRGB32Array = ^TRGB32Array;
  TRGB32Array = packed array [0 .. MaxInt div SizeOf(TRGBQuad) - 1] of TRGBQuad;
  procedure MakeGrey(Bitmap: TBitmap);
  // http://stackoverflow.com/questions/4101855/converting-a-pngimage-to-grayscale-using-delphi
  var
    w, h: integer;
    y: integer;
    sl: PRGB32Array;
    x: integer;
    grey: byte;
  begin
    Bitmap.PixelFormat := pf32bit;
    w := Bitmap.Width;
    h := Bitmap.Height;
    for y := 0 to h - 1 do
    begin
      sl := Bitmap.ScanLine[y];
      for x := 0 to w - 1 do
        with sl[x] do
        begin
          grey := (rgbBlue + rgbGreen + rgbRed) div 3;
          rgbBlue := grey;
          rgbGreen := grey;
          rgbRed := grey;
        end;
    end;
  end;
begin
  Bmp := Control2Bitmap(AWinControl);
  try
    MakeGrey(Bmp);
    AImage.AutoSize := True;
    AImage.Picture.Bitmap := Bmp;
  finally
    Bmp.Free;
  end;
end;

This creates this result, for example:

enter image description here

However, I need to make the image look like a disabled control:

enter image description here

How could I achieve this?

EDIT: MBo's solution works great for me with a factor of 3. However, with a trackbar control (TAdvTrackbar from TMS), a light area around the trackbar is left:

enter image description here

Upvotes: 1

Views: 1190

Answers (2)

Edijs Kolesnikovičs
Edijs Kolesnikovičs

Reputation: 1695

You can take a screenshot of any window by providing its handle:

function ScreenshotDisabledWindow(const AWindow: HWND;
  out AScreenshotBitmap: TBitmap): boolean;
var
  _Canvas: TCanvas;
  r, t: TRect;
  _WindowEnabled: boolean;
begin
  Result := False;
  if AWindow = 0 then
    Exit;

  _WindowEnabled := IsWindowEnabled(AWindow);
  _Canvas := TCanvas.Create;
  _Canvas.Handle := GetWindowDC(GetDesktopWindow);
  try
    if _WindowEnabled then
      EnableWindow(AWindow, False);

    if AWindow <> 0 then
      GetWindowRect(AWindow, t);

    r := Rect(0, 0, t.Right - t.Left, t.Bottom - t.Top);
    AScreenshotBitmap.Width := t.Right - t.Left;
    AScreenshotBitmap.Height := t.Bottom - t.Top;
    AScreenshotBitmap.Canvas.CopyRect(r, _Canvas, t);
    Result := True;
  finally
    ReleaseDC(0, _Canvas.Handle);
    FreeAndNil(_Canvas);
    EnableWindow(AWindow, _WindowEnabled);
  end;
end;

and you call it like this:

var
  _Image: TBitmap;
begin
  _Image := TBitmap.Create;
  try
    if not ScreenshotDisabledWindow(Button1.Handle, _Image) then
      raise Exception.Create('Invalid handle provided???');

    Image1.Picture.Assign(_Image);
  finally
    FreeAndNil(_Image);
  end;

Upvotes: 0

MBo
MBo

Reputation: 80187

You can generate light grey picture with diminished contrast using formula like this:

grey := (rgbBlue + rgbGreen + rgbRed + 2 * 255) div (3 + 2); // try also 1 instead of 2

But what to do if windows theme uses another color coding scheme for disabled?

Upvotes: 1

Related Questions