Mac OS X Programming读书笔记4 - Windows

本文介绍了在MacOSX中进行窗口编程的方法,包括显示/隐藏窗口、绘制窗口内容及更新事件处理等关键技术,并展示了如何使用SetWindowProperty和GetWindowProperty关联窗口与数据。

Chapter 4 Windows

1 Window Update

1. 可以调用ShowWindowHideWindow来显示/隐藏窗口。

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相关联起来,所调用的函数为SetWindowPropertyGetWindowProperty

SetWindowProperty的原型如下:

OSStatus SetWindowProperty( WindowRef window,

PropertyCreator propertyCreator,

PropertyTag propertyTag,

UInt32 propertySize,

void * propertyBuffer );

1. PropertyCreate propertyCreatorApplicationSignature,四个Char组成。可以传0

2. PropertyTag propertyTag:四个CharProperty的标记

3. Uint32 propertySizeproperty的大小(字节数)

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

GetWindowProperty的原型如下:

OSStatus GetWindowProperty( WindowRef window,

PropertyCreator propertyCreator,

PropertyTag propertyTag,

UInt32 bufferSize,

UInt32 * actualSize,

void * propertyBuffer );

1. PropertyCreate propertyCreatorApplicationSignature,四个Char组成。可以传0

2. PropertyTag propertyTag:四个CharProperty的标记

3. Uint32 propertySizeproperty的大小(字节数)

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 );

如何将Structurewindow相关联呢?

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,这本书我越看下去越觉得烂。。。。)

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性适应性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值