程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 純sdk創建能接收文件拖放的窗體

純sdk創建能接收文件拖放的窗體

編輯:vc教程
 #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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved