wininet源代码获取例子

wininet源代码获取例子

#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet")
#define ID_URLEDITBOX    1
#define ID_GETBUTTON    2
#define ID_SHOWEDITBOX    3
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("BtnLook") ;
    HWND         hwnd ;
    MSG          msg ;
    WNDCLASS     wndclass ;
    wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = szAppName ;
    if (!RegisterClass (&wndclass))
    {
        MessageBox (NULL, TEXT ("This program requires Windows NT!"),
            szAppName, MB_ICONERROR) ;
        return 0 ;
    }
    hwnd = CreateWindow (szAppName, TEXT ("WinINet源代码获取"),
        WS_OVERLAPPEDWINDOW,
        GetSystemMetrics (SM_CXSCREEN) / 4, GetSystemMetrics (SM_CYSCREEN) / 4,
        GetSystemMetrics (SM_CXSCREEN) / 2, GetSystemMetrics (SM_CYSCREEN) / 2,
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndUrlEdt,hwndGetBtn,hwndShowEdt;
    HINTERNET hConnect,hRequest,hSession;
    LPTSTR lpszHost;
    char szBuffer[1024*1024/10];
    TCHAR szStr[100];
    DWORD dwSize=0;
    DWORD dwDownloaded;
    static LPCTSTR pAcceptType[] = { TEXT("Accept: */*"), NULL };
    switch (message)
    {
    case WM_CREATE:
        hwndUrlEdt = CreateWindow (TEXT ("EDIT"), NULL,
            WS_CHILD | WS_VISIBLE |  WS_BORDER | ES_LEFT | ES_AUTOHSCROLL    ,
            20,20,500,20, hwnd, (HMENU)ID_URLEDITBOX,
            ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
        hwndGetBtn = CreateWindow (TEXT ("BUTTON"), TEXT("GO"),
            WS_CHILD | WS_VISIBLE |  BS_DEFPUSHBUTTON   ,
            530,20,30,20, hwnd, (HMENU)ID_GETBUTTON,
            ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
        hwndShowEdt = CreateWindow (TEXT ("EDIT"), TEXT(""),
            WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |  ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE  ,
            20,60,600,250, hwnd, (HMENU)ID_SHOWEDITBOX,
            ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
        return 0;
    case WM_COMMAND:
        if(LOWORD(wParam) == ID_GETBUTTON)
        {
            GetWindowText(hwndUrlEdt,szStr,sizeof(szStr)/sizeof(TCHAR));
            hSession = InternetOpen(TEXT("testWinINet"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
            hConnect = InternetOpenUrl(hSession,szStr,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
            ZeroMemory(szBuffer,sizeof(szBuffer));
            while (InternetReadFile(hConnect,szBuffer,sizeof(szBuffer),&dwDownloaded))
            {
                if (0==dwDownloaded) break;
                szBuffer[dwDownloaded]=0;
            }
            SetWindowText(hwndShowEdt,szBuffer);
            ZeroMemory(szBuffer,sizeof(szBuffer));
        }
        return 0;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}

程序缺点很多,1是点击“GO”之后,获取源代码的时候会卡住,因为没有用多线程,
2,存储源代码的内存空间受到了限制,csdn上面的人说受vc编译器堆栈大小的限制,超出了就stack overflow了。目前我只能使用临时文件存储的方法来解决。
3是子窗口都是使用代码写的,所以字体都是默认的fixedsys“system”,中文字体十分难看。
4是字符串如果不是gb2312和纯英文的,其他字符串全部乱码,如上图就是一个最好的例证。

说了这么多缺点,说出来以后“深造”后再修正吧。嘎嘎~~

发表评论

电子邮件地址不会被公开。 必填项已用*标注