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

📄 ch04.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
  TShiftState Shift, int X, int Y)

{

  CheckState(Shift);

}

</FONT></PRE>
<P>The SETEXP program shows how 
you can read and manipulate sets. In particular,
you work with sets of type <TT>TShiftState</TT>. When you are finished studying the
program, you will have all the knowledge you need to work with BCB sets.</P>
<P>The main form for the SETEXP program 
consists of four checkboxes, two panels,
four labels, and three bitbtns, as shown in Figure 4.3. The four checkboxes are placed
on top of the first panel, and the fourth label is placed on the top of the second
panel.<BR>
<BR>
<A 
NAME="Heading14"></A><A HREF="04ebu03.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/04/04ebu03.jpg">FIGURE 4.3.</A><FONT COLOR="#000077">
</FONT><I>The SETEXP program's main form enables you to manipulate variables of type
<TT>TShiftState</TT>.</I></P>
<P>SETEXP tells you whether the Shift or Ctrl 
keys are pressed when the mouse is
clicked, and it tells you whether the user pressed the right or left mouse button.
The code also shows how to use the <TT>Intersection</TT>, <TT>Union</TT>, and <TT>Difference</TT>
operators.</P>
<P>The key method in 
the SETEXP program looks at a variable of type <TT>TShiftState</TT>
and displays its contents to the user through the program's radio buttons:</P>
<PRE><FONT COLOR="#0066FF">void TForm1::CheckState(TShiftState Shift)

{

  ShiftKey-&gt;Checked = 
Shift.Contains(ssShift);

  ControlKey-&gt;Checked = Shift.Contains(ssCtrl);

  LeftButton-&gt;Checked = Shift.Contains(ssLeft);

  RightButton-&gt;Checked = Shift.Contains(ssRight);

}

</FONT></PRE>
<P>This code takes advantage of the fact that 
<TT>Contains</TT> returns a Boolean
variable, and the <TT>Checked</TT> property of a radio button is also declared to
be of type Boolean. As a result, you can test to see whether a particular element
is part of the <TT>Shift</TT> set. If it is, you 
can easily set the <TT>Checked</TT>
state of a radio button to record the result. For example, in the preceding code,
if <TT>ssShift</TT> is part of the current set, the Shift Key radio button is checked.</P>
<P>Two different routines pass variables 
of type <TT>TShiftState</TT> to the <TT>CheckState</TT>
method. The first routine is called whenever the user clicks in the panel at the
bottom of the program or the label that rests on the panel:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall 
TForm1::Label4MouseDown(TObject *Sender, TMouseButton Button,

  TShiftState Shift, int X, int Y)

{

  CheckState(Shift);

}

</FONT></PRE>
<P>This code passes the <TT>Shift</TT> variable on to <TT>CheckState</TT>, which
displays the contents of the 
variable to the user. For instance, if the Shift key
is being held down and the right mouse button is pressed, <TT>Label4MouseDown</TT>
is called. <TT>Label4MouseDown</TT> then passes the <TT>Shift</TT> variable to <TT>CheckState</TT>,
and 
<TT>CheckState</TT> causes the <TT>ShiftKey</TT> and <TT>RightButton</TT> controls
to be checked. The other two radio buttons are left unchecked.</P>
<P>There are three bitbtns on the right side of the main form. They are labeled Union,
Intersection, 
and Difference. Clicking any of these buttons demonstrates one of the
non-Boolean set operators. All three buttons have their <TT>OnClick</TT> event set
to the following function:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall 
TForm1::UnionClick(TObject *Sender)

{

  AnsiString Operators[3] = {&quot;+&quot;, &quot;*&quot;, &quot;-&quot;};

  TShiftState FinalSet;

  TShiftState LeftShift;

  TShiftState LeftCtrl;

  LeftShift &lt;&lt; ssLeft &lt;&lt; ssShift;

  LeftCtrl 
&lt;&lt; ssLeft &lt;&lt; ssCtrl;



  switch (TOptType(dynamic_cast &lt;TBitBtn*&gt;(Sender)-&gt;Tag))

  {

    case otUnion:

      FinalSet = LeftShift + LeftCtrl;

      break;

    case otIntersection:

      FinalSet = LeftShift * LeftCtrl;

      
break;

    case otDifference:

      FinalSet = LeftShift - LeftCtrl;

      break;

  }

  CheckState(FinalSet);

  Label2-&gt;Caption = Operators[dynamic_cast&lt;TBitBtn *&gt;(Sender)-&gt;Tag];

}

</FONT></PRE>
<P>The <TT>UnionClick</TT> method 
declares three variables of type <TT>TShiftState</TT>.
Two of these variables are used to create sets that are used by the rest of the <TT>SetBtnClick</TT>
method:</P>
<PRE><FONT COLOR="#0066FF">LeftShift &lt;&lt; ssLeft &lt;&lt; ssShift;

LeftCtrl 
&lt;&lt; ssLeft &lt;&lt; ssCtrl;

</FONT></PRE>
<P>The first line assigns the <TT>LeftShift</TT> variable to a set that contains
the values <TT>ssLeft</TT> and <TT>ssShift</TT>. The next line assigns the <TT>LeftCtrl</TT>
variable to a set that 
contains <TT>ssLeft</TT> and <TT>ssCtrl</TT>. The rest of
this method enables the user to see the union, intersection, and difference of these
two sets.</P>
<P>The <TT>switch</TT> statement in the middle of the <TT>SetBtnClick</TT> method
detects 
which of the three bitbtns the user clicked. This is the old, time-honored
technique featuring the use of an enumerated type and the assignment of zero-based
ordinal values to the <TT>Tag</TT> field of each button.</P>
<P>If the user clicks the Union 
button, the <TT>FinalSet</TT> variable is set to
the union of the <TT>LeftShift</TT> and <TT>LeftCtrl</TT> variables:</P>
<PRE><FONT COLOR="#0066FF">FinalSet = LeftShift + LeftCtrl;

</FONT></PRE>
<P>A click on the Intersection button executes the 
following code:</P>
<PRE><FONT COLOR="#0066FF">FinalSet = LeftShift * LeftCtrl;

</FONT></PRE>
<P>The difference of the sets is calculated if the user clicks the Difference button:</P>
<PRE><FONT COLOR="#0066FF">FinalSet = LeftShift - LeftCtrl;


</FONT></PRE>
<P>After the <TT>switch</TT> statement ensures the selection of the proper operator,
the <TT>FinalSet</TT> value is passed to <TT>CheckState</TT> and its contents are
displayed to the user. For instance, if the user clicks the Union 
button, the LeftButton,
ShiftKey, and ControlKey radio buttons are all checked. The Intersection button causes
the LeftKey to be checked, and the Difference button causes the ShiftKey to be checked.
Here is another way of looking at the work 
accomplished by these operators:</P>
<PRE><FONT COLOR="#0066FF">[ssLeft, ssShift] + [ssLeft, ssCtrl] = [ssLeft, ssShift, ssCtrl];

[ssLeft, ssShift] * [ssLeft, ssCtrl] = [ssLeft]

[ssLeft, ssShift] - [ssLeft, ssCtrl] = [ssShift]

</FONT></PRE>
<P>To 
help the user understand exactly what is happening, the current set operation
is displayed at the top of the form. For instance, if the user clicks the Union button,
the following expression is shown to the user:</P>
<PRE><FONT 
COLOR="#0066FF">LeftShift + LeftCtrl

</FONT></PRE>
<P>Throughout a run of the program, the words <TT>LeftShift</TT> and <TT>LeftCtrl</TT>
are displayed to the user in a pair of <TT>TLabels</TT>. A third label displays <TT>+</TT>,
<TT>-</TT>, or 
<TT>*</TT>, depending on the current state of the program:</P>
<PRE><FONT COLOR="#0066FF">Label2-&gt;Caption = Operators[dynamic_cast&lt;TBitBtn *&gt;(Sender)-&gt;Tag];

</FONT></PRE>
<P>In this code, <TT>Operators</TT> is an array of three strings 
that contains the
operators that return a set:</P>
<PRE><FONT COLOR="#0066FF">AnsiString Operators[3] = {&quot;+&quot;, &quot;*&quot;, &quot;-&quot;};

</FONT></PRE>
<P>The SETEXP program gives you enough information that you should be able to work

with the sets that are passed to BCB event handlers. The code shown here defines
the way sets are usually handled in all BCB programs. However, you can actually directly
manipulate the raw data that represents a BCB set. Techniques for performing 
these
manipulations are shown in the <TT>GetShift</TT> method, which is part of the program
examined in the next section of this chapter. You can also review the information
on sets in Chapter 3.<BR>
<BR>
<A NAME="Heading15"></A><FONT 
COLOR="#000077"><B>Tracking the Mouse and Keyboard</B></FONT></P>
<P>You now know enough to begin an in-depth study of the main event handlers used
by BCB forms and controls. The EVENTS2 program, shown in Listings 4.3 through 4.5,
enables you to trace 
the occurrence of all the keyboard or mouse interrupts generated
during the run of a program.<BR>
<BR>
<A NAME="Heading16"></A><FONT COLOR="#000077"><B>Listing 4.3. The EVENTS2 header
and main form provide a detailed look at how to track 
events.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">//--------------------------------------------------------------------------

#ifndef MainH

#define MainH

//--------------------------------------------------------------------------

#include 
&lt;Classes.hpp&gt;

#include &lt;Controls.hpp&gt;

#include &lt;StdCtrls.hpp&gt;

#include &lt;Forms.hpp&gt;

#include &lt;ExtCtrls.hpp&gt;

//--------------------------------------------------------------------------

class TForm1 : public TForm

{


__published:

  TPanel *Panel1;

  TLabel *Label1;

  TLabel *LMouseMove;

  TLabel *Label3;

  TLabel *LMouseDown;

  TLabel *Label5;

  TLabel *LKeyDown;

  TLabel *Label7;

  TLabel *LKeyUp;

  TLabel *Label9;

  TLabel *LMouseUp;

  TLabel 
*Label11;

  TLabel *LWidth;

  TLabel *Label13;

  TLabel *LHeight;

  TLabel *LSpecialMouse;

  TLabel *Label16;

  void __fastcall FormResize(TObject *Sender);

  void __fastcall FormPaint(TObject *Sender);

  void __fastcall FormMouseUp(TObject 
*Sender, TMouseButton Button,

  	TShiftState Shift, int X, int Y);

  void __fastcall FormKeyUp(TObject *Sender, WORD &amp;Key, TShiftState Shift);

  void __fastcall FormKeyDown(TObject *Sender, WORD &amp;Key, TShiftState Shift);

  void __fastcall 
FormMouseMove(TObject *Sender, TShiftState Shift, int X,

  int Y);

  void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,

  	TShiftState Shift, int X, int Y);



private:

MESSAGE void MyMouseMove(TWMMouse &amp;Message);

public:

  
virtual __fastcall TForm1(TComponent* Owner);

BEGIN_MESSAGE_MAP

  MESSAGE_HANDLER(WM_MOUSEMOVE, TWMMouse, MyMouseMove);

END_MESSAGE_MAP(TForm);

};

//--------------------------------------------------------------------------

extern TForm1 *Form1;


//--------------------------------------------------------------------------

#endif

</FONT></PRE>
<P><A NAME="Heading17"></A><FONT COLOR="#000077"><B>Listing 4.4. The EVENTS2 main
form provides a detailed look at how to track events.</B></FONT></P>

<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// Copyright (c) 1997 by Charlie Calvert

//

#include &lt;vcl.h&gt;

#pragma hdrstop

#include &quot;Main.h&quot;

#include &quot;VKeys1.h&quot;

#include &quot;Binary.h&quot;


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

TForm1 *Form1;

__fastcall TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

{

}

void TForm1::MyMouseMove(TWMMouse &amp;Message)

{

  TForm::Dispatch(&amp;Message);

  LSpecialMouse-&gt;Caption = &quot;X 
&quot; + IntToStr(Message.XPos) +

                           &quot; Y &quot; + IntToStr(Message.YPos);

}

void __fastcall TForm1::FormResize(TObject *Sender)

{

  LHeight-&gt;Caption = IntToStr(Width);

  LWidth-&gt;Caption = IntToStr(Height);

}


void __fastcall TForm1::FormPaint(TObject *Sender)

{

  Canvas-&gt;Font-&gt;Name = &quot;New Times Roman&quot;;

  Canvas-&gt;Font-&gt;Size = 48;

  Canvas-&gt;TextOut(1, Panel1-&gt;Height, &quot;Mouse Zone&quot;);

}

void __fastcall 
TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,

⌨️ 快捷键说明

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