// demoSetTransparentWndDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "demo.h"
#include "demoSetTransparentWndDlg.h"
#include "afxdialogex.h"
#include "demoTools.h"
#include "HwmSdkAgent.h"

// demoSetTransparentWndDlg 对话框

IMPLEMENT_DYNAMIC(demoSetTransparentWndDlg, CDialogEx)

demoSetTransparentWndDlg::demoSetTransparentWndDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_SET_TRANSPARENT_WND_DIALOG, pParent)
{

}

demoSetTransparentWndDlg::~demoSetTransparentWndDlg()
{
}

void demoSetTransparentWndDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_TRANSPARENT_WND, m_wndsEdit);
}


BEGIN_MESSAGE_MAP(demoSetTransparentWndDlg, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_SET_TRANSPARENT_WND_OK, &demoSetTransparentWndDlg::OnBnClickedButtonOk)
END_MESSAGE_MAP()


// demoSetTransparentWndDlg 消息处理程序


int demoSetTransparentWndDlg::clickSetTransparentWnd()
{
    int ret=0;
    CString tempCString;

    m_wndsEdit.GetWindowText(tempCString);
    std::string tempString = CTools::UNICODE2UTF(tempCString.GetString());
    vector<std::string> list = CTools::split(tempString, ';');
    std::string temp;
    int count = list.size();

    int realCount = 0;
    if (count > 0)
    {
        //申请结构体内存
        HWND* handles;
        handles = (HWND*)malloc(sizeof(HWND) * count);
        if (handles == NULL)
        {
            return -1;
        }
        memset(handles, 0, sizeof(HWND)*count);

        HWND* handlesTemp = handles;
        for (int i = 0; i < count; i++)
        {
            temp = list[i];
            *handlesTemp = CTools::str2hwnd(temp);
            handlesTemp++;
            realCount++;
        }

        ret = hwmsdkagent::SetTransparentWnd(handles, realCount);
        
        //释放内存空间
        free(handles);
        handles = NULL;
    }
    
    return ret;
}

void demoSetTransparentWndDlg::OnBnClickedButtonOk()
{
    int ret = clickSetTransparentWnd();
    if (hwmsdk::HWM_COMMON_SUCCESS != ret)
    {
        AfxMessageBox(_T("Set transparent wnds error"));
        return;
    }
    CDialogEx::OnOK();
}
