Solved

Changing a form from color to grayscale and back to color

Posted on 2002-03-24
7
281 Views
Last Modified: 2010-04-05
How do you change the color of a form to grayscale and back to color. I am trying to use this on the main form when the user makes the smaller form active giving the visual look of the main form inactive.  I've seen this done in applications including windows XP.
0
Comment
Question by:alspivey
7 Comments
 
LVL 3

Expert Comment

by:tongalite
ID: 6892548
Hi
well I guess you could change the mainform color to
silver(gray) when form2 becomes the active form and
reverse the process when the mainform receives the
focus. Double click the color listbox in Object Inspector
to bring up the colordialogbox... choose your custom color
and its value will be shown in the listbox. Use the value to hardcode the color of your mainform and the color you want it to be when inactive.... Have I over-simplified the situation... sorry if I misunderstood what you want :)

example:

Form1.Color:=TColor($00C9C9C9); //almost silver
Form1.Color:=TColor($00FFC082); //light shade of blue

tongalite
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6893435
Put these declarations in your main form's Private section:

    MDIDefProc: Pointer;
    MDIInstance: TFarProc;

    procedure MDIWndProc(var prmMsg: TMessage);
    procedure ShowGradient(prmDC: hDC;prmRed, prmGreen, prmBlue: byte);
    procedure CreateWnd;override;


Implemented like this:

procedure TfrmMain.MDIWndProc(var prmMsg: TMessage);
begin
     // This is where we set the colour for the gradient on the main form
     With prmMsg Do
     Begin
          // Only perform this if the number of colours are above 256.
          If (Msg = WM_ERASEBKGND)
          And (GetDeviceCaps(Canvas.Handle, BITSPIXEL) *
               GetDeviceCaps(Canvas.Handle, PLANES) >= 15) then
          Begin
               ShowGradient(wParam,127,127,127);
               Result := 1;
          End
          Else
          Result := CallWindowProc(MDIDefProc,ClientHandle,Msg,wParam,lParam);
     end;
end;

procedure TfrmMain.CreateWnd;
begin
     // This is part of the gradient processing
     inherited CreateWnd;
     MDIInstance := MakeObjectInstance(MDIWndProc); { create wrapper }
     MDIDefProc := pointer(SetWindowLong(ClientHandle,GWL_WNDPROC,
     longint(MDIInstance)));
end;

procedure TfrmMain.ShowGradient(prmDC:hDC;prmRed,prmGreen,prmBlue:byte);
var
   wrkBrushNew,wrkBrushOld: hBrush;
   wrkColor:TColor;
   wrkSize,wrkY,wrkDelta: integer;
   wrkRect:TRect;
begin
     // Gradient Routine.
     wrkDelta := 255 div (1 + ClientHeight); { number of shades desired }
     If wrkDelta = 0 Then wrkDelta := 1;     { yes, usually 1 }
     wrkSize := ClientHeight div 100;      { size of blended bars }
     If wrkSize = 0 Then wrkSize := 1;
     For wrkY := 0 To 1 + (ClientHeight div wrkSize) Do
     Begin
          wrkColor := RGB(prmRed,prmGreen,prmBlue);
          wrkRect := Rect(0, wrkY*wrkSize, ClientWidth,(wrkY + 1)*wrkSize);
          If FormStyle = fsNormal Then
          Begin
               Canvas.Brush.Color := wrkColor;
               Canvas.FillRect(wrkRect);
          End
          Else If FormStyle = fsMDIForm then
          Begin
               wrkBrushNew := CreateSolidBrush(wrkColor);
               wrkBrushOld := SelectObject(prmDC,wrkBrushNew);
               FillRect(prmDC,wrkRect,wrkBrushNew);
               SelectObject(prmDC,wrkBrushOld);
               DeleteObject(wrkBrushNew);
          End;
          If prmRed >wrkDelta then Dec(prmRed,wrkDelta);
          If prmGreen > wrkDelta then Dec(prmGreen,wrkDelta);
          If prmBlue  > wrkDelta then Dec(prmBlue,wrkDelta);
     end;
end;

J.
0
 
LVL 17

Expert Comment

by:geobul
ID: 6893571
Hi,
Why not simply to disable all the the controls on the main form?

in main form:
--------
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ControlsEnabled(value: boolean);
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.ControlsEnabled(value: boolean);
var
  i: integer;
begin
  with Form1 do
    for i := 0 to ComponentCount - 1 do
      if (Components[i] is TControl) then
        (Components[i] as TControl).Enabled := value;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ControlsEnabled(false);
  Form2.Show;
end;
--------
in form2:
----------
type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

uses unit1;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.ControlsEnabled(true);
end;
----------

Regards, Geo
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:alspivey
ID: 6894346
Sorry guys maybe I was not clear on what i'm trying to do. I am disabling the form and buttons on the main form while the user is on the secondary form. What i'm trying to do is what windows xp does when it turnes a form to black and white, gray scale. This is only a cosmetic look, I know i could turn the form and each button to a gray color. I was just looking for a API or something like that.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 200 total points
ID: 6906546
Hello alspivey, Here is some code I have used to color an Entire Form to show a "Not Enabled" state. It may not be what you want, but it will show that the form is "Different" than normal. There are 2 Forms, Form1 (main) and Form2.


procedure TForm1.FormPaint(Sender: TObject);
var
FmDc: HDC;
begin
if Not Enabled then
  begin
  FmDc := GetWindowDC(Form1.Handle);
  SelectObject(FmDc, GetStockObject(GRAY_BRUSH));
  PatBlt(FmDc, 0, 0, Width,Height,$A000C9);
  ReleaseDC(Form1.Handle, FmDc);
  end;

end;


procedure TForm1.Button1Click(Sender: TObject);
{this button shows the Form2 and disables Form1}
var
  FmDc: HDC;
begin
Form2.Show;
Application.ProcessMessages;
Form1.Enabled := False;
FmDc := GetWindowDC(Form1.Handle);
SelectObject(FmDc, GetStockObject(GRAY_BRUSH));
PatBlt(FmDc, 0, 0, Width,Height,$A000C9);
ReleaseDC(Form1.Handle, FmDc);
end;

This is the close Button on Form 2

procedure TForm2.SpeedButton1Click(Sender: TObject);
begin
Form1.Enabled := True;
Close;
Form1.Invalidate;
end;

you may need to also Repaint any Non-Graphical control.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6906558
sorry, I was lookin at an early version, the close for form 2 should be


procedure TForm2.SpeedButton1Click(Sender: TObject);
begin
Close;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Form1.Enabled := True;
Form1.Invalidate;
Action := caHide
end;
0
 

Author Comment

by:alspivey
ID: 6913417
Thanks for the info. This will work for what i'm trying to do, and will not restrict the os like a transparent vcl would do.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now