美文网首页程序员手机移动程序开发
10 SkinUI自定义控件和常用组件

10 SkinUI自定义控件和常用组件

作者: 吴忠亮 | 来源:发表于2017-01-18 08:09 被阅读177次

10.1 自定义控件

View组件只是一个矩形的空白区域,没有任何内容。对于SkinUI的其他UI组件来说,它们都继承了View组件,然后在View组件提供的空白区域上绘制外观。

基于SkinUI UI组件的实现原理,当SkinUI系统提供的UI组件不足以满足项目需求时,开发者完全可以通过继承View组件来派生自定义组件。当开发者打算派发自己的UI组件时,首先定义一个继承View的子类,然后重写View类的一个或多个方法。下面是一个自定义组件的源码:

  • 头文件
class CSkinMyButton : public CSkinButton
{
public:
    CSkinMyButton(CSkinView* pParent);
public:
    virtual void DrawForeground(HDC hdc);
    SKINUI_DECLARE_DYNCREATE()
};
  • 源文件
SKINUI_IMPLEMENT_DYNCREATE(CSkinMyButton)
CSkinMyButton::CSkinMyButton(CSkinView* pParent)
: CSkinButton(pParent)
{
}
void CSkinMyButton::DrawForeground(HDC hdc)
{
}

以上代码通过两个宏将自定义控件注册到SkinUI的工厂函数,可以在布局文件里通过【SkinMyButton】使用这个控件。
给自定义控件增加自定义属性,可以通过重写以下方法:

  • 头文件
virtual void InitAttribute(const CXMLNode& node);
  • 源文件
void CSkinMyButton::InitAttribute(const CXMLNode& node)
{
    CSkinButton::InitAttribute(node);

    tstring strAttribute;
    if(node.GetAttribute(_T("MyAttribute"), strAttribute))
    {
        m_MyAttribute = strAttribute;
    }
}

10.2 常用组件

10.2.1 菜单

菜单组件
  • 调用以下接口弹出资源菜单:
static HWND PopupMenu(CSkinDialog* pNotifyDlg, const tstring& strMenu, const CPoint& ptPopup, const CSize& szButton, PopupDirection direction = PD_UNKNOWN, CSkinView* pNotifyView = NULL);
  • 调用以下接口弹出动态菜单:
static HWND PopupMenu(CSkinDialog* pNotifyDlg, const CXMLNode& xmlNode, const CPoint& ptPopup, const CSize& szButton, PopupDirection direction = PD_UNKNOWN, CSkinView* pNotifyView = NULL);

弹出菜单时,可以自适应方向弹出,也可以指定弹出方向。

//弹出方向
enum PopupDirection
{
    PD_UNKNOWN = 0x0000,        //自适应
    PD_LEFT_TOP = 0x0001,       //左上弹出
    PD_RIGHT_TOP = 0x0002,      //右上弹出
    PD_LEFT_BOTTOM = 0x0004,    //左下弹出
    PD_RIGHT_BOTTOM = 0x0008    //右下弹出
};
  • 下面是弹出菜单的代码的示例代码:
void CMainDialog::OnBtnClickedPopMenu(UINT uNotifyCode, int nID, CSkinView* pView)
{
    if(pView)
    {
        CRect rect = pView->GetRect();
        CPoint ptOffset = pView->GetScrollOffset(CRect());
        CPoint pt(rect.left - ptOffset.x, rect.bottom - ptOffset.y);
        pView->GetOwner()->ClientToScreen(pt);
        SkinUI::PopupMenu(this, _T("MainMenu.xml"), pt,rect.Size(), PD_UNKNOWN);
    }
}

10.2.2 模态消息框

模态消息框组件
  • 调用以下接口弹出模态消息框:
static UINT MsgBox(const tstring& strText, HWND hParent = NULL, const tstring& strCaption = _T(""), UINT nType = MB_OK, const tstring& strOk = _T(""), const tstring& strCancel = _T(""), const tstring& strCheckBox = _T(""), BOOL bChecked = FALSE, tstring& strEditText = tstring(), BOOL bEditView = FALSE);
  • 或者调用对话框的方法:
UINT MsgBox(const tstring& strText, const tstring& strCaption = _T(""), UINT nType = MB_OK, const tstring& strOk = _T(""), const tstring& strCancel = _T(""), const tstring& strCheckBox = _T(""), BOOL bChecked = FALSE, tstring& strEditText = tstring(), BOOL bEditView = FALSE);
  • nType仅支持以下类型:
MB_OK
MB_YESNO
MB_ICONWARNING
MB_ICONERROR
  • 下面是弹出模态消息框的代码的示例代码:
void CMainDialog::OnBtnClickedMsgBox(UINT uNotifyCode, int nID, CSkinView* pView)
{
    MsgBox(SkinUI::GetString(_T("IDS_CONTROL_SHOW_TEXT23")));
}

10.2.3 非模态消息框

非模态消息框组件
  • 调用以下接口弹出非模态消息框:
static UINT MsgBoxModeless(const tstring& strText, HWND hParent = NULL, const tstring& strCaption = _T(""), UINT nType = MB_OK, const tstring& strOk = _T(""), const tstring& strCancel = _T(""), const tstring& strCheckBox = _T(""), BOOL bChecked = FALSE, UINT nId = 0, LPARAM lParam = NULL);
  • 或者调用对话框的方法:
UINT MsgBoxModeless(const tstring& strText, const tstring& strCaption = _T(""), UINT nType = MB_OK, const tstring& strOk = _T(""), const tstring& strCancel = _T(""), const tstring& strCheckBox = _T(""), BOOL bChecked = FALSE, UINT nId = 0, LPARAM lParam = NULL);
  • nType仅支持以下类型:
MB_OK
MB_YESNO
MB_ICONWARNING
MB_ICONERROR
  • 下面是弹出非模态消息框的代码的示例代码:
void CMainDialog::OnBtnClickedMsgBoxModeless(UINT uNotifyCode, int nID, CSkinView* pView)
{
    MsgBoxModeless(SkinUI::GetString(_T("IDS_CONTROL_SHOW_TEXT24")));
}

10.2.3 Alert

Alert组件
  • 调用以下接口弹出Alert:
void Alert(const tstring& strTitle, const tstring& strMessage, const tstring& strButtonName);
  • 下面是弹出Alert的示例代码:
void CMainDialog::OnBtnClickedAlert(UINT uNotifyCode, int nID, CSkinView* pView)
{
    tstring strTitle = _T("提示");
    tstring strMessage = _T("这是一个提示信息!");
    tstring strButtonName = _T("确定");
    Alert(strMessage, strTitle, strButtonName);
}

10.2.4 Confirm

Confirm组件
  • 调用以下接口弹出Confirm:
UINT Confirm(const tstring& strTitle, const tstring& strMessage, const tstring& strButtonOK, const tstring& strButtonCancel);
  • 下面是弹出Confirm的示例代码:
void CMainDialog::OnBtnClickedConfirm(UINT uNotifyCode, int nID, CSkinView* pView)
{
    tstring strTitle = _T("提示");
    tstring strMessage = _T("有没有收到我的消息?");
    tstring strButtonOK = _T("收到");
    tstring strButtonCancel = _T("没有");
    UINT nIndex = Confirm(strMessage, strTitle, strButtonOK, strButtonCancel);
}

10.2.5 Prompt

Prompt组件
  • 调用以下接口弹出Prompt:
UINT Prompt(const tstring& strTitle, const tstring& strMessage, const tstring& strButtonOK, const tstring& strButtonCancel, tstring& strEditText);
  • 下面是弹出Prompt的示例代码:
void CMainDialog::OnBtnClickedPrompt(UINT uNotifyCode, int nID, CSkinView* pView)
{
    tstring strTitle = _T("提示");
    tstring strMessage = _T("有没有收到我的消息?");
    tstring strButtonOK = _T("收到");
    tstring strButtonCancel = _T("没有");

    tstring strEditText;
    LONG nIndex = GetOwner()->Prompt(strMessage, strTitle, strButtonOK, strButtonCancel, strEditText);
}

10.2.6 Toast

Toast组件
  • 调用以下接口弹出Toast:
void Toast(const tstring& strText, UINT nType = TOAST_INFORMATION, int nDuration = 2, int nDelay = 0);
  • 下面是弹出Toast的示例代码:
void CMainDialog::OnBtnClickedToast(UINT uNotifyCode, int nID, CSkinView* pView)
{
    tstring strText = _T("这是一个提示信息!");
    UINT nType = TOAST_INFORMATION;
    int nDuration = 5;
    int nDelay = 0;
    Toast(strText, nType, nDuration, nDelay);
}

10.2.7 ActionSheet

ActionSheet组件
  • 调用以下接口弹出ActionSheet:
int ActionSheet(const list<tstring>& lstSheet, const tstring& strCaption = _T(""), const tstring& strCancel = _T(""));
  • 下面是弹出ActionSheet的示例代码:
void CMainDialog::OnBtnClickedActionSheet(UINT uNotifyCode, int nID, CSkinView* pView)
{
    tstring strTitle = _T("谁最屌?");
    tstring strButtonCancel = _T("取消");

    list<tstring> lstSheet;
    lstSheet.push_back(_T("孙悟空"));
    lstSheet.push_back(_T("猪八戒"));
    lstSheet.push_back(_T("唐僧"));
    lstSheet.push_back(_T("沙和尚"));
    LONG nIndex = GetOwner()->ActionSheet(lstSheet, strTitle, strButtonCancel);
}

10.2.8 颜色选择对话框

颜色选择对话框
  • 下面是弹出颜色选择对话框的示例代码:
void CMainDialog::OnBtnClickedColorDialog(UINT uNotifyCode, int nID, CSkinView* pView)
{
    CSkinColorDialog dlg(Color::Red, CC_FULLOPEN | CC_RGBINIT);
    if(dlg.DoModal(m_hWnd) == IDOK)
    {
        Color color = dlg.GetColor();
    }
}

10.2.9 文件选择对话框

文件选择对话框
  • 下面是弹出文件选择对话框的示例代码:
void CMainDialog::OnBtnClickedColorDialog(UINT uNotifyCode, int nID, CSkinView* pView)
{
    CSkinFileDialog dlg(SkinUI::GetString(_T("IDS_SEL_FILE")), s_lpstrAllFileFilter, 5, TRUE);
    if(dlg.DoModal(m_hWnd) == IDOK)
    {
        const list<tstring>& lstFile = dlg.GetFile();
    }
}

10.2.10 文件夹选择对话框

文件夹选择对话框
  • 下面是弹出文件夹选择对话框的示例代码:
void CMainDialog::OnBtnClickedColorDialog(UINT uNotifyCode, int nID, CSkinView* pView)
{
    CSkinFolderDialog dlg(SkinUI::GetString(_T("IDS_SEL_FOLDER")), TRUE);
    if(dlg.DoModal(m_hWnd) == IDOK)
    {
        tstring strFolder = dlg.GetFolder();
    }
}

10.2.11 另存为对话框

另存为对话框
  • 下面是弹出另存为对话框的示例代码:
void CMainDialog::OnBtnClickedColorDialog(UINT uNotifyCode, int nID, CSkinView* pView)
{
    CSkinSaveAsDialog dlg(_T("123.png"), s_lpstrAllFileFilter);
    if(dlg.DoModal(m_hWnd) == IDOK)
    {
        tstring strFolder = dlg.GetFile();
    }
}

相关文章

网友评论

    本文标题:10 SkinUI自定义控件和常用组件

    本文链接:https://www.haomeiwen.com/subject/rsjwuttx.html