开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1567|回复: 0
收起左侧

[源码分享] 纯sdk创建能接收文件拖放的窗体

[复制链接]
结帖率:50% (2/4)
发表于 2012-8-20 22:42:30 | 显示全部楼层 |阅读模式   湖南省长沙市
#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,
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n
        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;
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表