⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>If you are concerned that the solution
	to the problem in the last 
paragraph requires using three <TT>TTable</TT> objects
	instead of only two, I would ask you to recall that the goal of this book is to show
	how to get things done, not how to do things in the smallest possible space. If you
	can get things done in 
five minutes through a technique that you know is bug free,
	that is something you should give up only reluctantly, particularly if the alternative
	is working for days or weeks to implement a solution that is likely to have bugs
	that will take 
another week or two to squash. At the very least, you should implement
	the quick solution for the first draft of your application, and then come back and
	look for optimizations once you have a working version of the product. <BR>
	<BR>
	Don't ever 
try to optimize during your first draft of an application! There is no
	such thing as getting things right the first time in programming. Instead, you should
	implement a reasonable solution, critique it, come back and make improvements, critique
	the 
improvements, make another pass over the application, and so on. This kind of
	cycle demands that you not get too hung up on optimizations during early drafts,
	because you are likely to find that any one part of the application will change in
	future 
revisions. If you have the thing implemented correctly, and there is still
	time left in the project cycle, then you can come back and seek to optimize the code!
	<BR>
	<BR>
	The final kicker in this analysis is that contemporary application 
programmers rarely
	have time to optimize. Given the success rate of most projects, your customers or
	managers will usually be ecstatic if you just turn in a working solution to the problem
	on time. If you release the same application 10 percent 
faster and 20 percent smaller,
	but six months later, it's unlikely you will win quite the same number of kudos you
	think you deserve. I usually leave the minute optimizations up to the development
	teams at Borland. They know how to reach into the 
fire without getting burned. 
<HR>


</BLOCKQUOTE>

<P>If you want to change the author associated with a particular record, you just
click a new item in the list box. The author number will be changed automatically
for you by the lookup. It's all 
very simple and intuitive when viewed from the user's
perspective.
<H3><A NAME="Heading26"></A><FONT COLOR="#000077">TDBGrid at Runtime</FONT></H3>
<P><TT>TDBGrid</TT> objects can be completely reconfigured at runtime. You can hide
and show columns, 
change the order of columns, the color of columns, the color of
rows, the color of fields, and the width of columns.</P>
<P>The GridTricks program, shown in Figure 11.9, demonstrates how to take a <TT>TDBGrid</TT>
through its paces at runtime. The 
program is fairly straightforward except for two
brief passages. The first passage involves creating checkbox controls on-the-fly,
and the second shows how to change the traits of columns.<BR>
<BR>
<A NAME="Heading27"></A><A 
HREF="11ebu09.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/11/11ebu09.jpg">FIGURE 11.9.</A><FONT COLOR="#000077">
</FONT><I>The main GridTricks program enables you to change the appearance of a grid
at runtime. <BR>
</I><BR>
You need a color monitor to really see what is happening.</P>
<P>When the 
user wants to decide which fields are visible, GridTricks pops up a second
form and displays the names of all the fields from the <TT>ORDERS</TT> table in a
series of checkboxes. The user can then select the fields that he or she wants to
make 
visible. The selected checkboxes designate fields that are visible, whereas
the nonselected ones represent invisible fields. The program also enables you to
set the order and width of fields, as well as to hide and show the titles at the
top of the 
grid. (See Listings 11.1 and 11.2.) The code for the GridTricks program
is in the <TT>CHAP17</TT> directory on this book's CD-ROM. (See Listings 11.4-11.8.)<BR>
<BR>
<A NAME="Heading28"></A><FONT COLOR="#000077"><B>Listing 11.4. The main unit for
the 
GridTricks Program.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// File: Main.cpp

// Project: GridTricks

// Copyright (c) 1997 by Charlie Calvert

#include &lt;vcl\vcl.h&gt;

#pragma hdrstop

#include 
&quot;Main.h&quot;

#include &quot;DMod1.h&quot;

#include &quot;NamesDlg.h&quot;

#include &quot;ColumnEditor1.h&quot;

#include &quot;ShowOptions1.h&quot;

#define NEWCOLOR clGreen

#pragma resource &quot;*.dfm&quot;

TForm1 *Form1;

__fastcall 
TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

{

}

void __fastcall TForm1::Exit1Click(TObject *Sender)

{

  Close();

}

void __fastcall TForm1::FieldNames1Click(TObject *Sender)

{

  NamesDialog-&gt;ShowNames(fdFieldNames);

}

void 
__fastcall TForm1::FieldObjectNames1Click(TObject *Sender)

{

  NamesDialog-&gt;ShowNames(fdObjectNames);

}

///////////////////////////////////////

// DBGrid1DrawColumnCell

// Paints ROW different color depending on value of ItemsTotal field


///////////////////////////////////////

void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,

      const TRect &amp;Rect, Integer DataCol, TColumn *Column,

      TGridDrawState State)

{

   if (ColorRows1-&gt;Checked)

   {

     if 
(DMod-&gt;OrdersTableItemsTotal-&gt;Value &lt; 1000)

       DBGrid1-&gt;Canvas-&gt;Font-&gt;Color = clRed;

     else if (DMod-&gt;OrdersTableItemsTotal-&gt;Value &lt; 10000)

       DBGrid1-&gt;Canvas-&gt;Font-&gt;Color = clBlue;

     else

       
DBGrid1-&gt;Canvas-&gt;Font-&gt;Color = clGreen;

   }

   DBGrid1-&gt;DefaultDrawColumnCell(Rect, DataCol, Column, State);

}

void TForm1::ColorTitles(BOOL UseDefaultColor)

{

  TColor Colors[] = {clRed, clBlue, clGreen, clLime, clWhite, 
clFuchsia};

  int i;

  for (i = 0; i &lt; DBGrid1-&gt;Columns-&gt;Count; i++)

  {

    TColumn *Column = DBGrid1-&gt;Columns-&gt;Items[i];

    TColumnTitle *ColumnTitle = Column-&gt;Title;

    if (UseDefaultColor)

      
ColumnTitle-&gt;Font-&gt;Color = FDefaultColor;

    else

      ColumnTitle-&gt;Font-&gt;Color = Colors[random(7)];

  }

}

void __fastcall TForm1::AnimateTitles1Click(TObject *Sender)

{

  Timer1-&gt;Enabled = (!Timer1-&gt;Enabled);

  
AnimateTitles1-&gt;Checked = Timer1-&gt;Enabled;

  if (!AnimateTitles1-&gt;Checked)

    ColorTitles(True);

}

void __fastcall TForm1::ColorRows1Click(TObject *Sender)

{

  ColorRows1-&gt;Checked = (!ColorRows1-&gt;Checked);

  
DBGrid1-&gt;Repaint();

}

void __fastcall TForm1::MarkColumnClick(TObject *Sender)

{

  MarkColumn-&gt;Checked = (!MarkColumn-&gt;Checked);

  TColumn *Column = DBGrid1-&gt;Columns-&gt;Items[DBGrid1-&gt;SelectedIndex];

  if (MarkColumn-&gt;Checked)

  
{

    Column-&gt;Font-&gt;Color = NEWCOLOR;

    Column-&gt;Font-&gt;Style = TFontStyles() &lt;&lt; fsBold;

  }

  else

  {

    Column-&gt;Font-&gt;Color = FDefaultColor;

    Column-&gt;Font-&gt;Style = TFontStyles();

  }

  HandleCaption();

}


///////////////////////////////////////

// Handle Caption

///////////////////////////////////////

void TForm1::HandleCaption()

{

  TColumn *Column = DBGrid1-&gt;Columns-&gt;Items[DBGrid1-&gt;SelectedIndex];



  AnsiString 
S(DBGrid1-&gt;SelectedIndex);

  Caption = S;

  if (Column-&gt;Font-&gt;Color == FDefaultColor)

    Caption = &quot;Column &quot; + S + &quot; is Default&quot;;

  else

    Caption = &quot;Column &quot; + S + &quot; is Marked&quot;;

}

void 
__fastcall TForm1::DBGrid1ColEnter(TObject *Sender)

{

  TColumn *Column = DBGrid1-&gt;Columns-&gt;Items[DBGrid1-&gt;SelectedIndex];

  MarkColumn-&gt;Checked = (Column-&gt;Font-&gt;Color == NEWCOLOR);

  HandleCaption();

}

void __fastcall 
TForm1::FormCreate(TObject *Sender)

{

  FDefaultColor = DBGrid1-&gt;Font-&gt;Color;

  HandleCaption();

}

void __fastcall TForm1::Timer1Timer(TObject *Sender)

{

  ColorTitles(False);

}

void __fastcall TForm1::ShowFieldEditor1Click(TObject 
*Sender)

{

  ColumnEditor-&gt;ShowColumns();

}

void __fastcall TForm1::ToggleTitles1Click(TObject *Sender)

{

  if (DBGrid1-&gt;Options.Contains(dgTitles))

    DBGrid1-&gt;Options = TDBGridOptions(DBGrid1-&gt;Options) &gt;&gt; dgTitles;

  else

    
DBGrid1-&gt;Options = TDBGridOptions(DBGrid1-&gt;Options) &lt;&lt; dgTitles;

}

void __fastcall TForm1::ToggleIndicator1Click(TObject *Sender)

{

  if (DBGrid1-&gt;Options.Contains(dgIndicator))

    DBGrid1-&gt;Options = 
TDBGridOptions(DBGrid1-&gt;Options) &gt;&gt; dgIndicator;

  else

    DBGrid1-&gt;Options = TDBGridOptions(DBGrid1-&gt;Options) &lt;&lt; dgIndicator;

}

void __fastcall TForm1::ShowTitlesIndicator1Click(TObject *Sender)

{

  
ShowTitlesIndicator1-&gt;Checked =

    !(TDBGridOptions(DBGrid1-&gt;Options).Contains(dgIndicator) &amp;&amp;

    TDBGridOptions(DBGrid1-&gt;Options).Contains(dgTitles));

  if (ShowTitlesIndicator1-&gt;Checked)

    DBGrid1-&gt;Options = 
TDBGridOptions(DBGrid1-&gt;Options) &lt;&lt; dgIndicator &lt;&lt; dgTitles;

  else

    DBGrid1-&gt;Options = TDBGridOptions(DBGrid1-&gt;Options) &gt;&gt; dgIndicator &gt;&gt; dgTitles;

  DBGrid1-&gt;Refresh();

}

void __fastcall 
TForm1::ColLines1Click(TObject *Sender)

{

  if (DBGrid1-&gt;Options.Contains(dgColLines))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -