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

📄 descriptorfn.cpp

📁 symbian OS C++ S60 的 描述符(descriptor)的一个例程
💻 CPP
字号:
#include <e32base.h>
#include <e32cons.h>

// 定义本地数据
LOCAL_D CConsoleBase* console;
LOCAL_C void NewCsoLC();
LOCAL_D TInt err;
LOCAL_C void out(TPtrC des);
LOCAL_C TInt get();


//全局函数,被 E32 调用的主函数,
GLDEF_C TInt E32Main()  
    {
	__UHEAP_MARK;
	CTrapCleanup* cup=CTrapCleanup::New();
	TRAP(err,NewCsoLC());
	delete cup;
	__UHEAP_MARKEND;
	return 0; 
    }


LOCAL_C void NewCsoLC()
{
	__UHEAP_MARK;
	
	_LIT(TITLE,"RHandleBase Demo");
	TSize Ts=TSize();
	Ts.SetSize(-1,-1);
	console=Console::NewL(TITLE,Ts);
	CleanupStack::PushL(console);
	TInt key=0;

	while(true)
	{
		key=get();
		console->Printf(_L("[%C:%D]\n"),key,key);
		
	

		if(key==27)
		{
			User::Panic(_L("Exit"),100);
		}
		//press c/C
		if(key==99)
		{
			console->ClearScreen();	
		}

		// TBufC 和 TBuf<n> 
		if(key==49)
		{			 
			 //创建  
			 _LIT(DES,"我是啊牛!");
			 TBufC<10> des1(DES);
			 out(des1);
			 TBuf<10> des2(DES);
			 out(des2);
			 // 打印数据成员
			 console->Printf(_L("len=%d\n"),des2.Length());
			 //左取两个字符
			 out(des2.Left(2));
		}
		
		
		// TPtrC 
		if(key==50)
		{
			 
			 //创建  
			 _LIT(DES,"我是啊牛他爸爸!");
			 TBufC<10> des1(DES);
			  
			 TBuf<10> des2(DES);
			  
			 //获得 TPtrC的两种方法。
			 TPtrC des3;
			 des3.Set(des1);
			 out(des3);

			 TPtrC des4(des2);
			 out(des4);

		}

		// 
		if(key==51)
		{			 
			 //创建  
			 _LIT(DES1,"我是啊牛!");

			 _LIT(DES2,"我是啊猪他妈妈!");

			 TBufC<10> des1(DES1);
			 out(des1);
			 des1=DES2;
			 out(des1);
			 des1=DES1;
			 out(des1);
			
		}

		if(key==52)
		{			 
			 //创建  
			 _LIT(DES1,"我是啊牛");

			 _LIT(DES2,"我是啊猪他妈妈!");

			 TBufC<10> des1(DES1);
			 TPtrC des2;
			 des2.Set(DES2);

			 TPtr des3=des1.Des();
			 //追加
			 des3.Append(_L("牛!"));
			 out(des3);
			
			 TBuf<20> des4(des2);

			 des4.Replace(5,2,_L("爸爸的妈妈"));
			 out(des4);

			
		}

		if(key==53)
		{

			// 方法1:使用 New(), NewL(), or NewLC() 
			// 当前Buf的大小为零
			HBufC * Buf = HBufC::NewL(15);
			console->Printf(_L("Size=%d;len=%d\n"),Buf->Size(),Buf->Length());
			//通过赋值改变描述符内容
			*Buf=_L("阿牛吃小草。");
		
			out(*Buf);
			console->Printf(_L("Size=%d;len=%d\n"),Buf->Size(),Buf->Length());

			
			// 方法2:使用已经存在的描述符的 Alloc(), AllocL(), 或 AllocLC() 方法
			// 这些方法都返回以现有描述符内容初始化的HBufC指针。
			_LIT (KText , "阿牛拉猪屎。");

			TBufC<50> CBuf = KText;
			HBufC * Buf1 = CBuf.AllocL();
			out(*Buf1);
			console->Printf(_L("Size=%d;len=%d\n"),Buf1->Size(),Buf1->Length());

 
 
			// 改变 HBufC 所指向的内容 
			_LIT ( KText1 , "啊牛生小牛!");
			*Buf1 = KText1;
			out(*Buf1);
			console->Printf(_L("Size=%d;len=%d\n"),Buf1->Size(),Buf1->Length());
 
			// 把HBufC转为TPtr型,注意:这个时候,描述符的大小已经发生了改变。
			TPtr Pointer = Buf1->Des();
			// 进行删除操作
			Pointer.Delete(3,1);

			out(*Buf1);
			console->Printf(_L("Size=%d;len=%d;max=%d\n"),Buf1->Size(),Buf1->Length(),Pointer.MaxLength());
 
			//追加
			//_LIT ( KNew, "小牛生啊牛。");
			_LIT ( KNew, "爽");
			 
			Pointer.Append( KNew );
			out(*Buf1);
			console->Printf(_L("Size=%d;len=%d;max=%d\n"),Buf1->Size(),Buf1->Length(),Pointer.MaxLength());
		
		}



	}
	
	



	
	console->Printf(_L("Press to end\n"));
	console->Getch();
	CleanupStack::PopAndDestroy();
	__UHEAP_MARKEND;
}

LOCAL_C void out(TPtrC des)
{

	console->Printf(des);
	console->Printf(_L("\n"));
}

LOCAL_C TInt get()
{
	return console->Getch();
}

⌨️ 快捷键说明

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