📄 helloview.cpp
字号:
/*
* ============================================================================
* Name : CHelloView from CAknView
* Part of : Hello
* Copyright (c) 2003 Nokia. All rights reserved.
* ============================================================================
*/
// INCLUDE FILES
#include <aknviewappui.h>
#include <avkon.hrh>
#include <MyHello.rsg>
#include "MyHello.hrh"
#include "HelloView.h"
#include "HelloContainer.h"
#include "MyHelloAppUi.h"
#include "WorldView.h"
#include <myhello.mbg> //for mbm
#include <akncontext.h> //for CAknContextPane
#include <akntitle.h> //for CAknTitlePane
#include "MyHelloAppUi.h" //for CMyHelloAppUi
#include "SingleListView.h" //KViewId3
#include <aknnotewrappers.h>
#include <f32file.h> //for RFs
#include <s32file.h> //for RFileReadStream, RFileWriteStream
// ================= MEMBER FUNCTIONS =======================
// C++ default constructor can NOT contain any code, that
// might leave.
//
CHelloView* CHelloView::NewL()
{
CHelloView* self = NewLC();
CleanupStack::Pop(self);
return self;
}
CHelloView* CHelloView::NewLC()
{
CHelloView* self = new (ELeave) CHelloView;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CHelloView::CHelloView()
{
}
// EPOC default constructor can leave.
void CHelloView::ConstructL()
{
BaseConstructL(R_HELLO_VIEW);
}
// Destructor
CHelloView::~CHelloView()
{
}
// ---------------------------------------------------------
// TUid CHelloView::Id()
// Returns Id of the view.
// ---------------------------------------------------------
TUid CHelloView::Id() const
{
return KViewId1;
}
// ---------------------------------------------------------
// CHelloView::HandleCommandL(TInt aCommand)
// Handles commands
// ---------------------------------------------------------
void CHelloView::HandleCommandL(TInt aCommand)
{
switch(aCommand)
{
case EMyHelloCmdLogon:
{
AppUi()->ActivateLocalViewL(KViewId3);
//AppUi()->ActivateLocalViewL(KViewId2);
// TBuf<256> strName;
// iContainer->GetLoginName(strName);
//
// CAknInformationNote* information = new(ELeave) CAknInformationNote;
// information->ExecuteLD(strName);
//WriteFileToTxtL();
//ReadFileFromTxtL();
// WriteFileToStreamL();
// ReadFileFromStreamL();
break;
}
default:
{
AppUi()->HandleCommandL(aCommand);
break;
}
}
}
// ---------------------------------------------------------
// CHelloView::HandleClientRectChange()
// Handles client rect change.
// ---------------------------------------------------------
void CHelloView::HandleClientRectChange()
{
if (iContainer)
{
iContainer->SetRect(ClientRect());
}
}
// ---------------------------------------------------------
// CHelloView::DoActivateL(...)
// Creates the Container class object.
// ---------------------------------------------------------
void CHelloView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
TUid /*aCustomMessageId*/,
const TDesC8& /*aCustomMessage*/)
{
iContainer = new (ELeave) CHelloContainer;
iContainer->SetMopParent(this);
TRect rc;
rc.SetRect(ClientRect().iTl, TSize(176, 144));
iContainer->ConstructL(rc);
AppUi()->AddToStackL(*this, iContainer);
TBuf<KMaxPath> pathMbm;
#ifdef __WINS__
pathMbm.Copy(_L("z:\\system\\apps\\myhello\\myhello.mbm"));
#else
CMyHelloAppUi* pApp = (CMyHelloAppUi*)CEikonEnv::Static()->AppUi();
pApp->GetAppPath(pathMbm);
pathMbm.Append(_L("myhello.mbm"));
#endif
CEikStatusPane* statusPane = StatusPane();
//set icon
CAknContextPane* contextPane = (CAknContextPane*) statusPane->ControlL(TUid::Uid(EEikStatusPaneUidContext));
CFbsBitmap* bitmap = iEikonEnv->CreateBitmapL(pathMbm, EMbmMyhelloSnoopy_1_icon);
CFbsBitmap* bitmapMask = iEikonEnv->CreateBitmapL(pathMbm, EMbmMyhelloSnoopy_1_mask);
//contextPane->SetPicture(bitmap);
contextPane->SetPicture(bitmap, bitmapMask);
//set title
// CAknTitlePane* titlePane = (CAknTitlePane*) statusPane->ControlL(TUid::Uid(EEikStatusPaneUidTitle));
// titlePane->SetTextL(_L("Hello view!"));
//load chinese words
TBuf<32> sTmpTitle;
CEikonEnv::Static()->ReadResource(sTmpTitle, R_QTN_MH_HELLO_TITLE);
CAknTitlePane* titlePane = (CAknTitlePane*) statusPane->ControlL(TUid::Uid(EEikStatusPaneUidTitle));
titlePane->SetTextL(sTmpTitle);
//show
// CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
// informationNote->ExecuteLD(sTmpTitle);
}
// ---------------------------------------------------------
// CHelloView::DoDeactivate()
// Deletes the Container class object.
// ---------------------------------------------------------
void CHelloView::DoDeactivate()
{
if (iContainer)
{
AppUi()->RemoveFromStack(iContainer);
MEM_FREE(iContainer);
}
}
// ---------------------------------------------------------
// CHelloView::ReadFileByTxt()
// Read file.
// ---------------------------------------------------------
//读取普通文本文件内容
void CHelloView::ReadFileFromTxtL()
{
RFs& fs = CEikonEnv::Static()->FsSession();
RFile file;
TBuf<32> sFileName;
#ifdef __WINS__
sFileName.Copy(_L("c:\\test.txt"));//C:\Symbian\7.0s\Series60_v21_C\Epoc32\wins\c
#else
sFileName.Copy(_L("e:\\test.txt"));
#endif
TInt err = file.Open(fs, sFileName, EFileRead);
if (err != KErrNone)
{
return;
}
//获取文件当前长度
TInt nFileLen = 0;
file.Size(nFileLen);
//判断文件是否为空
if(nFileLen <= 0)
{
file.Close();
return;
}
//准备内存空间存放文件内容
HBufC8* pStr8Buf = HBufC8::NewL(nFileLen);
//读取文件内容
file.Read(pStr8Buf->Des());
//转换
HBufC* pStrBuf = HBufC::NewL(nFileLen);
pStrBuf->Des().Copy(pStr8Buf->Des());
delete pStr8Buf;
//显示读取的文件内容
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(pStrBuf->Des());
delete pStrBuf;
//关闭文件
file.Close();
}
//写普通文本文件
void CHelloView::WriteFileToTxtL()
{
//获取会话
RFs& fs = CEikonEnv::Static()->FsSession();
//创建会话
//User::LeaveIfError(fs.Connect());
RFile file;
TBuf<32> sFileName;
//设置文件路径和文件名
#ifdef __WINS__
sFileName.Copy(_L("c:\\test.txt"));
#else
sFileName.Copy(_L("e:\\test.txt"));
#endif
//以写文件的方式打开文件
TInt err = file.Open(fs, sFileName, EFileWrite);
//若文件打开失败则返回
if (err != KErrNone)
{
return;
}
//文件打开成功,则执行写文件的操作
//文件指针定位
//TInt pos = 0;
//file.Seek(ESeekEnd, pos);
//从Edit获取用户输入的数据,用于写入到文件中
TBuf<18> strName; //strName.Size();
iContainer->GetLoginName(strName);
//将用户输入的数据进行转换:16位转换成8位
TPtrC8 representation( (TUint8*)strName.Ptr(), strName.Size() );
//写入文件
file.Write( representation );
//清理文件缓冲
file.Flush();
strName.SetLength( 0 );
// file.Write(_L8("data of file !"));
// _LIT(message,"write ok !");
// CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
// informationNote->ExecuteLD(message);
//关闭文件
file.Close();
}
//读取流文件内容
void CHelloView::ReadFileFromStreamL()
{
//创建会话
RFs iFession;
User::LeaveIfError(iFession.Connect());
RFileReadStream readStream;
//以读取流文件的方式打开流文件
if( KErrNone != readStream.Open(iFession, _L("C:\\testStream.str"), EFileRead))
{
//若文件打开失败,则先关闭会话再返回
iFession.Close();
return;
}
//打开流文件成功
//压栈
readStream.PushL();
//读取流长度
TInt nLen = 0;
nLen = readStream.ReadInt32L();
//判断流内容长度
if(nLen > 0)
{
//若流数据内容长度大于零,则申请空间
HBufC* pStr = HBufC::NewL(nLen);
//读取流数据内容:内化
readStream >> pStr->Des();
//readStream.ReadL();
//显示读取到的内容
CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
informationNote->ExecuteLD(pStr->Des());
//清理空间
delete pStr;
}
//弹栈
readStream.Pop();
//释放
readStream.Release();
//关闭会话
iFession.Close();
}
//写数据到流文件
void CHelloView::WriteFileToStreamL()
{
//创建会话
RFs iFession;
User::LeaveIfError(iFession.Connect());
//删除文件
iFession.Delete(_L("testStream.str"));
//以写的方式创建流文件
RFileWriteStream writeStream;
User::LeaveIfError(writeStream.Create(iFession, _L("C:\\testStream.str"), EFileWrite));
//压栈
writeStream.PushL();
//从EDIT获取用户输入的数据
HBufC* pStr = HBufC::NewL(128); //pStr->Des().Copy(_L("Data stream of file !"));
TBuf<16> strName;
iContainer->GetLoginName(strName);
//将用户输入的数据存放到pStr中
TPtr pTprStr(pStr->Des());
pTprStr.Copy(strName);
//写数据长度到流文件
writeStream.WriteInt32L(pStr->Length());
//写数据到流文件:外化
writeStream << pStr->Des();
//writeStream.WriteL();
//清除/释放空间
delete pStr;
//提交
writeStream.CommitL();
//弹栈
writeStream.Pop();
//释放资源
writeStream.Release();
//关闭会话
iFession.Close();
// _LIT(message,"write ok !");
// CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
// informationNote->ExecuteLD(message);
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -