Win32GUICode

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Win32GUICode相关的知识,希望对你有一定的参考价值。

// WIN32DownLoad.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WIN32DownLoad.h"

#include <wininet.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmsystem.h>
#include <commdlg.h>
#include <Shlwapi.h>



#include <commctrl.h>        //InitCommonControls
 
#pragma comment(lib, "comctl32") //InitCommonControls

#pragma comment(lib ,"Wininet.lib")
#pragma comment(lib,"winmm.lib")



LRESULT CALLBACK WindowProc(
                            HWND hwnd,      // handle to window
                            UINT uMsg,      // message identifier
                            WPARAM wParam,  // first message parameter
                            LPARAM lParam   // second message parameter
                            );

TCHAR classname[80]= _T("download");
HWND hbutton1,hbutton2,heidt;
HWND hwnd,hwndPB ;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     

    
    WNDCLASS ex;
    ex.cbClsExtra = 0;
    ex.cbWndExtra = 0;
    ex.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
    ex.hCursor    = LoadCursor(NULL,IDC_ARROW);
    ex.hIcon      = LoadIcon(NULL,IDI_APPLICATION );
    ex.hInstance  = hInstance ;
    ex.lpfnWndProc = WindowProc ;
    ex.lpszClassName = classname ;
    ex.lpszMenuName  = NULL ;
    ex.style    = CS_VREDRAW|CS_HREDRAW ;


    if( RegisterClass(&ex) == 0 )
    {
        MessageBox(NULL,_T("注册窗口失败!"),_T("提示"),MB_OK);
    }

    hwnd= CreateWindow(classname,_T("技术"),WS_OVERLAPPEDWINDOW,320,0,700,700,NULL,NULL,hInstance,NULL);

    if( hwnd == NULL )
    {
        MessageBox(NULL,_T("创建窗口失败"),_T("error"),MB_OK);
    }

    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

    MSG msg;

    while(GetMessage(&msg,NULL,NULL,NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }



    return 0;
    

    
}


DWORD WINAPI PBThreadProc( LPVOID lpParameter )
{
    //CHAR szHead = (CHAR )lpParameter;

    //进度条 start
    
    
    ShowWindow( hwndPB , SW_SHOW );        //显示进度条

    ShowWindow( hbutton2 , SW_SHOW );  //显示百分比控件

    PBRANGE range;                        //进度条的范围
 
    
    SendMessage( hwndPB, PBM_SETRANGE,    //设置进度条的范围
 
        (WPARAM)0, (LPARAM)(MAKELPARAM(0,100)) );
 
    
    SendMessage( hwndPB, PBM_GETRANGE,    //获取进度条的范围
 
        (WPARAM)TRUE,                    //TRUE 表示返回值为范围的最小值,FALSE表示返回最大值
 
        (LPARAM)&range );
    
    //进度条 end


    DWORD dwFlags;
    if(!InternetGetConnectedState(&dwFlags,0))
    {
        MessageBox(hwnd,_T("没有网络连接!"),_T("提示"),MB_OK);
        return 0;
    }

    TCHAR strAgent[64];
    swprintf(strAgent,_T("Agent%ld"),timeGetTime());
    HINTERNET hopen;
    if(!(dwFlags&INTERNET_CONNECTION_PROXY)){

        hopen=InternetOpen(strAgent,INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,NULL,NULL,0);

    }else{
        hopen=InternetOpen(strAgent,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);

    }
    if(!hopen){
        MessageBox(hwnd,_T("打不开网络连接!"),_T("提示"),MB_OK);
        return 0;
    }

    TCHAR szHead[]= _T("Accept: */*\r\n\r\n");
    void * szTemp[16384];
    HINTERNET hConnect;
    FILE *fp;
    
    TCHAR  szUrlAdd[255]; 
    GetWindowText( heidt , szUrlAdd , 255 );

    

    if(!(hConnect=InternetOpenUrl(hopen,szUrlAdd,szHead,wcslen(szHead),INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_PRAGMA_NOCACHE,0))){
        MessageBox(hwnd,_T("打开网络地址失败!"),_T("提示"),MB_OK);
        return 0;
    }

    TCHAR pBuf[MAX_PATH];                                               //存放路径的变量
    GetCurrentDirectory(MAX_PATH,pBuf);                   //获取程序的当前目录
    wcscat(pBuf, wcsrchr( szUrlAdd ,/) );

    fp=_wfopen( pBuf ,_T("wb+"));

    if(fp==NULL){
        MessageBox(hwnd,_T("打开本地文件失败!"),_T("提示"),MB_OK);
        return 0;
    }

    DWORD dwByteToRead=0;
    DWORD dwSizeOfRq=4;
    DWORD dwBytes=0;
    DWORD sum=0;
    DWORD dwSize=0;
    if(!HttpQueryInfo(hConnect,HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER,(LPVOID)&dwByteToRead,&dwSizeOfRq,NULL)){
        
        dwByteToRead=0;

    }
    
    EnableWindow( hbutton1 , FALSE );
    SetDlgItemText(hwnd,1001,_T("正在下载..."));
    TCHAR sumdata[64];
    
    do{
        
        
        if(!InternetReadFile(hConnect,szTemp,16384,&dwSize)){
            InternetCloseHandle(hopen);
            fclose(fp);
            MessageBox(hwnd,_T("网络中断!"),_T("提示"),MB_OK);
            return 0;
        }
        if(dwSize==0){
            break;
        }else{
            sum+=dwSize;
            fwrite(szTemp,dwSize,1,fp);
        
            swprintf(sumdata,_T("%d %%"),(sum*100)/dwByteToRead);
            
            //设置进度条当前值
            SendMessage( hwndPB, PBM_SETPOS, (WPARAM)(sum*100)/dwByteToRead, (LPARAM)0 );
            
            SetDlgItemText(hwnd,1001,sumdata);
            SetDlgItemText(hwnd,1002,sumdata);
            
        }
    }while(TRUE);

    EnableWindow( hbutton1 , TRUE );
    SetDlgItemText(hwnd,1001,_T("下载"));
    InternetCloseHandle(hopen);
    fclose(fp);

    MessageBox(hwnd,_T("下载成功"),_T("提示"),MB_OK);

    SendMessage( hwndPB, PBM_SETPOS, (WPARAM)range.iLow, (LPARAM)0 ); //将进度条复位

//     ShowWindow( hwndPB , SW_HIDE );        //隐藏进度条
// 
//     ShowWindow( hbutton2 , SW_HIDE );  //隐藏 百分比控件
    
    return 0;
}






LRESULT CALLBACK WindowProc(
                            HWND hwnd,      // handle to window
                            UINT uMsg,      // message identifier
                            WPARAM wParam,  // first message parameter
                            LPARAM lParam   // second message parameter
                            )
{
    switch(uMsg)
    {
    case WM_DESTROY:
        {
            PostQuitMessage(0);
        }
        break;
        
    case WM_CREATE:
        {
            hbutton1= CreateWindow(_T("BUTTON"),_T("下载"),WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,550,200,100,30,hwnd,(HMENU)1001,NULL,NULL);
            hbutton2= CreateWindow(_T("Static"),_T("0 %"),WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,530,260,100,30,hwnd,(HMENU)1002,NULL,NULL);
            heidt= CreateWindow(_T("EDIT"),NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,70,200,450,30,hwnd,NULL,NULL,NULL);    
            SetWindowText( heidt , _T("http://dl.client.baidu.com/BaiduPlayer/BaiduPlayer1.19.0.78_1.exe") );
            
            hwndPB = CreateWindowEx( //创建进度条
 
                0,
 
                PROGRESS_CLASS,
 
                NULL,
 
                WS_CHILD | WS_VISIBLE,
 
                70,260,450,30,            //位置和大小在WM_SIZE中设置
 
                hwnd,
 
                (HMENU)0,
 
                ((LPCREATESTRUCT)lParam)->hInstance,
 
                NULL);

            //隐藏进度条 和 百分比 控件
//             ShowWindow( hwndPB , SW_HIDE );
//             ShowWindow( hbutton2 , SW_HIDE );
        }
        break;

    case WM_COMMAND:
        {
            //wParam HIWORD(wParam) LOWORD(wParam) 高位是事件 低位才是按钮
            switch(HIWORD(wParam))
            {
            case BN_CLICKED: //这里处理是单击
                {
                    if(LOWORD(wParam) == 1001)
                    {

                        TCHAR buff[80]=_T("");
                        ::GetWindowText(heidt,buff,80);                                    
                    
                        
                        //创建线程
                        CreateThread(
                            NULL,
                            0,
                            (LPTHREAD_START_ROUTINE)PBThreadProc,
                            buff,
                            0,
                            0
                            );


                    }
                    if(LOWORD(wParam) == 1002)
                    {
                        PostQuitMessage(0);
                    }
                }
                break;
            }

        }
        break;
    }
    
    
    
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

 转自:http://www.cctry.com/thread-238862-1-1.html

以上是关于Win32GUICode的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Win32 控制台应用程序中将 argv[] 设置为不区分大小写?

win32编程里,ransMessage()和DispatchMessage()是干嘛的

Win10蓝屏win32k power watchdog timeout 蓝屏代码0x0000019C

win32是啥意思

LaunchFullTrustProcessForCurrentAppAsync杀死win32应用程序

rundll32执行js代码&Win32/Poweeliks