文件拖放显示信息

#include <windows.h>

int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow );
BOOL InitApplication( HINSTANCE hInstance );
BOOL InitInstance( HINSTANCE hInstance,int nCmdShow );
LRESULT CALLBACK MainWndProc( HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam );
BOOL ShowFileInfo( HWND hWnd,HDC hDC,HDROP hDropInfo );

int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow )
{
    MSG msg;

    if( !InitApplication( hInstance ) ) return FALSE;
    if( !InitInstance( hInstance,nCmdShow ) ) return FALSE;
    while( GetMessage( &msg,NULL,0,0 ) ) {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
    return msg.wParam;
}

BOOL InitApplication( HINSTANCE hInstance )
{
    WNDCLASS wc;

    wc.style = 0;
    wc.lpfnWndProc = ( WNDPROC )MainWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL,IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL,IDC_ARROW );
//    wc.hbrBackground = GetStockObject( WHITE_BRUSH );
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "DropFileClass";

    return RegisterClass( &wc );
}

BOOL InitInstance( HINSTANCE hInstance,int nCmdShow )
{
    HWND hWnd;

    hWnd = CreateWindowEx(
        0,
        "DropFileClass",
        "DropFile Demo",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    if( !hWnd ) return FALSE;
    ShowWindow( hWnd,nCmdShow );
    UpdateWindow( hWnd );
    return TRUE;
}

LRESULT CALLBACK MainWndProc( HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam )
{
    HDC hDC;

    switch( uMsg ) {
        case WM_CREATE:
            DragAcceptFiles( hWnd,TRUE );
            break;
        case WM_DROPFILES:
            HDROP hDropInfo;
            hDropInfo = ( HDROP )wParam;
//          hDropInfo = ( HANDLE )wParam;
            hDC = GetDC( hWnd );
            ShowFileInfo( hWnd,hDC,hDropInfo );
            DragFinish( hDropInfo );
            ReleaseDC( hWnd,hDC );
            break;
        case WM_DESTROY:
            DragAcceptFiles( hWnd,FALSE );
            PostQuitMessage( 0 );
            break;
        default:
            return DefWindowProc( hWnd,uMsg,wParam,lParam );
    }
    return 0;
}

BOOL ShowFileInfo( HWND hWnd,HDC hDC,HDROP hDropInfo )
{
    POINT pt;
    WORD cFiles;
    HINSTANCE hCurrentInst;
    HICON hIcon;
    SHFILEINFO sfi;
    char lpszFile[ 80 ];
    char szBuff[ 1024 ];

    DragQueryPoint( hDropInfo,&pt );
    cFiles = DragQueryFile( hDropInfo,( UINT )0xFFFFFFFF,( LPTSTR )NULL,( UINT )NULL );
    if( cFiles > 1 ) {
        wsprintf( szBuff,"DragFileNumber: %d",( int )cFiles );
        TextOut( hDC,pt.x,pt.y,szBuff,lstrlen( szBuff ) );
        return FALSE;
    }
    else {
        DragQueryFile( hDropInfo,0,lpszFile,sizeof( lpszFile ) );
        if( SHGetFileInfo( lpszFile,0,&sfi,sizeof( SHFILEINFO ),SHGFI_DISPLAYNAME | SHGFI_TYPENAME ) ) {
            memset( szBuff,0,sizeof( szBuff ) );
            wsprintf( szBuff,"Name: %s Type: %s",sfi.szDisplayName,sfi.szTypeName );
        }
        hCurrentInst = ( HINSTANCE )GetWindowLong( hWnd,GWL_HINSTANCE );
        hIcon = ExtractIcon( hCurrentInst,lpszFile,0 );
        if( hIcon == NULL || hIcon == ( HICON )1 ) {
            TextOut( hDC,pt.x,pt.y,szBuff,lstrlen( szBuff ) );
        }
        else {
            DrawIcon( hDC,pt.x,pt.y,hIcon );
            TextOut( hDC,pt.x,pt.y + 32,szBuff,lstrlen( szBuff ) );
        }
    }
    return TRUE;
}

你可能感兴趣的:(文件拖放显示信息)