Maintaining a Device Context

All writing to a window's client area is performed through a Windows device context, or DC for short. The Windows API function GetDC() obtains a device context (in Visual Basic, we just use the hDC property), and the function fg_setdc() tells Fastgraph about it. These functions typically appear in the WM_CREATE section of the window procedure, as shown here. If a Fastgraph program does not call fg_setdc(), calls to fg_vbpaste() and fg_vbscale() will not work.

C/C++:

hDC = GetDC(hWnd);
fg_setdc(hDC);

C++Builder:

hDC = GetDC(Form1->Handle);
fg_setdc(hDC);

Delphi:

dc := GetDC(Form1.Handle);
fg_setdc(dc);

Visual Basic:

Call fg_setdc(hDC)

The WM_DESTROY handler should then use the Windows API function ReleaseDC() to release the device context:

C/C++:

ReleaseDC(hWnd,hDC);

C++Builder:

ReleaseDC(Form1->Handle,hDC);

Delphi:

ReleaseDC(Form1.Handle,dc);

In C/C++ programs, the device context returned by GetDC() cannot be stored in a local variable (unless declared static), as it must retain its value across window procedure calls. Visual Basic programs don't need to release the device context, since that's handled automatically.

The C/C++, MFC, and PowerBASIC versions of the Fastgraph example programs all specify the CS_OWNDC style flag when setting up the window class. This flag tells Windows to assign a unique device context to each window, and further guarantees that Windows will not reclaim any of our device contexts while the program executes. The C++Builder and Delphi versions do not do this, but we can specify CS_OWNDC in C++Builder and Delphi programs by overriding a form's CreateParams() method. To do this, we must add the following declaration to the form's class definition:

C++Builder:

virtual void __fastcall CreateParams(TCreateParams &Params);

Delphi:

procedure Create
Params(var Params: TCreateParams); override;

Then add our CreateParams() function to the source code module where you define the form's event handlers:

C++Builder:

void __fastcall TFo
rm1::CreateParams(TCreateParams &Params)
{
  TForm::CreateParams(Params);
  Params.WindowClass.style |= CS_OWNDC;
}

Delphi:

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  Inherited CreateParams(Params);
  Params.WindowClass.style :=
    Params.WindowClass.style or CS_OWNDC;
end;

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.