|

Introduction
One thing I love best about XP is the capability to have themed controls. You can even load new themes much like it's been possible with most window managers for the X Window System.
These themed controls can be used with .NET Windows Forms by supplying a manifest and – for some controls like Button – setting the FlatStyle property to FlatStyle.System.
Unfortunately, setting the FlatStyle property of a Button control to FlatStyle.System makes it impossible to display an image on the face of the button. Somehow, setting the Image property has no effect on these controls.
Background
A number of workarounds have been proposed, e.g., this one by MalteseFalcon. All approaches known to me use owner-drawn buttons to mimic the built in visual styles of XP but provide no ability to match third party visual styles.
The code snippet in this article makes it possible to have images on your XP-themed buttons while retaining the capability to adapt to custom visual styles.
The capability to draw controls using different visual styles was introduced by Microsoft with comctl32.dll, version 6, which has been shipping with the newest versions of Windows, i.e., Windows XP and Windows Server 2003. With this version, a number of new API calls have been introduced, among them is BCM_SETIMAGELIST which assigns an image list to a button control. The code in this article uses Platform Invocation Services (PInvoke) to use the BCM_SETIMAGELIST message.
The BCM_SETIMAGELIST message has another interesting property: if you supply more than one image in the image list, you can have the button display a different image for each of a number of states the button is in:
Normal
Hover
Pressed
Disabled
Focused
This behavior might be achieved using events as well, but using BCM_SETIMAGELIST, you get it without having to code the event handlers.
Using the code
The code wraps the System.Windows.Forms.Button class in a class called ImageButton. You can use this class just like a normal button except for the additional overloaded method SetImage. If you just want to display an image on the face of the button, you can use the method like this: Bitmap searchBitmap = new Bitmap("Search_16X16_32bpp.png");
ImageButton searchImageButton = new ImageButton();
searchImageButton.SetImage(searchBitmap);
...
If you want more control over the alignment, padding, or set images for different button states, use the overloaded methods: Bitmap goBitmap = new Bitmap("Go_48X48_32bpp.png");
Bitmap goHoverBitmap = new Bitmap("Go!_48X48_32bpp.png");
Bitmap goDisabledBitmap = new Bitmap("GoDisabled_48X48_32bpp.png");
ImageButton goImageButton = new ImageButton();
goImageButton.SetImage(goBitmap, goHoverBitmap, goBitmap, goDisabledBitmap,
goBitmap, ImageButton.Alignment.Center);
...
Images with alpha channel are supported.
Points of Interest
Windows Forms ImageLists have a bug: if you add an Image to an ImageList, the alpha channel is lost. Therefore, the class in this article uses a workaround by resetting the pixel values of the Image after it has been added to the ImageList.
The demo image shows some buttons with the default XP visual style (top) as well as the custom Chaninja RC5 visual style (bottom).
The images in the demo are from the Qute collection originally drawn for Mozilla Firebird by Arvid "Quadrone" Axelsson.
History
- February 24, 2004: Version 1.1.
- Added alignment for non-XP-style buttons.
- Added speed-up to Bitmap copying, contributed by Richard Deeming.
- February 26, 2004: Version 1.2.
- Added image scaling suggested by nxtwothou.
- February 28, 2004: Version 1.3.
- Added bicubic interpolation for rescaling.
- May 8, 2004: Version 1.4.
- Added drop down arrow capability
- May 14, 2004: Version 1.5.
- Added
ThemedImage property for the designer.
- Added auto-generation of disabled image (thanks to Carlos Leyva).
- October 25, 2004: Version 1.6.
- Fixed themes on/off detection bug (thanks to Leonid Kunin).
- Added NAnt file.
- Added I18n :-)
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 71 (Total in Forum: 71) (Refresh) | FirstPrevNext |
|
 |
|
|
I didn't want to implement such a large code, and I couldn't figure out what xlouk was talking about, so I found a different solution.
If you use the "standard" flat style and set the backColor to transparent, the xp-style background will show through. Then all you have to do is set the background image and you're done. I've tested it on a couple different theme settings and it looks great on all of them.
Hopefully this helps.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
this code is not any more needed when using .NET 2.0 Framework (VS2005). The control System.Windows.Forms.Button is now able to view an icon even with visual style enabled.
But if you nevertheless use the ImageButton class with .NET 2.0 framework or the latest 1.1 for XP Embedded, you may depending on the installed comctrl32.dll versions encounter a problem that icons loaded to an ImageList which is assigned to a ListView won't appear with enabled visual styles.
Reason is, that the call of
GetCommonControlDLLVersion(ref dllVersion);
in the ImageButton Code will force loading the default comctrl32.dll, which may be an old buggy one (V 5.82...) and don't view the icons loaded to the ImageList in a ListView control.
Regards,
xlouk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
So what can I do with this information - how can I reference the correct, latest version - which I believe is version 6?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
What you can do is, don't use the code of this project because it is obsolete in frameword >= V2.0 or don't call GetCommonControlDLLVersion(ref dllVersion) or load the latest version of the dll before calling this method or let .NET do this for you by loading a forms control before calling this method. .NET, when initiating a control, tries to use the latest version of the dll when not allready loaded. GetCommonControlDLLVersion(ref dllVersion) loads the default (probably buggy when installed as default, very likely) dll, when the correct dll is not allready loaded before the call.
When you update to framework 2.0, the buggy dll will not be replaced, it is still used as default dll beside the newer version which ist stored on an other place.
Regards,
xlouk
xlouk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
I'm having the same problem. My situation is a bit different however, since my product (written partly in C#/.NET and partly in C++/WTL) is a plugin to a host, written in C#/.NET. In the C++ part of my code is a list view that fails to display the images in its image list, since the host program calls EnableVisualStyles. When this call is removed the images show up nicely (but this is not a solution since the host program needs this call).
I believe my image list gets created by version 5.8 of comctrl32.dll, and my list view by version 6.0, which is causing the problem (see for example http://blogs.msdn.com/rprabhu/archive/2003/09/28/56540.aspx). Does anyone have an idea on what I can do..? When I create my image list (currently using the WTL class CImageList) and my list view (WTL class CListViewCtrl), is there any way that I can control which version of comctrl32.dll that is used..? It would be nice if they were created by the same version of comctrl32.dll =)
Thanks in advance!
/David
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
But the issue is, even in .NET 2.0 VS 2005, when you set the FlatStyle to System, the control does not display the image assigned to the image property.
But you are saying this is fixed in .NET 2.0. Can you clarify on this please? I think the issue is still there in .NET 2.0. Am i wrong?
Are you saying that the button control in .NET 2.0 can be made to display image even if its FlatStyle is set to System. Can you show how to make this work in .NET 2.0.
I tried this and it does not display the Image.
Also you say that .NET 2.0 button control can display Icon???? If i set an icon to the Image property, it does not display it properly - i.e. the shadow effects in the icon is lost and the control displays black spots where the shadow is supposed to be there.
-- modified at 13:20 Monday 13th August, 2007
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
per default comctl32.dll is installed in c:\windows\system32 folder which version is <6.0 and is buggy. In addition search under C:\Windows\WinSxS\x86_Microsoft.Windows.Common-Controls... folders for an installed comctl32.dll with version >= 6.0. You need it for a correct working image button with flat style system.
Then there is an other "bug" you have to avoid. The call of GetCommonControlDLLVersion() in this project will load the default comctl32.dll which is most likely V 5.82... and buggy even if you have the good one installed under the WinSxS folder. This happens, when this call occurs first, before a control is loaded, which is in this project the case. Comment out this call and set the ComCtlMajorVersion for a test to 6.0 in the ImageButton constructer.
But I can't remember how I got the comctl32.dll with V > 6.0 on my computer. May be per SP2 of Windows or .Net Framework 2.0 or an other way.
Good luck
xlouk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
When I clicked the DropdownButton in ur demo, the down-arrow disappeared:
1, Remove "ImageButtonTest.exe.manifest" file and run the ImageButtonTest.exe, that makes the app look like in Windows98.
2, PressDown the DropdownButton and then release but do not move mouse, now the down-arrow dispaapears.
3, move the mouse cursor, the down-arrow appears again.
PunCha
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I loaded the project and run as debug, I could not get it to work due to errors like this below Please advise what I done wrong?. Thanks
C:\C#_Experiment_Code\ImageButtonTest\ImageButtonTest.cs(220): The variable 'SearchImageButton' is either undeclared or was never assigned. C:\C#_Experiment_Code\ImageButtonTest\ImageButtonTest.cs(219): The variable 'GoImageButton' is either undeclared or was never assigned. C:\C#_Experiment_Code\ImageButtonTest\ImageButtonTest.cs(218): The variable 'DisableButton' is either undeclared or was never assigned. C:\C#_Experiment_Code\ImageButtonTest\ImageButtonTest.cs(217): The variable 'DropDownButton' is either undeclared or was never assigned. C:\C#_Experiment_Code\ImageButtonTest\obj\Debug\CSC244.tmp Error generating Win32 resource: Error reading icon 'C:\C#_Experiment_Code\ImageButtonTest\App.ico' -- The system cannot find the file specified. Could not find type 'OOGroup.Windows.Forms.ImageButton'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built. Could not find type 'OOGroup.Windows.Forms.ImageButton'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built. Could not find type 'OOGroup.Windows.Forms.ImageButton'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built. Could not find type 'OOGroup.Windows.Forms.ImageButton'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
>It's not so much what you have to learn if you accept weird theories, it's what you have to unlearn. (Isaac Asimov) >Life's journey is not to arrive at the grave safely in a well preserved body,but rather to skid in sideways, totally worn out, shouting "...holy sh*t...what a ride! [Riscy]
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
hi,
thanks for your button, it's great! I needed dropdown buttons with images and xp style for an IE toolbar: In my toolbar, I show contextmenus right under buttons when the user (left) clicks them. Just a question: do you know how to make those button look "pushed" when the contextmenu is displayed? I tried by setting them disabled, but it doesn't look nice.
Thank you very much!
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Hi,
The only way to achieve this I can think of is to use a RadioButton with Appearance set to "Button". This looks like a regular Button, but changes to the pushed look when the radio button is selected. I don't know if it's easy to adapt the code.
HTH
-- Michael Ganss O&O Services GmbH
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Michael,
Thanks for your answer, I came to a similar conclusion: in fact I'm deriving my control from a check box with button appearance. I'm working on it, it seems to work, I will post an article about it when I finish. I've slightly modified your logic, only used one image, added a dropdown menu feature, and not used the unsafe code as it crashes on my PC, but instead the code found in a comment posted here:
for (int row = 0; row < bm.Height; row++) { for (int col = 0; col < bm.Width; col++) { Marshal.WriteInt32( pDst, dstoff + col, Marshal.ReadInt32( pSrc, srcoff + col ) ); } srcoff += (src.Stride >> 2); dstoff += (src.Stride >> 2); } Anyway, thanks for your work, it helped me a lot!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi everybody,
I modified the code slightly and it resulted in a ImageCheckBox: a checkbox with an image and xp styles.
Here's my code. The changes are: -base class is CheckBox -Property "ImageAlign" is overridden so that it can be set by designer (would be nice to have in "ImageButton", too) ! -Setting the image by property "ThemedImage": current "ImageAlign" value is honored. -DropDown-Feature removed (makes no sense with a checkbox).
Enjoy it !
Wolfgang
=================================
/// /// A System.Windows.Forms.CheckBox that can have an image on systems that support visual styles. /// Uses the BCM_SETIMAGE message that is provided by comctl32.dll, version 6. /// On systems that have a lower version of comctl32.dll, i.e. earlier than Windows XP, /// falls back to FlatStyle.Standard. /// /// public class ImageCheckBox: CheckBox { /// /// Constructor. /// public ImageCheckBox() { FlatStyle = FlatStyle.System; }
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; }
/// /// The alignment of an image within a button. /// public enum Alignment { Left, Right, Top, Bottom, Center };
private const int BCM_SETIMAGELIST = 0x1600 + 2;
[StructLayout(LayoutKind.Sequential)] private struct BUTTON_IMAGELIST { public IntPtr himl; public RECT margin; public int uAlign; }
[DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref BUTTON_IMAGELIST lParam);
[StructLayout(LayoutKind.Sequential)] private struct DLLVERSIONINFO { public int cbSize; public int dwMajorVersion; public int dwMinorVersion; public int dwBuildNumber; public int dwPlatformID; }
[DllImport("comctl32.dll", EntryPoint="DllGetVersion")] private static extern int GetCommonControlDLLVersion(ref DLLVERSIONINFO dvi);
private int ComCtlMajorVersion = -1;
private Bitmap themedImage; [Description("The image on the face of the button."), Category("Appearance"), DefaultValue(null)] public Bitmap ThemedImage { get { return themedImage; }
set { if (value != null) { SetImage(value); }
themedImage = value; } }
/// /// Set the "ImageAlign" property. /// /// If the property "ThemedImage" is already set the image is re-rendered. /// [DefaultValue (ContentAlignment.MiddleCenter)] [Description ("Ausrichtung des Bildes. Wandelt intern auf einen Wert der Enum \"Alignment\" um !")] public new ContentAlignment ImageAlign { get { return base.ImageAlign; } set { Alignment alignment = GetAlignment(value);
base.ImageAlign = value;
//Ist das ThemedImage gesetzt ? Wenn ja, dann in die Checkbox packen ! if (this.themedImage != null) { this.SetImage (this.themedImage, alignment); } } }
/// /// Convert a content alignment to a "Alignment". /// /// /// private static Alignment GetAlignment (ContentAlignment _contentAlignment) { Alignment alignment = Alignment.Center; // public enum Alignment { Left, Right, Top, Bottom, Center }; if (_contentAlignment == ContentAlignment.MiddleLeft || _contentAlignment == ContentAlignment.TopLeft || _contentAlignment == ContentAlignment.BottomLeft) alignment = Alignment.Left; else if (_contentAlignment == ContentAlignment.MiddleRight || _contentAlignment == ContentAlignment.TopRight || _contentAlignment == ContentAlignment.BottomRight) alignment = Alignment.Right; else if (_contentAlignment == ContentAlignment.TopCenter) alignment = Alignment.Top; else if (_contentAlignment == ContentAlignment.BottomCenter) alignment = Alignment.Bottom; else if (_contentAlignment == ContentAlignment.MiddleCenter) alignment = Alignment.Center;
return alignment; }
/// /// Convert the Alignment to a content alignment. /// /// /// private static ContentAlignment GetContentAlignment(Alignment _alignment) { ContentAlignment contentAlignment = ContentAlignment.MiddleCenter; switch (_alignment) { case Alignment.Bottom: contentAlignment = ContentAlignment.BottomCenter; break; case Alignment.Left: contentAlignment = ContentAlignment.MiddleLeft; break; case Alignment.Right: contentAlignment = ContentAlignment.MiddleRight; break; case Alignment.Top: contentAlignment = ContentAlignment.TopCenter; break; case Alignment.Center: contentAlignment = ContentAlignment.MiddleCenter; break; }
return contentAlignment; }
/// /// Sets the image of the button to the specified value. /// The image is aligned at the center with no margin. /// All states of the button show the same image. /// /// The images for the button public void SetImage(Bitmap image) { //Use the current "ImageAlign" value ! //SetImage(new Bitmap[] { image }, Alignment.Center, 0, 0, 0, 0); SetImage(new Bitmap[] { image }, GetAlignment (this.ImageAlign), 0, 0, 0, 0); } /// /// Sets the image of the button to the specified value with the specified alignment. /// All states of the button show the same image. /// /// The images for the button /// The alignment of the image public void SetImage(Bitmap image, Alignment align) { SetImage(new Bitmap[] { image }, align, 0, 0, 0, 0); }
/// /// Sets the image of the button to the specified value with the specified alignment and margins. /// All states of the button show the same image. /// /// The images for the button /// The alignment of the image /// The left margin of the image in pixels /// The top margin of the image in pixels /// The right margin of the image in pixels /// The bottom margin of the image in pixels public void SetImage(Bitmap image, Alignment align, int leftMargin, int topMargin, int rightMargin, int bottomMargin) { SetImage(new Bitmap[] { image }, align, leftMargin, topMargin, rightMargin, bottomMargin); }
/// /// Sets the images of the button to the specified values. /// The images are aligned at the center with no margin. /// /// The images for the button /// The normal state image for the button. /// The hover state image for the button. /// The pressed state image for the button. /// The disabled state image for the button. /// The focused state image for the button. public void SetImage(Bitmap normalImage, Bitmap hoverImage, Bitmap pressedImage, Bitmap disabledImage, Bitmap focusedImage) { SetImage(new Bitmap[] { normalImage, hoverImage, pressedImage, disabledImage, focusedImage }, Alignment.Center, 0, 0, 0, 0); } /// /// Sets the images of the button to the specified values with the specified alignment. /// /// The images for the button /// The normal state image for the button. /// The hover state image for the button. /// The pressed state image for the button. /// The disabled state image for the button. /// The focused state image for the button. /// The alignment of the image public void SetImage(Bitmap normalImage, Bitmap hoverImage, Bitmap pressedImage, Bitmap disabledImage, Bitmap focusedImage, Alignment align) { SetImage(new Bitmap[] { normalImage, hoverImage, pressedImage, disabledImage, focusedImage }, align, 0, 0, 0, 0); }
/// /// Sets the images of the button to the specified values with the specified alignment and margins. /// /// The normal state image for the button. /// The hover state image for the button. /// The pressed state image for the button. /// The disabled state image for the button. /// The focused state image for the button. /// The alignment of the image /// The left margin of the image in pixels /// The top margin of the image in pixels /// The right margin of the image in pixels /// The bottom margin of the image in pixels public void SetImage(Bitmap normalImage, Bitmap hoverImage, Bitmap pressedImage, Bitmap disabledImage, Bitmap focusedImage, Alignment align, int leftMargin, int topMargin, int rightMargin, int bottomMargin) { SetImage(new Bitmap[] { normalImage, hoverImage, pressedImage, disabledImage, focusedImage }, align, leftMargin, topMargin, rightMargin, bottomMargin); } private bool generateDisabledImage = false; [Description("Determines whether the image for the disabled state will be generated automatically from the normal one."), Category("Appearance"), DefaultValue(false)] public bool GenerateDisabledImage { get { return generateDisabledImage; }
set { generateDisabledImage = value; } } [DllImport("UxTheme")] private static extern bool IsThemeActive();
[DllImport("UxTheme")] private static extern bool IsAppThemed();
private static bool IsVisualStylesEnabled { get { return OSFeature.Feature.IsPresent( OSFeature.Themes ) && IsAppThemed() && IsThemeActive(); } }
/// /// Sets the images of the button to the specified value with the specified alignment and margins. /// /// The images for the button. /// The alignment of the image /// The left margin of the image in pixels /// The top margin of the image in pixels /// The right margin of the image in pixels /// The bottom margin of the image in pixels public void SetImage(Bitmap[] images, Alignment align, int leftMargin, int topMargin, int rightMargin, int bottomMargin) { if (GenerateDisabledImage) { if (images.Length == 1) { Bitmap image = images[0]; images = new Bitmap[] { image, image, image, image, image }; }
images[3] = DrawImageDisabled(images[3]); }
if (ComCtlMajorVersion < 0) { DLLVERSIONINFO dllVersion = new DLLVERSIONINFO(); dllVersion.cbSize = Marshal.SizeOf(typeof(DLLVERSIONINFO)); GetCommonControlDLLVersion(ref dllVersion); ComCtlMajorVersion = dllVersion.dwMajorVersion; }
if (ComCtlMajorVersion >= 6 && FlatStyle == FlatStyle.System && IsVisualStylesEnabled) { RECT rect = new RECT(); rect.left = leftMargin; rect.top = topMargin; rect.right = rightMargin; rect.bottom = bottomMargin;
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST(); buttonImageList.margin = rect; buttonImageList.uAlign = (int)align;
ImageList = GenerateImageList(images); buttonImageList.himl = ImageList.Handle;
SendMessage(this.Handle, BCM_SETIMAGELIST, 0, ref buttonImageList); } else { FlatStyle = FlatStyle.Standard;
if (images.Length > 0) { Image = images[0]; }
switch (align) { //Don't modify value if it is already the correct one (would lead to stack overflows in settings the //property "ImageAlign") ! case Alignment.Bottom: if (this.ImageAlign != ContentAlignment.BottomCenter) this.ImageAlign = ContentAlignment.BottomCenter; break; case Alignment.Left: if (this.ImageAlign != ContentAlignment.MiddleLeft) this.ImageAlign = ContentAlignment.MiddleLeft; break; case Alignment.Right: if (this.ImageAlign != ContentAlignment.MiddleRight) this.ImageAlign = ContentAlignment.MiddleRight; break; case Alignment.Top: if (this.ImageAlign != ContentAlignment.TopCenter) this.ImageAlign = ContentAlignment.TopCenter; break; case Alignment.Center: if (this.ImageAlign != ContentAlignment.MiddleCenter) this.ImageAlign = ContentAlignment.MiddleCenter; break; } } }
private System.Drawing.Bitmap DrawImageDisabled(System.Drawing.Image image) { System.Drawing.Bitmap active = new Bitmap(image); System.Drawing.Bitmap disable = new Bitmap(active.Width, active.Height); System.Drawing.Graphics g = Graphics.FromImage(disable);
g.DrawImage(active, 0, 0); ControlPaint.DrawImageDisabled(g, active, 0, 0, Color.Empty); g.Dispose(); return disable; }
private ImageList GenerateImageList(Bitmap[] images) { ImageList il = new ImageList(); il.ColorDepth = ColorDepth.Depth32Bit;
if (images.Length > 0) { for (int i = 0; i < images.Length; i++) { int ReSizeHeight = this.Height; int ReSizeWidth = this.Width;
if (ReSizeHeight > 256) { ReSizeHeight = 256; } if (ReSizeWidth > 256) { ReSizeWidth = 256; }
if (images[i].Width > ReSizeWidth || images[i].Height > ReSizeHeight) { double Scaling; double WidthScaling = ReSizeWidth / (double)images[i].Width; double HeightScaling = ReSizeHeight / (double)images[i].Height;
if (WidthScaling < HeightScaling) { Scaling = WidthScaling; } else { Scaling = HeightScaling; }
int newWidth = (int)(images[i].Width * Scaling); int newHeight = (int)(images[i].Height * Scaling); Bitmap bm = new Bitmap(newWidth, newHeight); Graphics graphics = Graphics.FromImage(bm); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(images[i], 0, 0, newWidth, newHeight); images[i] = bm; } }
il.ImageSize = new Size(images[0].Width, images[0].Height);
foreach (Bitmap image in images) { il.Images.Add(image); Bitmap bm = (Bitmap)il.Images[il.Images.Count - 1];
// copy pixel data from original Bitmap into ImageList // to work around a bug in ImageList: // adding an image to an ImageList destroys the alpha channel
//WKnauf 25.02.2005: den Unsafe Code auskommentiert, den Safe Code reingenommen ! for (int x = 0; x < bm.Width; x++) { for (int y = 0; y < bm.Height; y++) { bm.SetPixel(x, y, image.GetPixel(x, y)); } }
/* // code below contributed by Richard Deeming // requires /unsafe compiler flag // does the same as the code above, albeit a lot faster Rectangle rc = new Rectangle(new Point(0, 0), image.Size); BitmapData src = image.LockBits(rc, ImageLockMode.ReadOnly, image.PixelFormat); BitmapData dst = bm.LockBits(rc, ImageLockMode.WriteOnly, image.PixelFormat); try { unsafe { int* pSrc = (int*)src.Scan0; int* pDst = (int*)dst.Scan0; for(int row=0; row < bm.Height; row++) { for(int col=0; col < bm.Width; col++) { pDst[col] = pSrc[col]; } pSrc += src.Stride >> 2; pDst += dst.Stride >> 2; } } } finally { bm.UnlockBits(dst); image.UnlockBits(src); }*/ } }
return il; } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I compiled and ran the demo project but the buttons had no xp style...just the standard style, although the property for FlatStyle is set to system
Any help is appreciated.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi Michael,
I was trying out your ImageButton control in one of my C# windows app projects. After a day, I removed it from the project. But now, the tree view control in my C# windows app is not displaying the images (from an imagelist) that it used to display before I included the ImageButton control.
If I remove the Application.EnableVisualStyles() from the constructor of the form, then teh images appear.
Any ideas why?
Thanks, Ragul
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Ragul,
that sounds strange. If you removed the source file from your solution, there shouldn't be anything left that can cause this behavior. Maybe it has something to do with the .manifest file.
Regards
-- Michael Ganss O&O Services GmbH
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Michael,
Yes, it seemed very strange to me too. I removed all .manifest from the project and still it does not get back the images in the treeview. The primary output (exe) from the project works perfectly fine in another XP box but would not have the images in the treeview in my box.
I do not know anything about coomctl32 but for what I learnt form ur article. I was wondering if it had something to do with the registry; as in, store the configurations of comctl32 in the registry or somthing like that??
Sholud I try reinstalling VS.Net?? Let me know if u find out something...
Thanks, Ragul
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Ragul,
AFAIK the registry is not involved at all regarding visual styles. The .manifest simply selects another version of the comctl32.dll, but that's not a permanent selection. As soon as you remove the .manifest, the .exe uses the old version again, and you don't get visual styles. I wouldn't reinstall VS.NET just yet. Have you seen this blog entry: http://blogs.msdn.com/rprabhu/archive/2003/09/28/56540.aspx There's a topic there that seems to describe the problem you are experiencing: "Q I sometimes have weird problems with Application.EnableVisualStyles. Some controls don't seem to pick up theming; images that are assigned to TreeView nodes or ListView items sometimes don't render at all."
HTH
-- Michael Ganss O&O Services GmbH
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
There's been a report bug with visual styles. If you add a Application.DoEvents() after your Application.EnableVisualStyles() your images should reappear
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi !
First of all: really great button !
I encountered some strange crashes and exceptions in a complex dialog with a bunch of different components (from different vendors) and controls. E.g: System.NullReferenceException: at System.Drawing.SafeNativeMethods.GdipCreateFont(HandleRef fontFamily, Single emSize, FontStyle style, GraphicsUnit unit, IntPtr& font) at System.Drawing.Font.Initialize(FontFamily family, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont) at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet) at myDialog.InitializeComponent()
It happened nearly always (but in some rare occasions I could open the dialog and some clicks later it crashed). Not using the ImageButton helped also. The solution was to comment the unsafe code in ImageButton.GenerateImageList and use the safe (but slower) code.
I hope I'm not the only one who had this problem and I hope I could help those.
Wolfgang
Wolfgang Knauf
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
| |