自己动手做Dialog Symbian

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

一直以来就是在看 Examp ,今天自己动手写自己的第一个空间,初学者可以和我一起学习,及我遇到的问题,帮助我们一起进步。

  打算做一个 Example 中的对话框输入控件,并把输入显示在手机背景上,好了,直接进入正题。

1 定义资源

  我们在默认的工程中 .rss 文件下的 RESOURCE MENU_PANE r_menu 中,定义一个自己的 MENU_ITEM ,参考已有的形式,相信不难解决,定义好后,在 inc 目录下添加自己定义的按钮 ID ,并在 .rls 文件中初始化自己定义按键名称(显示用的,相当于 Button 上的文字)

  之后再就来定义我做的 Dialog 的资源,关于资源中的参数的解释我在前面已经说过,这里就不详细说明,代码如下:

RESOURCE DIALOG r_dialog_text_edit_query

    {

    flags = EGeneralQueryFlags ;

    buttons = R_AVKON_SOFTKEYS_OK_CANCEL ;

    items =

        {

        DLG_LINE

            {

            type = EAknCtQuery;

            id = EGeneralQuery;

            control = AVKON_DATA_QUERY

                {

                layout = EDataLayout;

                label = "INPUT" ;

                control = EDWIN

                    {

                    width = 32;

                    maxlength = 32;

                    lines = 1;

                    };

                };

            }

        };

}

 

2 定义好资源,我们就可以建立我们这个控件的类,我们分成类头和类体来实现,符合 Symbian 的体系。

定义类头:

首先要定义这个头的宏,千万不要忘了在最后加上 #endif

    #ifndef MYEDITORDIALOG_H

#define MYEDITORDIALOG_H

……

#endif

之后可以参看 Example HelloWorld 来定义

#include <aknquerydialog.h>

#include <eikbctrl.h>

 

class CMyEditorDialog : public CAknTextQueryDialog

    {

public :

    CMyEditorDialog ( TDes & aBuf, HBufC *aDefInput);

   

    virtual ~CMyEditorDialog () {};

   

private :

    void PreLayoutDynInitL ();

private :

    HBufC & iDefInput ;

 

  };

开始接触 c++ 的朋友千万不要忘记在类的后面加 “;” , 这个类头不难理解,唯一需要解释的是 PreLayoutDynInitL ();

查看 SDK 得到如下解释:

From CEikdialog This function is called by the EIKON dialog framework just before the dialog is activated, after it has called PreLayoutDynInitL() and the dialog has been sized.

说明对话框活动的时候,需要这个函数才能设定好大小。(希望我不要翻译错误,英语很烂!)

 

实现类体

需要包含的库

#include <avkon.hrh> // 这个我测试下,发现似乎没有作用,是个系统的资源类

#include "MyEditorDialog.h"

同样可以参看 HW 代码原型,对构造函数默认初始化

CMyEditorDialog::CMyEditorDialog ( TDes & aBuf, HBufC *aDefInput )

: CAknTextQueryDialog ( aBuf ) // 注意这里很关键

, iDefInput (*aDefInput )

    {}

    还要实现 PreLayoutDynInitL ()

void CMyEditorDialog::PreLayoutDynInitL ()

    {

       // first we have to execute PreLayoutDynInitL() of the base-class

        CAknTextQueryDialog :: PreLayoutDynInitL ();

 

        // acquire pointer to editor-control and set the default input.

        CAknQueryControl * control = QueryControl ();

        control-> SetTextL ( iDefInput );

 

        // enable OK-button, so that default text can be accepted as it is

        // without modifying the text

        MakeLeftSoftkeyVisible ( ETrue );

    }

3 类的调用

  由于 UI 要调用 View ,所以我们先打好 View 的基础,在 View 的类头中添加

public :

    TDes & GetText (); 这个用来获得我们在 Dialog 的输入,返回是引用

private :

       const CFont * iFont ;  // 字体

       TBuf <24> iText ;  // 内容

实现 GetText ()

TDes & CMyEditorAppView::GetText ()

{  

    return iText ;

}

更改 ConstructL 设置字体

void CMyEditorAppView::ConstructL ( const TRect & aRect)

    {

    // Create a window for this application view

    CreateWindowL ();

   

    iFont = AknLayoutUtils :: FontFromId ( EAknLogicalFontPrimaryFont ); // 这里用到头 #include <aknutils.h>

    iText . Zero (); // 0

 

    // Set the windows size

    SetRect (aRect);

 

    // Activate the window, which makes it ready to be drawn

    ActivateL ();

    }

主要是 Draw 这个函数

void CMyEditorAppView::Draw ( const TRect & /*aRect*/ ) const

    {

    // Get the standard graphics context

    CWindowGc & gc = SystemGc ();

    gc. SetPenStyle ( CGraphicsContext :: EDotDashPen );

    gc. SetBrushColor ( KRgbWhite );

    gc. SetBrushStyle ( CGraphicsContext :: ESolidBrush );

    // Gets the control's extent

    TRect drawRect( Rect ());

 

    // Clears the screen

      if ( iText . Length () > 0)

            {

            gc. UseFont ( iFont );

            gc. DrawText ( iText , drawRect, Rect (). Height ()/3, CGraphicsContext :: ECenter );

            gc. DiscardFont ();

            }

      else

         {

         gc. Clear (drawRect);

         }

 

    }  

 

不过也很轻松的过去了,到此 View 的基础都已经打好。接下来就要在调用这个函数。

4 UI void CMyEditorAppUi::HandleCommandL ( TInt aCommand)

添加一个自己按钮的 Case

case ECommand3 :

           {

           HBufC * defaultText = StringLoader :: LoadLC ( R_FILE_TEXT );

           CMyEditorDialog * dlg = new ( ELeave ) CMyEditorDialog( iAppView ->GetText(), defaultText); //

           dlg-> ExecuteLD ( R_DIALOG_TEXT_EDIT_QUERY );

           CleanupStack :: PopAndDestroy ( defaultText );

           }

           break ;

这些都没难度,关键是 dlg-> ExecuteLD ( R_DIALOG_TEXT_EDIT_QUERY ); 自己在断点 Debug 的时候发现执行语句后 iText 的值就会得到,但是 Symbian 不让我们自己处理因为我们在 new 的时候第一个参数执行 CAknTextQueryDialog ( aBuf ) ,一个系统函数,查看 SDK 发现没有太多解释,网上也没查到满意的,还有就是 SDK 中的构造函数是 2 个参数的,可是现在我调用的是一个,这个问题我也没有解决,希望高手留下个说法。不胜感激

  接下来就可以调试测试自己的对不对,不过最好是每次写一个模块都要测试下,调试会发现出现一大堆错误,原因是程序没问题,可是在 DLL 链接的时候只有头是没有办法运行的,你还必须要告诉编译器,你还要用到什么类,我觉得 j2me 的时候有没这些情况,鄙视 Symbian ,好了,添加的方法是在 MMp 文件中 自己查看 SDK ,找到要添加的 Lib 就可以运行了,祝大家搞定

博文完 宝杰文 有误请斧正

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值