#include <Symbol.h>
#include <MyFrame.h>

extern SOCKET hAfxRemoteSocket;


void RegisterQQ_RecvFC(void (*pFc)(char*, int, DWORD, IP_ADDRESS), DWORD);
void SendRemoteQQ(char* pString, int nCount, DWORD dwIP);



void far QQ_Recv_QQorkstation(char* pString, int nSize, DWORD dwParam, IP_ADDRESS mIpAddr)
{
    CDialog* pDialog = (CDialog *)dwParam;
    if (pDialog == NULL)
    {
        return;
    }

    CMultiEdit* pEdit2 = (CMultiEdit *)pDialog->ObjectFromID(EDITBASE + 2 - 1);

    char buf[1024];
    ::sprintf (buf, "[对方IP: \"%d.%d.%d.%d\"]\xd\xa",
        mIpAddr[0],
        mIpAddr[1],
        mIpAddr[2],
        mIpAddr[3]);

    _fstrcat (buf, pString);

    pEdit2->SetWindowText (buf);
    pEdit2->Paint();

    return;
}


void far ClickButton1_QQWorkstation(CObject* pCur)
{
    if (hAfxRemoteSocket == INVALID_SOCKET)
    {
        ::AfxMessageBox ("网络", "程序没有加载WinSail的TCP/IP协议栈！");
        return;
    }

    CDialog *pDialog = (CDialog *)pCur->GetParent();

    CMultiEdit* pEdit1 = (CMultiEdit *)pDialog->ObjectFromID(EDITBASE + 1 - 1);

    char buf[512];

    _fmemset(buf, 0, 512);
    pEdit1->GetWindowText (buf, 512);


    ::SendRemoteQQ(buf, _fstrlen(buf) + 1, NULL);

    return;
}


void far QQ_Workstation()
{

	//Create a Dialog On the Center of Desktop
	CDialog* pDialog = new CDialog;//分配实例
	pDialog->CreateWindow(0, 0, 576, 303, "聊天室");//建立窗口
	pDialog->Center();//窗口对中


	//Create Close's Button Control
	CCloseButton* pCloseButton = new CCloseButton(pDialog);//分配和建立关闭按钮

	//Create a Bitmap Control
	CLabel* pLabel1 = new CLabel(pDialog);
	pLabel1->CreateObject(1, 29, 123, 263, NULL);
	pLabel1->SetBitmapFlags(TRUE);
	pLabel1->SetBackColorDisplay(TRUE);
	pLabel1->SetBitmapFile("Bmp\\DESKTOP.BMP");

	//Create MultiEdit Control
	CMultiEdit* pEdit1 = new CMultiEdit(pDialog);
	pEdit1->CreateObject(175, 174, 381, 78, NULL);
	pEdit1->SetID(EDITBASE + 1 - 1);

	//Create a Button Control
	CButton* pButton1 = new CButton(pDialog);
	pButton1->CreateObject(261, 260, 211, 24, "发送(\x3S\x3)");
	pButton1->SetShortcutKey(VK_ALT_S);
	pButton1->SetID(BUTTONBASE + 1 - 1);

	//Create MultiEdit Control
	CMultiEdit* pEdit2 = new CMultiEdit(pDialog);
	pEdit2->CreateObject(175, 52, 381, 113, NULL);
	pEdit2->SetID(EDITBASE + 2 - 1);

	//Create a Label Control
	CLabel* pLabel2 = new CLabel(pDialog);
	pLabel2->CreateObject(138, 98, 28, 16, "接收");

	//Create Group Control
	CStatic* pGroup1 = new CStatic(pDialog);
	pGroup1->CreateObject(132, 29, 433, 263, "聊天室");

	//Create a Label Control
	CLabel* pLabel3 = new CLabel(pDialog);
	pLabel3->CreateObject(138, 201, 24, 16, "发送");


	//Show Window
	pDialog->ShowWindow();//显示窗口

    pButton1->SetFc(::ClickButton1_QQWorkstation);
    ::RegisterQQ_RecvFC (::QQ_Recv_QQorkstation, (DWORD)pDialog);

	//go into Message Loop
	int nResult = pDialog->DoModal();//进入窗口消息循环

    ::RegisterQQ_RecvFC ((void (*)(char *, int))NULL, (DWORD)NULL);

	//Destroy Window
	delete pDialog;//删除对话框



	return;

}
