// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
// 创建窗口;
#include <process.h>
#include "resource.h"
#include "Assist.h"

// 全局变量;
HMODULE g_hModule = NULL;
HWND g_hDlgWnd = NULL;

// 声明函数;
void __cdecl MyWinThread(LPVOID lpParam);
BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);

BOOL APIENTRY DllMain( HMODULE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        g_hModule = hModule;
        _beginthread(MyWinThread, 0, NULL);
        //g_hDlgWnd = CreateDialogParam(g_hModule, MAKEINTRESOURCE(IDD_DLG_ASSIST), NULL, DialogProc, WM_INITDIALOG);
        //ShowWindow(g_hDlgWnd, SW_SHOW);
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

void __cdecl MyWinThread(LPVOID lpParam)
{
    g_hDlgWnd = CreateDialogParam(g_hModule, MAKEINTRESOURCE(IDD_DLG_ASSIST), NULL, DialogProc, NULL);

    RECT rtDlg;
    GetWindowRect(g_hDlgWnd, &rtDlg);

    int nScreenX = GetSystemMetrics(SM_CXSCREEN);
    int nScreenY = GetSystemMetrics(SM_CYSCREEN);

    // 在资源中设置窗口属性为TopMost才能置顶;
    SetWindowPos(g_hDlgWnd, HWND_TOP, nScreenX / 2 - rtDlg.right/2, nScreenY/2 - rtDlg.bottom/2, 0, 0, SWP_NOSIZE|SWP_SHOWWINDOW);
    // 设置鼠标样式;
    //HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GUITEST));
    //ShowWindow(g_hDlgWnd, SW_SHOW);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
#if 1
        if ( !IsDialogMessage(g_hDlgWnd, &msg) ) // 指定窗口;
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
#else
        TranslateMessage(&msg);
        DispatchMessage(&msg);
#endif
    }
}

// MSDN:
// DialogProc它必须不调用DefWindowProc函数来处理不需要的邮件。不需要的消息由对话框窗口过程在内部处理
// 所以内部不要调用DefWindowProc,否则会有异常现象;
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
        InitCallData();
        break;
    case WM_CLOSE:
        DestroyWindow(hwndDlg);
        //EndDialog(hwndDlg, 0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_COMMAND:
        {
            // 控件ID;
            INT nID = LOWORD(wParam);
            // 控件事件;
            INT nEvent = HIWORD(wParam);
            switch(nID)
            {
            case IDOK:
                MessageBox(hwndDlg, _T("确定"), _T("单击"), MB_OK);
                break;
            case IDCANCEL:
                MessageBox(hwndDlg, _T("取消"), _T("单击"), MB_OK);
                break;
            case BTN_SAVE_AS_OUTPUT_DATA:
                break;
            case BTN_GO:
                {

                }
                break;
			case BTN_CONNECT:
				{
					Call_Connect();
				}
				break;
			case BTN_HIJACT_ALL_CALL:
				if ( HijackedAllCall() )
				{
					MessageBox(hwndDlg, _T("劫持All Call成功"), _T("劫持"), MB_OK);
				}
				break;
            default:
                break;
            }                
        }
        break;
    case WM_LBUTTONUP:
        //MessageBox(hwndDlg, _T("xxxxx"), _T("单击"), MB_OK);
        break;
    default:
        break;
    }

    return FALSE;
}