|
Chapter 5 Controls
1 Command Signatures and Control IDs
1. 可以将Command和Control联系起来,但是当你收到事件的时候,你并不知道当时是哪个Control被按下了。事实上,你也不需要关心,因为Command本来被设计成代表命令,至于是点击Button还是选择菜单都没有关系
2. 当你需要知道Control究竟发生了什么事情的时候,你需要给Control分配一个Control ID,包括:
a. Signature:一个4个Char的标记,一般来说应该是程序的Creator Code,也就是Application本身的Signature。大部分情况下,单一程序的所有Control ID的Signature都是一样的
b. ID:一个整数,真正的独立在此程序中标记该Control的ID
2 Buttons
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="Picture_x0020_4" style="VISIBILITY: visible; WIDTH: 134.25pt; HEIGHT: 81pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1028"><imagedata o:title="" src="file:///D:%5Ctmp%5Cmsohtmlclip1%5C01%5Cclip_image001.emz"></imagedata></shape>
1. Bevel Button
2. ImageWell
3 Radio Buttons
<shape id="Picture_x0020_5" style="VISIBILITY: visible; WIDTH: 134.25pt; HEIGHT: 56.25pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1027"><imagedata o:title="" src="file:///D:%5Ctmp%5Cmsohtmlclip1%5C01%5Cclip_image002.emz"></imagedata></shape>
系统负责维护Radio Button的状态,不需要自己编程处理。
1: ControlHandle numBeepsRadioButtonGroup;
2: ControlID numBeepsControlID = { kControlSignature, kRadioGroupControlID };
3: GetControlByID( window, &numBeepsControlID,
&numBeepsRadioButtonGroup );
SInt32 numBeepsValue;
4: numBeepsValue = GetControl32BitValue( numBeepsRadioButtonGroup );
| 1. 声明一个Control的Handle
2. 声明Control的Control ID,也就是这个Radio Button Group的ID
3. 通过Control ID获得Control的Handle
4. 通过Handle获得Control的32-bit整数值,表示哪一个Radio Button被按下了
4 Checkboxes
<shape id="Picture_x0020_3" style="VISIBILITY: visible; WIDTH: 134.25pt; HEIGHT: 51.75pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1026"><imagedata o:title="" src="file:///D:%5Ctmp%5Cmsohtmlclip1%5C01%5Cclip_image003.emz"></imagedata></shape>
和上面的非常类似,GetControl32BitValue返回0说明没有Check,1则为Checked
5 Text Input Fields
<shape id="Picture_x0020_2" style="VISIBILITY: visible; WIDTH: 179.25pt; HEIGHT: 123pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1025"><imagedata o:title="" src="file:///D:%5Ctmp%5Cmsohtmlclip1%5C01%5Cclip_image004.emz"></imagedata></shape>
应该使用GetcontrolData来获得Text Input Fields所对应的字符串:
OSErr GetControlData(
ControlRef inControl,
ControlPartCode inPart,
ResType inTagName,
Size inBufferSize,
void * inBuffer,
Size * outActualSize );
| 1. ControlRef inControl:ControlRef也就是ControlHandle,所以直接传递用GetControlByID的返回值即可
2. ControlPartCode inPart:指定访问Control的哪一部分,部分Control由多个Control组成,由不同的常量代表不同的子控件。如果没有子控件,可以传递kControlEntireControl代表整个控件本身
3. ResType inTagName:数据的类型,对于Text Input Field应该传递kControlEditTextCFStringTag
4. Size inBufferSize:缓冲区大小
5. Void *inBuffer:缓冲区本身
6. Size *outActualSize:实际大小
举例如下:
CFStringRef theString;
GetControlData(
stringInTextEdit,
kControlEntireControl,
kControlEditTextCFStringTag,
sizeof( CFStringRef ),
&theString,
NULL );
|
同样的,可以用SetControlData来设置Text Input Field的字符串:
OSErr SetControlData(
ControlRef inControl,
ControlPartCode inPart,
ResType inTagName,
Size inSize,
void * inData);
| |
|
|