设为首页 收藏本站
查看: 912|回复: 0

[经验分享] Mac OS X Programming读书笔记4

[复制链接]

尚未签到

发表于 2016-5-16 09:49:25 | 显示全部楼层 |阅读模式
Chapter 4 Windows  

1 Window Update  

1. 可以调用ShowWindow和HideWindow来显示/隐藏窗口。  

2. DrawString作用是在当前Graphics Pen位置显示字符串  

3. MoveTo移动当前Graphics Pen  

Window绘图的时候发送Update事件:  

EventTypeSpec windowEvent = { kEventClassWindow,  

kEventWindowDrawContent };  

  

下面代码安装一个Update事件处理函数:  

EventTargetRef target;  

  

EventHandlerUPP handlerUPP;  

EventTypeSpec windowEvent = { kEventClassWindow,  

kEventWindowDrawContent };  

  

target = GetWindowEventTarget( window );  

handlerUPP = NewEventHandlerUPP( WindowEventHandler );  

InstallEventHandler( target, handlerUPP, 1, &windowEvent,  

(void *)window, NULL );  

  

Update事件处理函数可以这么写:  

pascal OSStatus WindowEventHandler( EventHandlerCallRef handlerRef,  

EventRef event, void *userData)  

{  

OSStatus result = eventNotHandledErr;  

UInt32 eventKind;  

WindowRef window;  

window = ( WindowRef )userData;  

eventKind = GetEventKind( event );  

if ( eventKind == kEventWindowDrawContent )  

{  

UpdateWindow( window );  

}  

return result;  

}  

  

在UpdateWindow中必须首先调用SetPortWindowPort用来指定当前绘图所用的Port(这名字还真怪)。Port是用来定义一个绘图环境的(类似Windows中的设备上下文DC)。每个窗口都有一个Port。屏幕也被认为是一个Port。如果不调用SetPortWindowPort,程序的行为将无法预测。  

void UpdateWindow( WindowRef window )  

{  

SetPortWindowPort( window );  

// code to draw the contents of the window goes here  

}  

  

一个完整的UpdateWindow示例如下:  

void UpdateWindow( WindowRef window )  

{  

FMFontFamily fontFamily;  

  

SetPortWindowPort( window );  

  

fontFamily = FMGetFontFamilyFromName( "\pTimes" );  

TextFont( fontFamily );  

TextFace( bold + italic );  

TextSize( 24 );  

MoveTo( 30, 60 );  

DrawString( "\pThis is drawn from code!" );  

}  

  

\p表示字符串为一个Pascal String。  

2 Associating Information with Windows  

在Mac OS X中可以把数据和Window相关联起来,所调用的函数为SetWindowProperty和GetWindowProperty。  

SetWindowProperty的原型如下:  

OSStatus SetWindowProperty( WindowRef window,  

PropertyCreator propertyCreator,  

PropertyTag propertyTag,  

UInt32 propertySize,  

void * propertyBuffer );  

1. PropertyCreate propertyCreator:Application的Signature,四个Char组成。可以传0  

2. PropertyTag propertyTag:四个Char的Property的标记  

3. Uint32 propertySize:property的大小(字节数)  

4. Void *propertyBuffer:指向实际数据  

GetWindowProperty的原型如下:  

OSStatus GetWindowProperty( WindowRef window,  

PropertyCreator propertyCreator,  

PropertyTag propertyTag,  

UInt32 bufferSize,  

UInt32 * actualSize,  

void * propertyBuffer );  

1. PropertyCreate propertyCreator:Application的Signature,四个Char组成。可以传0  

2. PropertyTag propertyTag:四个Char的Property的标记  

3. Uint32 propertySize:property的大小(字节数)  

4. Uint32 *actualSize:实际大小,可传NULL  

5. Void *propertyBuffer:指向实际数据  

举例如下:  

UInt32 windowNumber = 99;  

WindowRef window;  

UInt32 theNumber;  

...  

err = CreateWindowFromNib( nibRef, CFSTR("MainWindow"), &window );  

...  

// associate the data (99) in variable windowNumber with a window:  

SetWindowProperty( window, 0, 'test', sizeof( UInt32 ),  

&windowNumber );  

...  

// retrieve the data (99) from the window and store it in theNumber:  

GetWindowProperty( window, 0, 'test', sizeof( UInt32 ),  

NULL, &theNumber );  

  

如何将Structure和window相关联呢?  

1. 定义下面这个Structure  

typedef struct  

{  

UInt32 number;  

Str255 string;  

} WindowData, **WindowDataHandle;  

  

2. 创建一个Handle,相当于分配一块内存,大小为sizeof(WindowData)  

WindowDataHandle windDataHndl;  

windDataHndl = ( WindowDataHandle )NewHandle( sizeof( WindowData ) );  

  

3. 给Handle所指向的数据赋值。注意WindowDataHandle是指针的指针(为什么是指针的指针呢?因为NewHandle创建的是一块可重定位的内存)  

UInt32 theNumber = 5;  

(**windDataHndl).number = theNumber;  

  

Str255 theString = "\pCopyright (c) 2001"  

numBytes = theString[0] + 1;  

BlockMoveData( theString, (**windDataHndl).string, numBytes );  

  

Str255本质上其实是一个数组,最多可以Hold255个字符。第一个元素是String大小(Pascal String正是这样),所以theString[0] + 1可以获得实际大小  

4. 之后调用SetWindowProperty(我觉得这里似乎错了,应该是sizeof(WindowDataHandle),而不是sizeof(WindowData),待验证)  

SetWindowProperty(window, 0, 'test', sizeof(WindowData),  

&windDataHndl);  

  

5. 如果要获得Property,可以调用GetWindowProperty(我觉得这里似乎错了,应该是sizeof(WindowDataHandle),而不是sizeof(WindowData),待验证)  

GetWindowProperty( window, 0, 'test', sizeof( WindowData ),  

NULL, &windDataHndl );  

  

(BTW,这本书我越看下去越觉得烂。。。。)  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-217599-1-1.html 上篇帖子: 如何在mac OS X下安装Mongodb 下篇帖子: Mac OS X Programming读书笔记3
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表