 |
|
 |
I download and run it.
myBitmap = new Bitmap(Environment.CurrentDirectory + "\\Exit1.bmp")
System.SystemException {"使用了无效参数。"} System.SystemException
|
|
|
|
 |
|
 |
I'm trying to use this method on the context menus in a toolbar. Originally, I put my call to SetMenuItemBitmaps in the constructor function, so that the images would appear immediately the toolbar was visible. This caused the toolbar to never become visible. So I moved that code to the menu.PopUp event handler to see what would happen. Now the toolbar appears, but whenever I try to open the menu I get a message saying, "Error: PInvoke item (field,method) must be Static." I must admit that I have no idea what this means. Any help would be greatly appreciated.
|
|
|
|
 |
|
 |
Never mind, I got it working
|
|
|
|
 |
|
 |
Entirely based on the example on this page, but more C#, and simpler to see, and use:
[DllImport("user32.dll")]
public static extern int SetMenuItemBitmaps(IntPtr hMenu, IntPtr nPosition, int wFlags, IntPtr hBitmapUnchecked, IntPtr hBitmapChecked);
public const int MF_BYPOSITION = 0x400;
private int AddImageToMenuItem(MenuItem mi,string bitmap_file)
{
Bitmap bmp = new Bitmap(bitmap_file);
Color backColor = bmp.GetPixel(1, 1);
bmp.MakeTransparent(backColor);
return AddImageToMenuItem(mi,bmp);
}
private int AddImageToMenuItem(MenuItem mi,Bitmap bmp)
{
IntPtr intp = bmp.GetHbitmap();
return SetMenuItemBitmaps(mi.Parent.Handle,(IntPtr) mi.Index,MF_BYPOSITION,intp,intp);
}
|
|
|
|
 |
|
 |
Anyone can help me ?
I set transparence by this code
...
myBitmap = new Bitmap(this.IMGL_MENU.Images[1]);
backColor = myBitmap.GetPixel(1, 1);
myBitmap.MakeTransparent(backColor);
intp = myBitmap.GetHbitmap();
...
but I have no effect.
(Images are store into an ImageList)
|
|
|
|
 |
|
 |
Yes, it seems like transparency isn't working in this example. The sample images have the same gray background as the menus.
It seems like it ought to be:
backColor = myBitmap.GetPixel(0, 0);
The pixels in both the sample images at 1, 1 are not the background color.
I get a blue background instead of transparent when calling MakeTransparent.
|
|
|
|
 |
|
 |
The good doctor expounds on alternatives for developing lightweight controls for browsers, and offers a Visual Basic .NET solution for attaching an icon to a menu item.
Controls and Icons a la .NET
|
|
|
|
 |
|
 |
The code doesn't seem to work with the standard image size of 16x15 pixels? I tried it but they are cropped. Getting 13x13 images like the ones you have used seems impossible and all standard icons looked terrible when I tried to reduce their size...
|
|
|
|
 |
|
 |
yeah.. you need 13x13 bitmaps to use SetMenuItemBitmaps, maybe the easiest way to add bitmaps to your menu. It may seem to be a little inconvenient, but I found that 13x13 bitmaps are very easy to modify manually than 16x16 or other larger size formats!
|
|
|
|
 |
|
 |
Create toolbar with pictures and give menu items same ID as corresponding toolbar ID. Compiler will take care of the rest.
|
|
|
|
 |
|
 |
Could you be more specific about what you mean by "ID" please?
I have set up a toolbar with buttons with an image on each, and a dropdown menu with several sub-items. Then I tried to change the Name property of each menu item to be the same as the appropriate toolbar button, but .Net wouldn't have a bar of it.
Thanks
Sue
|
|
|
|
 |
|
|
 |
|
 |
...this looks like a very quick and easy way to add images to menu items as oposed to drawing the menu items using OwnerDraw?
|
|
|
|
 |
|
 |
yup, it defnitely is, unless you want your menu to look totally different
Assumption is the mother of all Screw-ups
|
|
|
|
 |
|
 |
Hi,
Good work, but i guess there are to much API calls. There are some (Syncfusion,DevExpress) Guys that draws menuitem with images also, but do this with 100% C#. I think if we use to much API calls, the goal of the DotNet Framework, device indepented, would not work....
.:Greets from Jerry Maguire:.
|
|
|
|
 |
|
 |
Jerry Maguire wrote:
I think if we use to much API calls, the goal of the DotNet Framework, device indepented, would not work....
Basically the dotnet framework is API calls contained by try{} catch(){}.
|
|
|
|
 |
|
 |
...
.:Greets from Jerry Maguire:.
|
|
|
|
 |
|
 |
True, but if I'm not mistaken (couldn't be ), that's only true for the current implementation of the CLR. The next version of Windows is supposed to be based on the .NET framework natively. Now, I don't know if that means that there'll be a virtual machine to run old API based apps, or if it just means that Windows will ship with .NET, but either way, I believe the framework is supposed to be tied in much more intimately with the OS. And of course, when (and if) other OS's make their own implementation of the CLR, you definately won't be able to rely on API's if you want machine independence.
That being said, it's still nice to see a menu bitmap article, as the concept, until recently, has always confused me, and I'm quite frankly VERY surprised that .NET menus don't already support bitmaps, given how long the idea has been around and all.
Jamie Nordmeyer
Portland, Oregon, USA
|
|
|
|
 |