//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include "Unit_Formmain.h"
#include "unit_removablecore.h"
#include "unit_diskinfo.h"
#include "unit_about.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm_removable *Form_removable;
TRemovableCore *rmvcore = NULL;
//---------------------------------------------------------------------------
__fastcall TForm_removable::TForm_removable(TComponent* Owner)
: TForm(Owner)
{
diskname = '\0';
currentState = 0;
rmvcore = new TRemovableCore();
rmvcore->setDeleAddRemovable(OnAddRemovable);
rmvcore->setDeleRemoveRemovable(OnRemoveRemovable);
}
//---------------------------------------------------------------------------
void __fastcall TForm_removable::OnAddRemovable(char name)
{
diskname = name;
currentState = 1;
Timer1->Enabled = true;
int size = removableDisk.size();
removableDisk.push_back(name);
refresh();
}
void __fastcall TForm_removable::OnRemoveRemovable(char name)
{
diskname = name;
currentState = 2;
Timer1->Enabled = true;
int i = 0;
for(i = 0;i < removableDisk.size();i++)
if(name == removableDisk[i])
break;
removableDisk.erase(&removableDisk.at(i));
int size = removableDisk.size();
refresh();
}
void __fastcall TForm_removable::FormShow(TObject *Sender)
{
rmvcore->init();
}
String TForm_removable::ByteToFloatString(double byte)
{
String floatstring;
float num = byte;
int unitflag = 0;
while(num >= 1024.0f)
{
num /= 1024.0f;
unitflag++;
}
char tmpchr[256];
sprintf(tmpchr,"%4.2f",num);
floatstring = tmpchr;
switch(unitflag)
{
case 0://字节
floatstring += 'B';
break;
case 1://K字节
floatstring += "KB";
break;
case 2://M字节
floatstring += "MB";
break;
case 3://G字节
floatstring += "GB";
break;
default:
break;
}
return floatstring;
}
//---------------------------------------------------------------------------
void TForm_removable::addRemovableDisk(char name)
{
String disk = String(name); //驱动器号
String filesystem = TDiskInfo::GetFileSystemString(name); //磁盘文件系统
String volume = TDiskInfo::GetVolumeString(name); //磁盘卷表
double tsize = TDiskInfo::GetTotalSize(name);//磁盘总大小
String totalsize = tsize;
totalsize += '(';
totalsize += ByteToFloatString(tsize);
totalsize += ')';
double usize = TDiskInfo::GetUsedSize(name); //获取已使用空间
String usedsize = usize;
usedsize += '(';
usedsize += ByteToFloatString(usize);
usedsize += ')';
double fsize = TDiskInfo::GetFreeSize(name); //获取剩余空间
String freesize = fsize;
freesize += '(';
freesize += ByteToFloatString(fsize);
freesize += ')';
String corp;
addInfo(disk,filesystem,volume,totalsize,usedsize,freesize,corp);
}
void TForm_removable::addInfo(String name,String filesystem,String volume,String totalsize
,String usedsize,String restsize,String corp)
{
TStrings *s = new TStringList();
s->Add(name);
s->Add(filesystem);
s->Add(volume);
s->Add(totalsize);
s->Add(usedsize);
s->Add(restsize);
TListItem *item = ListView_Disk->Items->Add();
item->SubItems = s;
s = NULL;
}
//刷新
void TForm_removable::refresh()
{
ListView_Disk->Items->Clear();
//添加所有磁盘信息
for(int i = 0;i < removableDisk.size();i++)
addRemovableDisk(removableDisk[i]);
}
void TForm_removable::SaveToFile(String filename)
{
//判断文件是否存在
FILE* fp = fopen(filename.c_str(),"r");
if (NULL != fp)
{
if(IDNO == MessageBox(this->Handle,"指定文件已存在,确定要覆盖吗?","提示",MB_YESNO))
return;
}
int num = ListView_Disk->Items->Count;
fp = fopen(filename.c_str(),"w");
for(int i = 0;i < num;i++)
{//保存所有数据
String tempstr;
TStrings *s = ListView_Disk->Items->Item[i]->SubItems;
for(int j = 0;j < s->Count;j++)
{
if(String("") != s->Strings[j])
tempstr += s->Strings[j];
else
tempstr += String("(无信息)");
tempstr += String(',');
}
fprintf(fp,"%s\n",tempstr);
}
fclose(fp);
MessageBox(this->Handle,"信息保存完成。","提示",MB_OK);
}
void __fastcall TForm_removable::Button_SaveClick(TObject *Sender)
{
//保存数据
TSaveDialog *od = new TSaveDialog(this->Owner);
od->Filter = "(*.rtf)|*.rtf";
od->DefaultExt = "rtf";
if(true == od->Execute())
{//确定
String filename = od->FileName;
SaveToFile(filename);
}
delete od;od = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm_removable::NotifyIcon(TMessage& Msg)
{
POINT MousePos;
switch(Msg.LParam)
{
case WM_RBUTTONUP:
if (GetCursorPos(&MousePos))
{
PopupMenu_NotifyRButton->PopupComponent = Form_removable;
SetForegroundWindow(Handle);
PopupMenu_NotifyRButton->Popup(MousePos.x, MousePos.y);
}
break;
case WM_LBUTTONDBLCLK:
this->Show();
break;
default:
break;
}
TForm::Dispatch(&Msg);
}
bool __fastcall TForm_removable::TrayMessage(DWORD dwMessage)
{
NOTIFYICONDATA tnd;
PSTR pszTip;
pszTip = "可移动式设备";//TipText();
tnd.cbSize= sizeof(NOTIFYICONDATA); //结构的大小
tnd.uCallbackMessage =WM_NOTIFYICON; //自定义回调消息,在头文件中声明
tnd.hWnd = Handle; //接受回调消息的窗口句柄
tnd.uID = WM_NOTIFYICON; //图标标志号
lstrcpyn(tnd.szTip,pszTip,sizeof(tnd.szTip));
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP; //指定以下三个参数哪个包含有效数据
if (dwMessage == NIM_MODIFY)
{
if(0 != currentState)
{
String infotitle = "移动式设备消息";
String info = "驱动器 ";
info += diskname;
info += String(":");
if (1 == currentState)
info += "插入";
else if (2 == currentState)
info += "弹出";
lstrcpy(tnd.szInfo, info.c_str());
lstrcpy(tnd.szInfoTitle, infotitle.c_str());
tnd.uTimeout = 100;
tnd.dwState = NIS_SHAREDICON;
tnd.dwStateMask = 0;
tnd.dwInfoFlags = NIF_INFO;
tnd.uFlags = tnd.uFlags | NIF_INFO;
}
tnd.hIcon = (HICON)IconHandle(); //取得图标句柄
if (pszTip)
lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
else tnd.szTip[0] = '\0';
}
else
{
tnd.hIcon = NULL;
tnd.szTip[0] = '\0';
}
return (Shell_NotifyIcon(dwMessage, &tnd));
}
HICON __fastcall TForm_removable::IconHandle(void)
{
switch(currentState)
{
case 0://普通状态
return Image_Normal->Picture->Icon->Handle;
case 1://插入状态
return Image_Insert->Picture->Icon->Handle;
case 2://拔出状态
return Image_Remove->Picture->Icon->Handle;
default:
break;
}
}
bool TForm_removable::IsInstanceExists()
{
if(!CreateMutex(NULL,TRUE,"No Previous Instance!"))
{
MessageBox(NULL,"创建互斥对象失败。","提示",MB_OK);
exit(0);
}
if(GetLastError() == ERROR_ALREADY_EXISTS)
return true;
return false;
}
void __fastcall TF
没有合适的资源?快使用搜索试试~ 我知道了~
用C++设计USB程序接口
共49个文件
~cpp:7个
obj:7个
cpp:7个
4星 · 超过85%的资源 需积分: 9 42 下载量 103 浏览量
2009-03-13
11:55:26
上传
评论
收藏 1.22MB RAR 举报
温馨提示
1.当Usb设备(如U盘插入)USB接口时,在windows的系统托盘里出现一个图标并弹出一个提示(如信息为:拔出时提示:xxx设备已拔出) 2.单击提示信息,弹出显示USB设备信息的界面。设备信息包括:U盘厂商信息等。
资源推荐
资源详情
资源评论
收起资源包目录
removeab.rar (49个子文件)
removeable
unit_simplethread.~cpp 964B
removeable.~bpr 4KB
Unit_RemovableCore.obj 169KB
ICON_REMOVE.ico 2KB
Unit_Formmain.obj 184KB
unit_simplethread.h 844B
Unit_Formmain.h 3KB
Unit_About.cpp 712B
unit_simplethread.cpp 964B
Unit_Formmain.cpp 10KB
removeable.rar 298KB
Unit_About.obj 34KB
removeable.bpr 4KB
Unit_RemoveableCore.h 1KB
Unit_DiskInfo.obj 105KB
Unit_RemoveableCore.~h 1KB
Unit_RemoveableCore.~cpp 2KB
Unit_RemoveableCore.obj 168KB
Unit_Formmain.ddp 51B
removeable.cpp 1KB
Unit_DiskInfo.h 763B
Unit_RemoveableCore.cpp 2KB
Unit_Formmain.~h 3KB
Unit_Formmain.dfm 18KB
removeable.obj 16KB
ICON_INSERT.ico 2KB
Unit_About.~cpp 712B
ICON_NORMAL.ico 2KB
Unit_About.~h 871B
removeable.exe 720KB
Thumbs.db 7KB
removeable.res 2KB
Unit_Formmain.~dfm 18KB
unit_simplethread.~h 843B
Unit_DiskInfo.~h 763B
Unit_DiskInfo.~cpp 3KB
Unit_RemovableCore.~h 1KB
Unit_About.dfm 977B
Unit_RemovableCore.cpp 2KB
Unit_Formmain.~ddp 51B
unit_simplethread.obj 16KB
Unit_DiskInfo.cpp 3KB
Unit_Formmain.~cpp 10KB
Unit_About.h 871B
Unit_RemovableCore.~cpp 2KB
removeable.tds 3.38MB
removeable.~cpp 1KB
Unit_About.~dfm 977B
Unit_RemovableCore.h 1KB
共 49 条
- 1
yalishanda1213
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页