|
A few things - don't use a StreamWriter , you may run into encoding problems. An icon is a binary file, so just use something simple like a FileStream .
Also, instead of Application.StartupPath + "/TestI.ico" , use Path.Combine(Application.StartupPath, "TestI.ico") , which properly combines the directories and/or filename with the correct directory separator. It may have worked this time, but it usually won't depending on the circumstances.
Finally to the real problem. The icon support in the .NET stinks. Icon files contain a header section that describes the resolution and addresses of indexed bitmaps (has palettes) within the file. Because of an apparent but in the System.Drawing classes, the 32-bit icon (which contains the per-pixel alpha channel) is not well preserved (and sometimes not at all).
The best way is to P/Invoke the various icon functions from the Windows Shell APIs, like SHCreateFileExtractIcon , which uses the COM interface IExtractIcon , which is pretty simple to declare as a managed interface (be sure to use the GuidAttribute with the right interface ID (IID)).
As far as extracting various resolutions from an icon file with multiple bitmaps, this is exactly what Windows does with methods like that from above and all the lower-level icon functions like GetIcon . Most of the time, the small and large icon handles (HICON ) are returned at once with the appropriate resolution. In order to get 32-bit icons with the native APIs, you must use an application manifest to bind to Common Controls 6, which is described in my article Windows XP Visual Styles for Windows Forms[^]. If you're using .NET 1.1 or higher, you can also call Application.EnableVisualStyles before Application.Run , but be sure to read the documentation for a few quirks (like calling Application.DoEvents in between the two to avoid common problems).
If you want complete control over the icons, how they save, and which icons are extract, read about Icons in Win32[^]. All those structs can be create in .NET easily enough, so long as you use the appropriate StructLayoutAttribute since some structs have a pack size, IIRC, of 2.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Does anyone have an idea on how to capture scrollbar events from a ListView control?
|
|
|
|
|
Extend it and override WndProc . Handle the WM_HSCROLL (0x0114) and WM_VSCROLL (0x0115) notification messages. See the Platform SDK for the possible WPARAM and LPARAM values.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I running a service with no dialogs. I like to put an icon in the tray to show that the process is running. I don't know where to begin.
Thanks.
|
|
|
|
|
If this is running as a true Windows Service (Services Control Panel), then you would normally write a second app that communicates with the service you wrote and controls the icon in the Tray.
RageInTheMachine9532
|
|
|
|
|
other than that u could just insert a notifyicon component from the toolbox :P
i jsut havent figured out how to add a context menu to it!
|
|
|
|
|
I got a file which is an assembly file (managed code).
Now how do I find out programatically from unmanaged code?
TIA
Promise only what you can do. And then deliver more than what you promised. This signature was created by "Code Project Quoter".
|
|
|
|
|
Kant wrote:
I got a file which is an assembly file (managed code).
Now how do I find out programatically from unmanaged code?
Find out what?
RageInTheMachine9532
|
|
|
|
|
Dave Kreskowiak wrote:
Find out what?
My bad.
Find out programatically(from unmanaged), Is that file is an assembly file or not.
Promise only what you can do. And then deliver more than what you promised. This signature was created by "Code Project Quoter".
|
|
|
|
|
See my article, Shell Extensions for .NET Assemblies[^], a handy overlay icon handler that distinguishes between the two. The source is included.
There's a single bit that is different in the PE/COFF header of the executable. You load and map the DLL or EXE as a data file and traverse several virtual tables to find the address of this bit, then you check it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
There's a single bit that is different in the PE/COFF header of the executable
Now how the HELL did you figure that one out??
Damn geniuses...year younger than me, already got an MVP...I don't have an MVP title...work my fingers to the bone, day in, day out, for bent pennies...
|
|
|
|
|
I strive to know everything.
I read an article a long time back in MSDN Magazine from executable master Matt Pietrek, the guy who wrote dumpbin.exe. In this article he briefly mentioned about this bit but nothing more. Using concepts from his article i dug into the PE/COFF header format and wrote the code to find that bit and check it. Learned a heck of a lot about the PE/COFF in the process, which is really what I was after in the first place.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
I strive to know everything.
*sigh*
Don't take this the wrong way Heath, but sometimes I really wished you lived around here....you sound like you share so many of my viewpoints, you'd be cool to hang out with
Want some (unaskedfor) advice from someone who's only a little bit older? Don't limit your strive to coding related stuff alone...right now, I've got about 20 different things I'm trying to teach myself now, and only a handful are computer related. Makes life a blast
Jeremy Kimball
|
|
|
|
|
Don't take this the wrong way, but I really hate agism. I - and a handful of others blessed with the skills of good development - were programming circles around some of our University profs. I was one of 3 go-to guys when I was still in high school in a town of over 20,000 back then. In this field, actually development skills have little to do with age. Again, don't take it the wrong way, but it's just a pet pieve of mine. I've experienced a lot of unnecessary hardships because of it from upper management taking credit or passing credit to others for what I did soley (and I don't mean just giving credit to the team, which I could handle - I mean actually saying that they personally did it all).
I also don't limit myself to development (not coding, which I use to refer to those that blindly write code without really knowing what they're doing). I love photography and people - including my father, a professional photographyer - say I have an eye for it. I also learned WAY MORE about feminine hygeine in high school since most of my friends were girls (as you can probably guess, I wasn't a big fan of athletes).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Don't take *this* the wrong way, but you need to lighten up
I was being extremely sarcastic in saying I'm older than you. (born on 5/10/77, which makes me a whopping year older than you). I was and still am in the same boat you're in now - problems dealing with "old-school" developers who don't trust your judgement, even when it's obviously right.
I had the same situation with the whole female thing in high school...it's an interesting experience trying to explain the female ovulation cycle to your girlfriend
|
|
|
|
|
public sealed class Singleton<br />
{<br />
static readonly Singleton instance=new Singleton();<br />
<br />
static Singleton()<br />
{<br />
}<br />
<br />
Singleton()<br />
{<br />
}<br />
<br />
public static Singleton GetInstance()<br />
{<br />
return instance;<br />
}<br />
}
That was from this[^] article.
Maybe you can answer this question:
Is there an explicit need there for the private constructor to avoid initialization? If we remove it completely, will .NET supply a default public parameterless constructor?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
The point of making a constructor private/protected is so that someone using the object via the API can't create an instance on their own. This is very handy if you want to make an object with singleton behavior.
The only way you get the instance (the only instance) is using GetInstance . Doing it this way, there is no other way create the object. If you leave a construtor publicly exposed then it cirumvents the code you put in place to enforce the singleton behavior.
|
|
|
|
|
Thank you that was very clarifying.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
If you remove the private default constructor, yes, the compiler will provide one.
Also, this isn't the best singleton pattern to use. The one I posted earlier was. It uses a double-check locking approach which eliminates a couple race conditions that may occur.
This has been discussed in the past in this forum so if you want to know more, try searching for "singleton" or something for the C# forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Well, what I'm doing right now is, I'm providing the binary deserialization mechanism in the static constructor, since I only need to load the settings from the file once in the lifetime of the program. Then, I'm providing a Save instance method so that I could save multiple times, which is needed whenever the settings dialog is opened and closed with Ok button.
If there is nothing wrong with this in your opinion, then don't post an answer; otherwise, please comment.
Thanks!
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
There's nothing wrong with it, but what I was referring to in the previous post could result in your settings being loaded more than once. Now, this probably wouldn't be a problem, but locking your resources will gaurantee that it wouldn't.
I'm not trying to down-play your approach, only present different ideas. It's you're implementation - do what you want.
Typically in a singleton you provide static methods and properties and hide instance details from other classes. For instance, you could also have your Save method as static and it gets the private Instance property (or uses your GetInstance method) and saves the data. For the caller then, it's as simple as doing MySettings.Save .
Again, just some things to think about. Your approach would work fine in most cases (only may become a problem with multiple AppDomains in a single process, which may not apply).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi
I am using VS 2003 with the latest windoze media player.
I want to hide a video in a dll.
Then I want the media player to access it. How do I do that?
Thanks!
Antoine
This by our hands that dream,
"I shall find a way or make one!"
|
|
|
|
|
You can't, not without implementing your own native pluggable protocol handler for windows (like that which handles http(s), ms-help, its, res, etc.). Windows Media Player uses monikers to bind to streams and there is not - at this time - a plugglable protocol handler for streaming embedded resources.
Also, embedded a video in an assembly is extremely wasteful. You'll increase the load times of the assembly incredibly. Just distribute it as a separate file and point the URL property of WMP to the file when you need to play it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yeah,
OK. thanks for the answer. didn't thought about the loss of loading time.
Do you know of a way to keep the videos secure? Digital Rights Managment
seems a way to go -is it???
Cheers
Antoine
This by our hands that dream,
"I shall find a way or make one!"
|
|
|
|
|
Yes, DRM would be something to look into.
Beside, if you think storing the file as an embedded resource (or even in the .rsrc section of the PE/COFF executable itself) would keep it secure, you'd be sorely surprised. ildasm.exe, for example, can easily extract anything in an assembly. One could throw together a simple .NET app to do this as well. Resources in native DLLs can easily be extracted, too (though writing the code for it is a big harder, but plenty of apps - including Visual Studio - can make quick and easy work of that by just loading the .rsrc section and providing extraction capabilities).
If you did want to embed it stil, you'd have to extract it anyway in order for WMP to play it. At that point someone could also mess with it.
DRM is really about your only protection, if it is really that necessary.
Microsoft MVP, Visual C#
My Articles
|
|
|
|