SOUI中SRealWnd的使用

這裏做一個VLC的例子(在SOUI中使用VLC來播放一下本地文件和流)

Step1 使用嚮導建立一個工程,將需要包含的VLC文件包含進去
Step2 SOUI 界面的一些設置
Step3 編寫代碼 實現對應的功能
Step4 查看效果

思考:怎麼減小VLC的體積 減小plugins的體積?

  • Step1 使用嚮導建立工程 建立XML文件

主窗口

<SOUI name="mainWindow" title="@string/title" bigIcon="ICON_LOGO:32" smallIcon="ICON_LOGO:16" width="800" height="600" margin="5,5,5,5" resizable="0" wndType="appMain" appWnd="1" translucent="0">
    <root skin="skin_1_jpg" cache="1">
        <caption pos="0,0,-0,30" show="1" font="adding:0">
            <text pos="5,5" colorText="#ffffffff" font="face:宋體,size:20">VLC demo</text>
            <imgbtn name="btn_close" skin="_skin.sys.btn.close" pos="-45,0" tip="close" animate="1" />
        </caption>
        <window pos="5,[2,-5,-5">
            <text pos="26,16,@-1,@24" colorText="#ffffff">播放路徑:</text>
            <edit pos="[10,{,-100,@24" colorBkgnd="#FFFFFF" cueText="" colorText="#000000" name="edit_playurl" colorBkgnd="RGBA(0,0,0,0)" margin="1,1,1,1" colorBorder="#F9E3F8" />
            <button pos="[5,{,@60,@24" colorText="#FFFFFF" skin="skin_1_jpg" name="btn_play">播 放</button>
            <window pos="26,[10,@730,@500">
                <realwnd pos="10,10,-10,-10" name="wnd_play" wndclass="SRealWnd" id="" wndname="playwnd" margin="1,1,1,1" colorBorder="#F9E3F8" />
            </window>
        </window>
    </root>
</SOUI>

子窗口

<?xml version="1.0"?>
<SOUI name="window" title="佈局" bigIcon="ICON_LOGO:32" smallIcon="ICON_LOGO:16" width="400" height="300" margin="1,5,5,5" wndType="8" toolWindow="1">
    <root cache="1">
        <window pos="0,0,-0,-0" colorText="#ffffff" skin="skin_2_jpg" />
    </root>
</SOUI>
  • Step2 界面的設置

可以參照mfc.demo這個例子 
這裏直接抄過來 改一下對應的類

#pragma once

#include <unknown/obj-ref-impl.hpp>

namespace SOUI
{
    class CSouiRealWndHandler :public TObjRefImpl2<IRealWndHandler,CSouiRealWndHandler>
    {
    public:
        CSouiRealWndHandler(void);
        ~CSouiRealWndHandler(void);

        /**
         * SRealWnd::OnRealWndCreate
         * @brief    創建真窗口
         * @param    SRealWnd * pRealWnd --  窗口指針
         * @return   HWND -- 創建出來的真窗口句柄
         * Describe  
         */    
        virtual HWND OnRealWndCreate(SRealWnd *pRealWnd);

        /**
        * SRealWnd::OnRealWndDestroy
        * @brief    銷燬窗口
        * @param    SRealWnd *pRealWnd -- 窗口指針
        *
        * Describe  銷燬窗口
        */
        virtual void OnRealWndDestroy(SRealWnd *pRealWnd);

        /**
        * SRealWnd::OnRealWndInit
        * @brief    初始化窗口
        * @param    SRealWnd *pRealWnd -- 窗口指針
        *
        * Describe  初始化窗口
        */
        virtual BOOL OnRealWndInit(SRealWnd *pRealWnd);

        /**
        * SRealWnd::OnRealWndSize
        * @brief    調整窗口大小
        * @param    SRealWnd *pRealWnd -- 窗口指針
        * @return   BOOL -- TRUE:用戶管理窗口的移動;FALSE:交由SOUI自己管理。
        * Describe  調整窗口大小, 從pRealWnd中獲得窗口位置。
        */
        virtual BOOL OnRealWndSize(SRealWnd *pRealWnd);
    };

}
#include "StdAfx.h"
#include "SouiRealWndHandler.h"
#include "SPlayDlg.h"

namespace SOUI
{
    CSouiRealWndHandler::CSouiRealWndHandler(void)
    {
    }

    CSouiRealWndHandler::~CSouiRealWndHandler(void)
    {
    }

    HWND CSouiRealWndHandler::OnRealWndCreate( SRealWnd *pRealWnd )
    {
        const SRealWndParam &param=pRealWnd->GetRealWndParam();
        if(param.m_strClassName==_T("SRealWnd"))
        {
            SPlayDlg *pWnd = new SPlayDlg;
            //mfc.demo中的例子 pbtn->Create(param.m_strWindowName,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,::CRect(0,0,0,0),CWnd::FromHandle(pRealWnd->GetContainer()->GetHostHwnd()),pRealWnd->GetID());
            pWnd->Create(pRealWnd->GetContainer()->GetHostHwnd(), WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, 0);
            pRealWnd->SetData(pWnd);
            //返回成功創建後的窗口句柄
            return pWnd->m_hWnd;
        }else
        {
            return 0;
        }
    }

    void CSouiRealWndHandler::OnRealWndDestroy( SRealWnd *pRealWnd )
    {
        const SRealWndParam &param=pRealWnd->GetRealWndParam();
        if(param.m_strClassName==_T("SRealWnd"))
        {//銷燬真窗口,釋放窗口占用的內存
            SPlayDlg * pWnd = (SPlayDlg*) pRealWnd->GetData();
            if(pWnd)
            {
                pWnd->DestroyWindow();
                delete pWnd;
            }
        }
    }
    
    //不處理,返回FALSE
    BOOL CSouiRealWndHandler::OnRealWndSize( SRealWnd *pRealWnd )
    {
        return FALSE;
    }

    //不處理,返回FALSE
    BOOL CSouiRealWndHandler::OnRealWndInit( SRealWnd *pRealWnd )
    {
        return FALSE;
    }
}


Step3 處理播放的邏輯
關於vlc怎麼包含對應的文件 文末給出github上整個工程地址(這個後續增加)

void CMainDlg::OnPlay()
{
    //SMessageBox(NULL, L"點擊了播放", L"提示", MB_OK);
    ReleaseVlc();
    //處理播放的邏輯
    SRealWnd* pWnd = (SRealWnd*)FindChildByName(L"wnd_play");
    SASSERT(pWnd);

    SStringT strPath = L"";
    SEdit* pEdit = FindChildByName2<SEdit>(L"edit_playurl");
    strPath = pEdit->GetWindowTextW();
    
    

    //對路徑進行一個編碼的轉換 轉換成UTF8來進行調用
    wchar_t* wchar = strPath.GetBuffer(strPath.GetLength());
    strPath.ReleaseBuffer();
    size_t len = wcslen(wchar) + 1;
    size_t converted = 0;
    char* format = (char*)malloc(len * sizeof(char));
    wcstombs_s(&converted, format, len, wchar, _TRUNCATE);

    //根據strPath判斷播放的類型 如果是本地則使用本地的播放方法 區別 m_media 的創建方法
    int type = strPath.Find(L"http");
    m_media = strPath.Find(L"http") == -1 ? libvlc_media_new_path(m_inst, format) : libvlc_media_new_location(m_inst, format);
    if (!m_media)
    {
        SMessageBox(NULL, L"輸入文件名有誤,請重新輸入!\n正確輸入例如:F:\\1.jpg", L"提示", MB_OK);
        return;
    }
    m_player = libvlc_media_player_new_from_media(m_media);
    libvlc_media_release(m_media);
    libvlc_media_player_set_hwnd(m_player, (void*)pWnd->GetRealHwnd());
    int ret = libvlc_media_player_play(m_player);
    if (0 != ret)
    {
        SMessageBox(NULL,L"播放文件失敗",L"提示",MB_OK);
    }
    free(format);
}

void CMainDlg::ReleaseVlc()
{
    if (m_player != nullptr)
    {
        libvlc_media_player_stop(m_player);
        libvlc_media_player_release(m_player);
    }
    m_player = nullptr;

}

Step4 效果圖

111

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章