|
楼主 |
发表于 2010-9-25 15:30:02
|
显示全部楼层
最新升级版( 基于GUI操作界面 ) 源码:
#define IDC_DISK0 0x1000
#define IDC_DISK1 0x1001
#define IDC_DISK2 0x1002
#define IDC_LIST 0x2000
#define IDC_LINK 0x2001
#define IDC_UNLINK 0x2002
#define IDC_GROUP_DISK 0x3000
#define IDC_GROUP_OPS 0x3001
static int use=0;
static int con=0;
static int disk=0;
static RESULT usb_con_proc(HWND hwnd,U32 msg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HANDLE hfile;
RECT rc,m_rc[4];
char buf[128],*p;
int i,x,y;
////
switch(msg)
{
case WM_CREATE:
x=4;
y=4;
GetClientRect(hwnd,&rc);
CreateWindow("groupbox",hwnd,x,y,rc.dx-4-4,rc.dy-40,0,IDC_GROUP_DISK,"磁盘列表");
GetClientRect(GetWindowItem(hwnd,IDC_GROUP_DISK),&rc);
CreateWindow("listbox",GetWindowItem(hwnd,IDC_GROUP_DISK),2,2,rc.dx-2*2,rc.dy-2*2,0,IDC_LIST,"List");
GetClientRect(hwnd,&rc);
y=rc.dy-28;
GetMatrixRectangle(m_rc,x,y,rc.dx-4-4,24,4,1,8,2);
CreateWindow("button",hwnd,m_rc[0].x,m_rc[0].y,m_rc[0].dx,m_rc[0].dy,0,IDC_LINK,"连接");
CreateWindow("button",hwnd,m_rc[1].x,m_rc[1].y,m_rc[1].dx,m_rc[1].dy,0,IDC_UNLINK,"断开");
CreateWindow("button",hwnd,m_rc[2].x,m_rc[2].y,m_rc[2].dx,m_rc[2].dy,0,3,"<<");
CreateWindow("button",hwnd,m_rc[3].x,m_rc[3].y,m_rc[3].dx,m_rc[3].dy,0,4,">>");
i=0;
for(i=0;i<_DRIVES;i++)
{
if(GetDriveName(i))
{
StrPrintf(buf,"%d: %s - %dMB",i,GetDriveName(i),(GetDriveSectorCount(i)*GetDriveSectorSize(i))>>20);
SendMessage(GetWindowItem(GetWindowItem(hwnd,IDC_GROUP_DISK),IDC_LIST),LB_ADDSTRING,i,(LPARAM)buf);
}
}
//SetTimer(hwnd,0x3000,500,0);
break;
////
case WM_COMMAND:
{
U16 code,id;
////
code =HIWORD(wParam);
id =LOWORD(wParam);
switch(id)
{
case IDC_LINK:
{
if(code==BN_UP)
{
int cur;
if(con==0)
{
cur =SendMessage(GetWindowItem(GetWindowItem(hwnd,IDC_GROUP_DISK),IDC_LIST),LB_GETCURSEL,0,0);
StrPrintf(buf,"是否要连接到: %s ?",GetDriveName(cur));
if(MessageBox(hwnd,buf,"信息",MB_YESNO|MB_ICONQUESTION)==IDYES)
{
disk=cur;
if(USB_MassStorageEntry(disk))
{
con=1;
StrPrintf(buf,"USB连接 [已连接到:%s]",GetDriveName(disk));
SetWindowText(hwnd,buf);
}
else
{
con=0;
if(MessageBox(hwnd,"驱动器错误","出错信息",MB_OK|MB_ICONERROR)==IDOK)
{
SendMessage(hwnd,WM_CLOSE,0,0);
}
}
InvalidateRect(hwnd,0,0);
}
}
else
{
MessageBox(hwnd,"USB已连接,如果需要连接到其它磁盘,请先断开当前连接.","信息",MB_OK|MB_ICONERROR);
}
}
}
break;
////////
case IDC_UNLINK:
{
if(code==BN_UP)
{
if(con==1)
{
StrPrintf(buf,"确定要断开 %s 连接吗?",GetDriveName(disk));
if(MessageBox(hwnd,buf,"信息",MB_YESNO|MB_ICONQUESTION)==IDYES)
{
USB_MassStorageExit(disk);
con=0;
StrPrintf(buf,"USB连接 [已断开]");
SetWindowText(hwnd,buf);
InvalidateRect(hwnd,0,0);
}
}
else
{
MessageBox(hwnd,"当前没有连接!","信息",MB_OK|MB_ICONWARNING);
}
}
}
break;
////////
}
}
////
case WM_PAINT:
hdc=BeginPaint(hwnd);
EndPaint(hwnd,hdc);
break;
////
case WM_CLOSE:
if(con==1)
{
USB_MassStorageExit(disk);
con=0;
}
//KillTimer(hwnd,0x3000);
DestroyWindow(hwnd);
break;
////
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
/*============================================================================*/
int USBConnect(void *p)
{
HWND hwnd;
MSG msg;
WNDCLASS wcls;
////
if(use==1) return;
use=1;
wcls.Name ="USB连接";
wcls.Style =WS_CAPTION|WS_BORDER_DUAL|WS_VISIBLE;
wcls.ExStyle =0;
wcls.ExData =0;
wcls.hCursor =0;
wcls.BkColor =RGB(128,128,128);
wcls.Proc =usb_con_proc;
hwnd =CreateMainWindow(&wcls,20,ScreenHeight()-320,200,160);
ShowWindow(hwnd,SW_SHOW);
while(1)
{
if(PeekMessage(&msg,hwnd,PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(con==1)
{
DiskOps();
}
}
use=0;
}
/*=========================================================================================*/ |
|