|
Article Updated - the references to ImageViewer are now gone.
Thanks again for the update,
Iain.
|
|
|
|
|
Hi
Nice code
How 2 detect if device(like usb mouse/keyboard or barcode scanner) inserted?
Can u help?
thanx
Have a nice code day
|
|
|
|
|
A quick play with plugging my USB mouse shows I get
WM_DEVICECHANGE, with WPARAM=DBT_DEVNODES_CHANGED.
Looking this up on MSDN, (and checking myself) LPARAM always equals 0, so no help there.
But if you go look for the original question / answer by Matthew Faithful (see Background in article), that may give you more ideas.
Failing that, you may need to think about your core problem. Why does your software care how many mice the uses uses? etc?
Iain.
|
|
|
|
|
thanx 4 reply
I got same results when usb mouse plugged.
The main issue that i interested for is the BARCODE SCANNER not mouse/keyboard...etc
Okay, i'll get the DBT_DEVNODES_CHANGED notification message,but there is more data must arrives to detects the port that the device connected to and the device informations...etc
Realy, I need to learn how can i managing(Read from) the BARCODE SCANNER set .
Thanx
|
|
|
|
|
Short, sweet, and seems to work well with DVD drives and card readers. It doesn't detect my camera, but I suppose Vista's image import wizard (gotta love+hate it) could be getting in the way somehow.
|
|
|
|
|
Would be interesting to know if explorer shows a drive icon, or if your camera is somehow being shown as a TWAIN device (or such like).
Just buy me a camera like yours, and I'll be happy to investigate further!
Yours hasn't-got-around-to-deciding-how-simple-or-fancy-a-camera-he-wants-so-hasn't-bought-one-yet,
Iain.
|
|
|
|
|
Iain Clarke wrote: Just buy me a camera like yours, and I'll be happy to investigate further!
Sorry, I can't buy you a camera like mine. Even if I could, you probably wouldn't want it. Most people think it's too big and bulky, but I like the pictures it takes
Anyway, back on topic: it shows up in My Computer as a drive icon. Actually, it's a cute little camera icon listed under "portable devices"
Its memory is a Compact Flash card, and if my memory serves me correctly, Compact Flash cards provide an IDE drive interface. At any rate, I'm pretty sure it isn't a Twain device. I guess I forgot to mention that in my previous post. Sorry!
|
|
|
|
|
I think you've possibly answered your own question - it shows up under "portable devices", rather than straight under My Computer (or equivalent in vista) with a drive letter. Or am I wrong, and it does get a drive letter?
I'm guessing if you debug, that DBT_DEVICEARRIVAL is sent, and maybe volume->dbcv_devicetype == DBT_DEVTYP_VOLUME, but if volume->dbcv_unitmask doesn't have a bit set, then I won't pop it in the list of adding / not-adding.
Iain.
|
|
|
|
|
Hi Iain. Nice article. I was just wondering about this topic last week.
Re: Drive Icons
There is no need to make up a bitmap and your own image list. The image list built into Windows Explorer is readily accessible. First though, we need 3 helper functions to make the sample code more readable.
// GetShellImageList() - returns a handle to the shell icon image list
HIMAGELIST GetShellImageList()
{
SHFILEINFO sfi;
return (HIMAGELIST) SHGetFileInfo( "C:\\", 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
}
// GetShellIcon() - returns the shell icon subscript associated with a given pathname
// NOTE: I believe a trailing backslash is necessary
int GetShellIcon(LPCTSTR pszPath, UINT uFlags = 0 )
{
SHFILEINFO sfi;
SHGetFileInfo( pszPath, 0, &sfi, sizeof(sfi), uFlags|SHGFI_SYSICONINDEX|SHGFI_SMALLICON );
return sfi.iIcon;
}
// GetVolumeName() - returns the volume name of a drive
// NOTE: I believe a trailing backslash necessary. eg. "C:\\".
CString GetVolumeName(CString sDrive)
{
char pszVolName[MAX_PATH]="";
sDrive=sDrive.Left(1);
GetVolumeInformation( sDrive+":\\", pszVolName, MAX_PATH, 0, 0, 0, 0, 0 );
return CString(pszVolName) + "(" + sDrive + " ";
}
How to use shell icons in your app:
1) After your tree or list control is created (eg. in OnInitDialog), connect your CImageList to the shell icon image list and your tree or list controls:
if ( m_ImageList.Attach( GetShellImageList() ) )
m_Tree.SetImageList( &m_ImageList, TVSIL_NORMAL );
2) When inserting items into your list or tree control, Use GetShellIcon(path) to get an icon id. Eg. if we wanted the icon(s) for "F:", we do this:
int nDriveIcon = GetShellIcon( "F:\\", 0 );
int nDriveIconSelected = GetShellIcon( "F:\\", SHGFI_OPENICON | SHGFI_SELECTED );
m_Tree.InsertItem( GetVolumeName("F:\\"), nDriveIcon, nDriveIconSelected );
3) And while exiting, dont forget to Detach your CImageList from the system image list before it passes out of scope:
CMyDialog::~CMyDialog()
{
if (m_ImageList) m_ImageList.Detach();
}
And thats about it. I hope that helps.
-Dan G.
PS. Hmmm... maybe I should write a CShellImageList class to make this even more compact?
|
|
|
|
|
This looked very handy, but when I try your GetShellIcon function, I only get a image list with five images - the overlays (sharing hand, shortcut arrow, a blob I don't recognise), unknown document symbol, then (at position 4) the icon for the drive you've specified.
If I call the ShGetFileInfo function with "c:\", I get a hard drive picture in 4. If I call it with "z:\\", then I still get five images, but #4 is the network drive picture.
Sadly, it doesn't return an image list with all the different drive piccies.
This would mean your GetShellIcon would always return 4...
I'm using XPsp2, if that makes a difference between me and you.
Iain.
|
|
|
|
|
Hmm.. I know it works in my directory picker tree dialog, but I must have messed something up when I extracted the code. I will mock up a test project and post a correction soon. Thats what I get for editing code without testing it. Sorry about that.
|
|
|
|
|
Okay. I traced the code in my directory picker.
A slight correction of my code description. It attaches to your application's local shell icon cache, not the global icon cache.
The behavior you are seeing is correct. Your local imagelist will grow as you call GetShellIcon on different paths. If drives have the same icon, it just returns the same subscript. So the reason you only see 4-5 icons is because you havent called GetShellIcon for other drives, folders or network paths yet.
I also have another helper function that can get the icons for special shell folders like Desktop, Recycle Bin, My Documents, Network Neighborhood etc, but you have to use PIDLs instead of pathnames to mess with that.
-Dan G.
|
|
|
|
|
You are spot on. If I do:
il = (HIMAGELIST)SHGetFileInfo( "C:\\", 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
ShowImageList (il, -1, ILD_NORMAL);
il = (HIMAGELIST)SHGetFileInfo( "D:\\", 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
ShowImageList (il, -1, ILD_NORMAL);
il = (HIMAGELIST)SHGetFileInfo( "Z:\\", 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
ShowImageList (il, -1, ILD_NORMAL);
Then the image list builds up with each one.
I had assumed it would be acessing the same imagelist explorer does, but in hindsight that might not be a smart idea. So the instance of shell32.dll we load has its own list that gets added to whenever we ask it a question.
I'll attack my article over the weekend, as it would be nice to show pretty piccies.
Iain.
|
|
|
|
|
Nice Job Iain
Thanks for sharing, I will wait for the updated version... with the icons.
Cheers
Alex
|
|
|
|
|
|