徹底解決Symbian開發全螢幕顯示問題

來源:互聯網
上載者:User
首先,我們可以利用Carbide.vs嚮導建一個項目,名字就叫"TestScreen",選擇基於Eikon的傳統控制項架構。

  那麼在CTestScreenAppUi的二階建構函式裡就有如下代碼:

  void CTestScreenAppUi::ConstructL()

  {

  BaseConstructL();

  iAppContainer = new (ELeave) CTestScreenContainer;

  iAppContainer->SetMopParent( this );

  iAppContainer->ConstructL( ClientRect() );

  AddToStackL(iAppContainer);

  }

  這裡面有很關鍵的一句,就是我用紅色顯示的那段代碼。它把當前UI的ClientRect()傳遞給Container類,我們都知道Container類是控制項類,負責整個程式的介面顯示,那麼UI傳遞給Container的這個ClientRect()到底是什麼東東呢?我們看看SDK HELP:

  ClientRect()

  TRect ClientRect() const;

  Description

  Gets the area of the screen available to the application for drawing, not including the space that is available for any of the following, where required: non-application areas which should always be displayed, an application status pane, an application button group, an application menu bar, an application title band and an application tool bar.

  Importantly, the co-ordinates of the rectangle are relative to the whole screen area so, for example, the co-ordinate for the top, left point of the area available for drawing may be (0, 45).

  Return value

  TRect The area of the screen available to the application for drawing.

  從Description我們可以看到:ClientRect()獲得應用程式繪製的有效螢幕地區,但是這個地區不包括那些總是顯示的非應用程式地區,比如:應用程式狀態面板(application status pane)、按鈕(button group)、應用程式的菜單bar、標題、工具條。

  而且更重要的是從下面一行可以看出,這個ClientRect()所獲得地區的top-left座標是(0,45)。

  通過上面的分析我們知道,UI在構造我們的Container時傳遞一個所謂的"客戶矩形地區",這個"客戶矩形地區"的top-left座標是(0,45),從而也就知道如果要讓我們的程式全螢幕顯示,那麼我們需要改變的是構造Container的時候傳遞的矩形大小。

  那麼就有如下幾種方法:

  ①如果我們知道螢幕尺寸,那麼就可以把iAppContainer->ConstructL( );裡面的參數改為TRect (0,0,176,208)。

  ②上面的程式不具有適配性,因為我們把螢幕的寬度和高度寫死了。

  我們來看Symbian給我們提供的一個方法

  ApplicationRect()

  TRect ApplicationRect() const;

  Description

  Gets the total area of the screen available to the application. This includes the space that is available for a toolbar, toolband or title band, if the application requires them.

  Return value

  TRect The total area of the screen available to the application.

  Description寫的很明顯了,我就不翻譯了。這個方法可以獲得螢幕的整個尺寸,我們把程式可以改為:

  iAppContainer->ConstructL( ApplicationRect() );從而實現程式的全螢幕顯示。

  ③第三中方法是最笨的方法了,那就是不改變UI所傳遞的"客戶矩形地區"的大小,傳遞的仍然是ClientRect()。但是到了Container後再採用"亡羊補牢"的做法!把status pane、menu bar等隱藏起來。

  而且這種方法也容易出錯誤,下面是一個同行犯的錯誤,他在Container類裡寫入下面代碼:

  void CTestScreenContainer::ConstructL(const TRect& aRect)

  {

  CreateWindowL();

  iLabel = new (ELeave) CEikLabel;

  iLabel->SetContainerWindowL( *this );

  iLabel->SetTextL( _L("Example View") );

  iToDoLabel = new (ELeave) CEikLabel;

  iToDoLabel->SetContainerWindowL( *this );

  iToDoLabel->SetTextL( _L("Add Your controls/n here") );

  SetRect(aRect);

  CEikStatusPane* statusp = iEikonEnv->AppUiFactory()->StatusPane();

  if(statusp)

  statusp->MakeVisible(EFalse);

  iEikonEnv->AppUiFactory()->Cba()->MakeVisible(EFalse);

  ActivateL();

  }

  為了使用CEikStatusPane類要加入標頭檔#include

  為了使用CEikButtonGroupContainer類要加入標頭檔#include

  其中iEikonEnv->AppUiFactory()是在Symbian中擷取UI執行個體常用的方法,這和MFC是一樣,你千萬不能new一個CTestScreenAppUi出來,因為他們是由架構調用的,我們並不知道何時調用。

  但是因為他是在Container類裡調用這兩個方法,也就是說ClientRect()擷取"矩形地區"之後程式才設定status pane、Cba為不可見!所以當然也沒什麼用,程式仍然無法全螢幕顯示。

  所以說即使你在UI類裡寫下面的代碼,但因為代碼是在擷取"矩形地區"之後才設定status pane、Cba為不可見,程式仍然無法全螢幕顯示!

  void CTestScreenAppUi::ConstructL()

  {

  BaseConstructL();

  iAppContainer = new (ELeave) CTestScreenContainer;

  iAppContainer->SetMopParent( this );

  iAppContainer->ConstructL( ClientRect() );

  //在擷取"矩形地區"後設定status pane、Cba為不見

  CEikStatusPane* statusp = StatusPane();

  if(statusp)

  statusp->MakeVisible(EFalse);

  Cba()->MakeVisible(EFalse);

  AddToStackL( iAppContainer );

  }

  所以千萬記住:如果要通過設定status pane、Cba為不可見的方法獲得全屏,千萬要在擷取"矩形地區"之前設定!

  ④上面集中方法都是通過在UI類設定"矩形地區"的大小,或者通過設定status pane、Cba不可見隱式改變"矩形地區"的大小實現全屏的。

  這裡我們介紹一種在Container類裡,在UI設定完"矩形地區"後再改變螢幕為全螢幕顯示的方法。void CTestScreenContainer::ConstructL(const TRect& aRect)

  {

  CreateWindowL();

  iLabel = new (ELeave) CEikLabel;

  iLabel->SetContainerWindowL( *this );

  iLabel->SetTextL( _L("Example View") );

  iToDoLabel = new (ELeave) CEikLabel;

  iToDoLabel->SetContainerWindowL( *this );

  iToDoLabel->SetTextL( _L("Add Your controls/n here") );

  SetRect(aRect);

  SetExtentToWholeScreen();

  ActivateL();

  }

  但是要千萬記得:SetExtentToWholeScreen()一定要在SetRect(aRect)之後調用才有效果。這點很容易理解,因為如果SetExtentToWholeScreen()改變螢幕為全屏後,再調用SetRect(aRect)又把螢幕尺寸設定為UI裡傳遞的"矩形地區"的大小了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.