Impactnetworking


Borland C ++ Builder FAQ
The content The last update: 12/12/2008

What method of cleaning TCanvas the fastest?

АПИ function PatBlt.
Example:
PatBlt (PaintBox1-> Canvas-> Handle,//Handle Canvas'а 
       0,                        //initial coordinate on X 
       0,                        //initial coordinate on Y 
       PaintBox1-> Width,         //finite coordinate on X 
       PaintBox1-> Height,        //finite coordinate on Y 
       WHITENESS                 //how to paint over 
       );

Why at me RichEdit does not want open and saves RTF?

It is necessary for this purpose RichEdit to install property PlainText in true.

How by means of TMediaPlayer to learn, the film in milliseconds and in frames how many lasts?

MediaPlayer1-> TimeFormat = tfFrames//we install time format in frames 
ShowMessage ("Length frames:" + IntToStr (MediaPlayer1-> Length)); 
MediaPlayer1-> TimeFormat = tfMilliseconds//we install time format in milliseconds 
ShowMessage ("Length milliseconds:" + IntToStr (MediaPlayer1-> Length)); 

How to work with resources?

The author: OlegGG
The initial link: - - -

One of variants:

Manually in a notepad we create a file with a name, for example, "resource.rc".
Inside a format such:
[name] [type] "[a file name]"

To it we write, for example:
BOLSHOI_FILE BIG_FILES "bigfile.txt"

We connect a file.rc to the program (Shift + F11 and there our file is sampled).

And somewhere in the program:
TResourceStream *res_stream = new TResourceStream (0,             //we take from ours exe 
                                                  "BOLSHOI_FILE",//a resource name 
                                                  "BIG_FILES"    //resource type 
                                                  );
res_stream-> SaveToFile ("big_file.bin"); 
delete res_stream; 
Thus, we receive ours "bigfile.txt", saved under a name "big_file.bin".

Naturally, it is better to make exception handling!

How to change color of a cell in TCustomGrid?

Let's assume, we need to change attributes of the text and a background of a line in component TDBGrid if value of any field satisfies to in advance given condition. For this purpose it is accepted to use the handler of event OnDrawColumnCell of this component. We mark that the possibilities given at its usage, are rather various. Let's consider the elementary application with TDBGrid, containing one component TTable, one component TDataSource and one component TDBGrid: we Install values of their properties according to the table resulted more low:

Component Property Value Table1 DatabaseName BCDEMOS (or DBDEMOS) TableName events.db Active true DataSource1 DataSet Table1 DBGrid1 DataSource DataSource1

Fig. 1 Test application at a design stage Normally for image copying in cells method OnDrawColumnCell is used. Its parameter Rect - the structure describing a rectangle occupied with a cell, parameter Column - column DBGrid in which it is necessary to change a method of drawing of the image. For a text output method TextOut of property Canvas of component TDBGrid is used. Let's assume, we need to change text color and a background of a line depending on value of any field (for example, VenueNo). We create the handler of event OnDrawColumnCell of component DBGrid1. In case of a C ++ Builder it looks like:

void __ fastcall TForm1:: DBGrid1DrawColumnCell (TObject *Sender, const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State) { if (Table1-> FieldByName ("VenueNo")-> Value == 1) { DBGrid1-> Canvas-> Brush-> Color=clGreen; DBGrid1-> Canvas-> Font-> Color=clWhite; DBGrid1-> Canvas-> FillRect (Rect); DBGrid1-> Canvas-> TextOut (Rect.Left+2,Rect.Top+2,Column->Field->Text); } }

In case of Delphi the appropriate code looks like:

procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (Table1.FieldByName (' VenueNo ').Value=1) then begin with DBGrid1.Canvas do begin Brush. Color: = clGreen; Font. Color: = clWhite; FillRect (Rect); TextOut (Rect. Left+2, Rect. Top+2, Column. Field. Text); end; end; end;

As a result at a performance stage at display of lines in which field value VenueNo is equal 1, the background of cells will be colored in green color, and the text will be deduced by white color. Fig. 2 Change of background color and a font in lines with field value VenueNo=1 at a performance stage. At an output of the selected lines all data in cells appeared left-justified. If we want to display more correctly text alignment in a column, it is necessary to modify slightly our code, considering value of property Alignment leaking (that is drawn at present) columns:

void __ fastcall TForm1:: DBGrid1DrawColumnCell (TObject *Sender, const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State) { if (Table1-> FieldByName ("VenueNo")-> Value == 1) { DBGrid1-> Canvas-> Brush-> Color=clGreen; DBGrid1-> Canvas-> Font-> Color=clWhite; DBGrid1-> Canvas-> FillRect (Rect); if (Column-> Alignment == taRightJustify) { DBGrid1-> Canvas-> TextOut (Rect. RIGHT-2 DBGrid1-> Canvas-> TextWidth (Column-> Field-> Text), Rect. Top+2, Column-> Field-> Text); } else { DBGrid1-> Canvas-> TextOut (Rect.Left+2,Rect.Top+2,Column->Field->Text); } } }

The appropriate code for Delphi looks like:

procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (Table1.FieldByName (' VenueNo ').Value=1) then begin with DBGrid1.Canvas do begin Brush. Color: = clGreen; Font. Color: = clWhite; FillRect (Rect); if (Column. Alignment=taRightJustify) then TextOut (Rect. RIGHT-2 TextWidth (Column. Field. Text), Rect. Top+2, Column. Field. Text) else TextOut (Rect. Left+2, Rect. Top+2, Column. Field. Text); end; end; end;

In this case text alignment in columns coincides with alignment of columns. Let's mark that value of offset (in this case 2 пиксела), generally speaking, depends on typeface and the type size used in the given column, and should steal up individually. Fig. 3 Change of color taking into account text alignment in columns. If it is necessary to display in the non-standard image not all the line long but only some cells, it is necessary to analyze a name of the field displayed in the given column, as in the event handler resulted more low. The example for a C ++ Builder looks so:

void __ fastcall TForm1:: DBGrid1DrawColumnCell (TObject *Sender, const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State) { if ((Table1-> FieldByName ("VenueNo")-> Value == 1) AND (Column-> FieldName == "VenueNo")) { DBGrid1-> Canvas-> Brush-> Color=clGreen; DBGrid1-> Canvas-> Font-> Color=clWhite; DBGrid1-> Canvas-> FillRect (Rect); DBGrid1-> Canvas-> TextOut (Rect. RIGHT-2 DBGrid1-> Canvas-> TextWidth (Column-> Field-> Text), Rect. Top+2, Column-> Field-> Text); } }

The appropriate code for Delphi looks like:

procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (Table1.FieldByName (' VenueNo ').Value=1) and (Column. FieldName ='VenueNo ') then begin with DBGrid1.Canvas do begin Brush. Color: = clGreen; Font. Color: = clWhite; FillRect (Rect); TextOut (Rect. RIGHT-2 TextWidth (Column. Field. Text), Rect. Top+2, Column. Field. Text) end; end; end;

As a result selected there are only cells for which the conditions selected by us are satisfied.