#include <windows.h>
#include <windowsx.h>
#include <aygshell.h>
#include <atlconv.h>
#include "ConnMgr.h"
#include "ConnectProcess.h"
#include <urlmon.h>
#include "MainWnd.h"
#pragma comment(lib, "aygshell.lib")
#pragma comment(lib, "urlmon.lib")
#define FILEDOWNNAME TEXT("http://img.szhk.com/Image/2009/08/25/200908250952220861.gif")
#define MY_CLASSNAME TEXT("MY_CLASSNAME")
#define MY_WINDOWSNAME TEXT("MY_WINDOWNAME")
MainWnd::MainWnd():m_Mgr(NULL)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::MainWnd()")));
}
MainWnd::~MainWnd()
{
if(m_Mgr != NULL)
{
delete m_Mgr;
m_Mgr = NULL;
}
DEBUGMSG(TRUE, (TEXT("MainWnd::~MainWnd()")));
}
BOOL MainWnd::Init(HINSTANCE hInstance, int nShowCmd)
{
WNDCLASS wcMB = {0};
wcMB.style = CS_HREDRAW|CS_VREDRAW;
wcMB.lpfnWndProc = MainWnd::MainProc;
wcMB.lpszClassName = MY_CLASSNAME;
wcMB.hCursor = NULL;
wcMB.hIcon = NULL;
wcMB.cbClsExtra = 0;
wcMB.cbWndExtra = 0;
wcMB.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcMB.hInstance = hInstance;
wcMB.lpszMenuName = NULL;
if(!RegisterClass(&wcMB))
{
return FALSE;
}
HWND hWnd = CreateWindow(MY_CLASSNAME, MY_WINDOWSNAME, WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, this);
if(!IsWindow(hWnd))
{
return FALSE;
}
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT MainWnd::MainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
MainWnd* pThis = (MainWnd*)GetWindowLong(hWnd, GWL_USERDATA);
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_CREATE, MainWnd::OnCreate);
HANDLE_MSG(hWnd, WM_DESTROY, pThis->OnDestroy);
HANDLE_MSG(hWnd, WM_COMMAND, pThis->OnCommand);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
BOOL MainWnd::OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
if(NULL == lpCreateStruct)
{
return FALSE;
}
MainWnd* pThis = (MainWnd*)lpCreateStruct->lpCreateParams;
if(NULL == pThis)
{
return FALSE;
}
SetWindowLong(hwnd, GWL_USERDATA, (LONG)pThis);
HINSTANCE hInstance = GetModuleHandle(NULL);
CreateWindow(TEXT("EDIT"), NULL, WS_BORDER|WS_CHILD|WS_VISIBLE, 10, 130, 150, 20, hwnd, (HMENU)IDC_MY_EDIT_URL, hInstance, NULL);
CreateWindow(TEXT("LISTBOX"), NULL, WS_CHILD|WS_VISIBLE|WS_BORDER, 10, 10, 150, 120, hwnd, (HMENU)IDC_MY_LISTBOX, hInstance, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("MAPURL"), WS_CHILD|WS_VISIBLE, 170, 10, 60, 20, hwnd, (HMENU)IDC_MY_BUTTON_MAPURL, hInstance, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("ENUM"), WS_CHILD|WS_VISIBLE, 170, 50, 60, 20, hwnd, (HMENU)IDC_MY_BUTTON_ENUM, hInstance, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("INTERNET"), WS_CHILD|WS_VISIBLE, 170, 90, 60, 20, hwnd, (HMENU)IDC_MY_BUTTON_INTERNET, hInstance, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("WAP"), WS_CHILD|WS_VISIBLE, 170, 140, 60, 20, hwnd, (HMENU)IDC_MY_BUTTON_WAP, hInstance, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("DownLoad"), WS_CHILD | WS_VISIBLE, 170, 190, 60, 20, hwnd, (HMENU)IDC_MY_BUTTON_DOWNLOAD, hInstance, NULL);
if(NULL == pThis->m_Mgr)
{
pThis->m_Mgr = new Connmgr;
}
SHINITDLGINFO shidi = {0};
shidi.dwFlags = SHIDIF_SIZEDLGFULLSCREEN|SHIDIF_CANCELBUTTON;
shidi.dwMask = SHIDIM_FLAGS;
shidi.hDlg = hwnd;
SHInitDialog(&shidi);
return TRUE;
}
void MainWnd::OnDestroy(HWND hwnd)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnDestroy\n")));
PostQuitMessage(0);
}
void MainWnd::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDCANCEL:{ OnClose(hwnd); } break;
case IDC_MY_BUTTON_MAPURL: { OnMapUrl(hwnd, hwndCtl, codeNotify); } break;
case IDC_MY_BUTTON_ENUM: { OnEnum(hwnd, hwndCtl, codeNotify); } break;
case IDC_MY_BUTTON_INTERNET: { OnInternet(hwnd, hwndCtl, codeNotify); } break;
case IDC_MY_BUTTON_WAP: { OnWap(hwnd, hwndCtl, codeNotify); } break;
case IDC_MY_BUTTON_DOWNLOAD: { OnDownLoad(hwnd, hwndCtl, codeNotify); } break;
}
}
void MainWnd::OnClose(HWND hwnd)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnDestroy\n")));
DestroyWindow(hwnd);
}
//枚举回调
int MainWnd::OnConnMgrDest(CONNMGR_DESTINATION_INFO *pDest, void* cx)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnConnMgrDest\n")));
MainWnd* pthis = (MainWnd*)cx;
HWND hwnd = FindWindow(MY_CLASSNAME, MY_WINDOWSNAME);
HWND hwndCtl = GetDlgItem(hwnd, IDC_MY_LISTBOX);
LPTSTR szBuf = new TCHAR[GUID_STR_LEN];
ZeroMemory(szBuf, sizeof(TCHAR)*GUID_STR_LEN);
StringFromGUID2(pDest->guid, szBuf, GUID_STR_LEN);
ListBox_AddString(hwndCtl, szBuf);
delete []szBuf;
szBuf = NULL;
return 0;
}
void MainWnd::OnMapUrl(HWND hwnd, HWND hwndCtl, UINT codeNotify)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnMapUrl\n")));
}
void MainWnd::OnEnum(HWND hwnd, HWND hwndCtl, UINT codeNotify)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnEnum\n")));
if(m_Mgr != NULL)
{
m_Mgr->EnumConnMgr(MainWnd::OnConnMgrDest, this);
}
}
void MainWnd::OnInternet(HWND hwnd, HWND hwndCtl, UINT codeNotify)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnInternet\n")));
if(m_Mgr != NULL)
{
if(!m_Mgr->ConnectNet())//连接net
{
return;
}
}
HWND hwndServer = NULL;
HWND hwndUrl = NULL;
hwndServer = GetDlgItem(hwnd, IDC_MY_EDIT_SERVER);
hwndUrl = GetDlgItem(hwnd, IDC_MY_EDIT_URL);
int SernerLen = GetWindowTextLength(hwndServer);
int UrlLen = GetWindowTextLength(hwndUrl);
SernerLen += 1;
UrlLen += 1;
LPTSTR szServer = new TCHAR[SernerLen];
LPTSTR szUrl = new TCHAR[UrlLen];
GetWindowText(hwndServer, szServer, SernerLen);
GetWindowText(hwndUrl, szUrl, UrlLen);
HTTPCONNECT HttpInfo = {0};
// HttpInfo.lpszServer = szServer;
// HttpInfo.lpszUrl = szUrl;
HttpInfo.lpszServer = TEXT("img.szhk.com");
HttpInfo.lpszUrl = TEXT("/Image/2009/08/25/200908250952220861.gif");
HttpInfo.lpszVerb = TEXT("GET");
HttpInfo.pvPostData = NULL;
HttpInfo.dwPostDataLen = 0;
if(!ConnectProcess(&HttpInfo, MainWnd::OnConnectInternet, this))
{
DEBUGMSG(TRUE, (TEXT("ConnectProcess failed\n")));
}
delete []szServer;
szServer = NULL;
delete []szUrl;
szUrl = NULL;
}
void MainWnd::OnWap(HWND hwnd, HWND hwndCtl, UINT codeNotify)
{
DEBUGMSG(TRUE, (TEXT("MainWnd::OnWap\n")));
if(m_Mgr != NULL)
{
m_Mgr->ConnectWap();//连接wap
}
}
//指针函数回调
int MainWnd::OnConnectInternet(PBYTE pbRecvBuf, DWORD dwLen, LPVOID pvContext)
{
return 0;
}
void MainWnd::OnDownLoad(HWND hwnd, HWND hwndCtl, UINT codeNotify)
{
TCHAR szFileName[MAX_PATH] = {0};//存放文件路径及文件名
if (!xfGetExePath(szFileName, MAX_PATH))
{
return ;
}
TCHAR szURL[MAX_PATH] = FILEDOWNNAME;
// GetDlgItemText(hwnd, IDC_MY_EDIT_URL, szURL, MAX_PATH);
_tcscat(szFileName, (_tcsrchr(szURL, TEXT('/'))+1));
DEBUGMSG(TRUE, (TEXT("URL: %s\n"), szURL));
DEBUGMSG(TRUE, (TEXT("FileName: %s\n"), szFileName));
// 下载文件
HRESULT hr = URLDownloadToFile(NULL, szURL, szFileName, 0, NULL);
if (SUCCEEDED(hr))
{
// xfMB(TEXT("URLDownloadToFile OK!"));
//画图
HDC hdc = GetDC(hwnd);
HDC hMemdc = CreateCompatibleDC(hdc);
HBITMAP hOldBmp = NULL;
BITMAP bmp = {0};
HBITMAP pbmp = SHLoadImageFile(szFileName);
hOldBmp = SelectBitmap(hMemdc, pbmp);
GetObject(pbmp, sizeof(BITMAP), &bmp);
StretchBlt(hdc, 10, 160, 150, 100, hMemdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
SelectBitmap(hMemdc, hOldBmp);
DeleteDC(hMemdc);
hMemdc = NULL;
ReleaseDC(hwnd, hdc);
}
else
{
xfMB(TEXT("URLDownloadToFile failed!"));
}
}
评论0