简单控件示例(02_BasicControls)

简单控件

此示例展示以下控件的使用方法:

按钮控件UiButton 使用流程

单行文本框控件UiSingleLineEdit 使用流程

文字工具条控件UiToolbar_Text 使用流程

对话框MzMessageBoxEx 的使用参见MzCommonDlg.h

02_BasicControls.jpg
02_BasicControls_1.jpg
02_BasicControls_2.jpg

获得编辑框内的内容:

  CMzString str(64+m_Edit.GetTextLen());
  // 格式化字符串
  wsprintf(str.C_Str(), L"You'd input:\n%s", m_Edit.GetText().C_Str());
  // 弹出消息框
  MzMessageBoxEx(m_hWnd, str.C_Str(), L"", MB_OK, false);

设置及显示弹出菜单:

  CPopupMenu ppm;
  struct PopupMenuItemProp pmip;    

  pmip.itemCr = MZC_BUTTON_PELLUCID;
  pmip.itemRetID = IDC_PPM_CANCEL;
  pmip.str = L"Cancel";
  ppm.AddItem(pmip);

  pmip.itemCr = MZC_BUTTON_ORANGE;
  pmip.itemRetID = IDC_PPM_OK;
  pmip.str = L"OK";
  ppm.AddItem(pmip);  

  RECT rc = MzGetWorkArea();    
  rc.top = rc.bottom - ppm.GetHeight();
  ppm.Create(rc.left,rc.top,RECT_WIDTH(rc),RECT_HEIGHT(rc),m_hWnd,0,WS_POPUP);  

  int nID = ppm.DoModal();
  if (nID==IDC_PPM_OK)
  {
  // do what you want...
  }
  if (nID==IDC_PPM_CANCEL)
  {
  // do what you want...
  }

完整示例:

/************************************************************************/
/*
 * Copyright (C) Meizu Technology Corporation Zhuhai China
 * All rights reserved.
 * 中国珠海, 魅族科技有限公司, 版权所有.
 *
 * This file is a part of the Meizu Foundation Classes library.
 * Author:    Michael
 * Create on: 2008-12-8
 */
/************************************************************************/

//请按照以步骤运行此实例代码:
//首先, 打开VS2005/2008创建一个Win 32智能设备项目
//在项目向导中选择M8SDK, 并勾选空项目
//然后,在项目中新建一个cpp文件,将此处代码拷贝到cpp文件中
//最后,按照M8SDK的帮助文档,配置项目属性
//现在,可以运行此程序了

//包含MZFC库的头文件
#include <mzfc_inc.h>

//此代码演示了:
//  创建和初始化应用程序
//  创建和初始化窗体
//  按钮控件的使用及其命令消息的处理
//  使用单行编辑器
//  控件的显示、隐藏,
//  文字按钮工具条、弹出菜单、消息框

#define MZ_IDC_TESTBTN1  101
#define MZ_IDC_TOOLBAR1  102

#define IDC_PPM_OK    103
#define IDC_PPM_CANCEL  104

#define MZ_IDC_EDIT  105

// 从 CMzWndEx 派生的主窗口类
class CSample1MainWnd: public CMzWndEx
{
  MZ_DECLARE_DYNAMIC(CSample1MainWnd);
public:
  // 窗口中的按钮控件
  UiButton m_btn;
  
  // 文字按钮工具条
  UiToolbar_Text m_Toolbar;

  // 单行编辑器
  UiSingleLineEdit m_Edit;

protected:
  // 窗口的初始化
  virtual BOOL OnInitDialog()
  {
    // 必须先调用基类的初始化
    if (!CMzWndEx::OnInitDialog())
    {
      return FALSE;
    }

              // 初始化单行编辑控件,并添加到窗口中
    m_Edit.SetPos(MZM_MARGIN_MAX,MZM_MARGIN_MAX,GetWidth()-MZM_MARGIN_MAX*2,70);
    m_Edit.SetID(MZ_IDC_TESTBTN1); //you must set an unique ID for a edit control
    m_Edit.SetTip(L"Please enter text...");             // set the tips text
    m_Edit.SetTextColor(RGB(255,0,0)); // you could also set the color of text
              m_Edit.SetSipMode(IM_SIP_MODE_GEL_PY);
    AddUiWin(&m_Edit); // don't forget to add the control to the window

              // 初始化按钮控件,并添加到窗口中
    m_btn.SetButtonType(MZC_BUTTON_PELLUCID);
    m_btn.SetPos(100,130,280,100);
    m_btn.SetID(MZ_IDC_TESTBTN1);
    m_btn.SetText(L"Get Edit's Text");
    //m_btn.SetTextColor(RGB(0,255,0));
    AddUiWin(&m_btn);

              // 初始化文字工具条,并添加到窗口中
    m_Toolbar.SetPos(0,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR,GetWidth(),MZM_HEIGHT_TEXT_TOOLBAR);
    m_Toolbar.SetButton(0, true, true, L"Exit");
    m_Toolbar.SetButton(1, true, true, L"Menu");
    m_Toolbar.SetButton(2, true, true, L"Hide button");
    m_Toolbar.SetID(MZ_IDC_TOOLBAR1);
    AddUiWin(&m_Toolbar);

    return TRUE;
  }

  // 重载命令消息的处理函数
  virtual void OnMzCommand(WPARAM wParam, LPARAM lParam)
  {
    UINT_PTR id = LOWORD(wParam);
    switch(id)
    {
    case MZ_IDC_TESTBTN1:
      {
        CMzString str(64+m_Edit.GetTextLen());
        // 格式化字符串
        wsprintf(str.C_Str(), L"You'd input:\n%s", m_Edit.GetText().C_Str());
        // 弹出消息框
        MzMessageBoxEx(m_hWnd, str.C_Str(), L"", MB_OK, false);
      }
      break;
    case MZ_IDC_TOOLBAR1:
      {
        int nIndex = lParam;
        if (nIndex==0)
        {
          PostQuitMessage(0);
          return;
        }
        if (nIndex==1)
        {
          // 弹出菜单
          CPopupMenu ppm;
          struct PopupMenuItemProp pmip;      

          pmip.itemCr = MZC_BUTTON_PELLUCID;
          pmip.itemRetID = IDC_PPM_CANCEL;
          pmip.str = L"Cancel";
          ppm.AddItem(pmip);

          pmip.itemCr = MZC_BUTTON_ORANGE;
          pmip.itemRetID = IDC_PPM_OK;
          pmip.str = L"OK";
          ppm.AddItem(pmip);  

          RECT rc = MzGetWorkArea();      
          rc.top = rc.bottom - ppm.GetHeight();
          ppm.Create(rc.left,rc.top,RECT_WIDTH(rc),RECT_HEIGHT(rc),m_hWnd,0,WS_POPUP);      
          int nID = ppm.DoModal();
          if (nID==IDC_PPM_OK)
          {
            // do what you want...
          }
          if (nID==IDC_PPM_CANCEL)
          {
            // do what you want...
          }
          return;
        }
        if (nIndex==2)
        {
          m_btn.SetVisible(!m_btn.IsVisible());
          m_btn.Invalidate();
          m_btn.Update();
          if (m_btn.IsVisible())
            m_Toolbar.SetButtonText(2, L"Hide button");
          else
            m_Toolbar.SetButtonText(2, L"Show button");

          m_Toolbar.Invalidate();
          m_Toolbar.Update();
          return;
        }
      }
      break;
    }
  }
};

MZ_IMPLEMENT_DYNAMIC(CSample1MainWnd)

// 从 CMzApp 派生的应用程序类
class CSample1App: public CMzApp
{
public:
  // 应用程序的主窗口
  CSample1MainWnd m_MainWnd;

  // 应用程序的初始化
  virtual BOOL Init()
  {
              // 初始化 COM 组件
    CoInitializeEx(0, COINIT_MULTITHREADED);

              // 创建主窗口
    RECT rcWork = MzGetWorkArea();
    m_MainWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0);
    m_MainWnd.SetBgColor(RGB(0,0,0));
    m_MainWnd.Show();

              // 成功则返回TRUE
    return TRUE;
  }
};

// 全局的应用程序对象
CSample1App theApp;


Generated at Tue Feb 9 15:09:53 2010 for Meizu M8 SDK Documentation by  doxygen 1.6.1