Skip to content

Commit

Permalink
.NET8.0 added
Browse files Browse the repository at this point in the history
UIImage added
  • Loading branch information
harborsiem committed Nov 15, 2023
1 parent 4bff53b commit 4b73713
Show file tree
Hide file tree
Showing 7 changed files with 1,035 additions and 822 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
#### Changed (Ribbon)

- Destroy EventLogger.
- Static method GetBitmap(IUIImage) in Ribbon class added.
- Hide Interface methods Execute, UpdateProperty, GetValue
- Update Ribbon Size for Designer
- New Name property (RibbonButton, RibbonComboBox, ...) for easier debugging. One can set the Name after instantiation the RibbonButton, ...
- Create method in GalleryItemEventArgs, ColorPickerEventArgs, FontControlEventArgs add a sender parameter for checking
- UIImage class new designed. This class can help for handling between Bitmap class and IUIImage interface

#### Changed (RibbonTools)

Expand Down
111 changes: 4 additions & 107 deletions Ribbon/Ribbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;
using MethodInvoker = System.Windows.Forms.MethodInvoker;

namespace RibbonLib
{
Expand All @@ -33,6 +34,7 @@ public class Ribbon : Control, IUICommandHandler
private const string uriFile = "file://";

private IUIImageFromBitmap _imageFromBitmap;
private UIImage _uiImage;
private RibbonUIApplication _application;
private Dictionary<uint, IRibbonControl> _mapRibbonControls = new Dictionary<uint, IRibbonControl>();
internal Dictionary<uint, IRibbonControl> MapRibbonControls { get { return _mapRibbonControls; } }
Expand Down Expand Up @@ -543,6 +545,7 @@ void InitFramework(string resourceName, IntPtr hInstance)
// create ribbon framework object
Framework = CreateRibbonFramework();
_imageFromBitmap = CreateImageFromBitmapFactory();
_uiImage = new UIImage(_imageFromBitmap);

// create ribbon application object
_application = new RibbonUIApplication(this, this);
Expand Down Expand Up @@ -602,6 +605,7 @@ void DestroyFramework()

if (_imageFromBitmap != null)
{
_uiImage?.Dispose();
// remove reference to imageFromBitmap object
_imageFromBitmap = null;
}
Expand Down Expand Up @@ -1240,112 +1244,5 @@ public IntPtr MarkupHandle
return _loadedDllHandle;
}
}

/// <summary>
/// Get a Bitmap from an IUIImage
/// </summary>
/// <param name="uiImage"></param>
/// <returns></returns>
public static unsafe Bitmap GetBitmap(IUIImage uiImage)
{
if (uiImage == null)
throw new ArgumentNullException(nameof(uiImage));
IntPtr hBitmap; //HBITMAP
uiImage.GetBitmap(out hBitmap);
// Create the BITMAP structure and get info from our nativeHBitmap
//this is a workaround because GDI+ did it not correct for 32 bit Bitmaps
BITMAP bitmapStruct = new BITMAP();
int bitmapSize = Marshal.SizeOf(bitmapStruct);
int size = PInvoke.GetObject(hBitmap, bitmapSize, &bitmapStruct);
//if (size != bitmapSize)
// return null;
Bitmap managedBitmap;
if (Has32BitAlpha(ref bitmapStruct))
{
// Create the managed bitmap using the pointer to the pixel data of the native HBitmap
managedBitmap = new Bitmap(
bitmapStruct.bmWidth, bitmapStruct.bmHeight, bitmapStruct.bmWidthBytes, PixelFormat.Format32bppArgb, (IntPtr)bitmapStruct.bmBits);
if (bitmapStruct.bmHeight > 0)
managedBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
}
else
{
managedBitmap = Bitmap.FromHbitmap(hBitmap);
}
//PInvoke.DeleteObject(hBitmap); //Maybe not a good idea
return managedBitmap;
}

private static Bitmap TryConvertToAlphaBitmap(Bitmap bitmap)
{
if (bitmap.PixelFormat == PixelFormat.Format32bppRgb && bitmap.RawFormat.Guid == ImageFormat.Bmp.Guid)
{
BitmapData bmpData = null;
try
{
bmpData = bitmap.LockBits(new Rectangle(new Point(), bitmap.Size), ImageLockMode.ReadOnly, bitmap.PixelFormat);
if (BitmapHasAlpha(bmpData))
{
Bitmap alpha = new Bitmap(bitmap.Width, bitmap.Height, bmpData.Stride, PixelFormat.Format32bppArgb, bmpData.Scan0);
//Do not Dispose bitmap because we use the Scan0 data in the alpha Bitmap
//or is it better to copy all data from bitmap to alpha and Dispose bitmap ?
return alpha;
}
}
finally
{
if (bmpData != null)
bitmap.UnlockBits(bmpData);
}
}
return bitmap;
}

//From Microsoft System.Drawing.Icon.cs
private unsafe static bool BitmapHasAlpha(BitmapData bmpData)
{
bool hasAlpha = false;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 3; j < Math.Abs(bmpData.Stride); j += 4)
{
// Stride here is fine since we know we're doing this on the whole image.
unsafe
{
byte* candidate = unchecked(((byte*)bmpData.Scan0.ToPointer()) + (i * bmpData.Stride) + j);
if (*candidate != 0)
{
hasAlpha = true;
return hasAlpha;
}
}
}
}

return false;
}

private static unsafe bool Has32BitAlpha(ref BITMAP bitmapStruct)
{
if (bitmapStruct.bmBitsPixel == 32)
{
for (int i = 0; i < bitmapStruct.bmHeight; i++)
{
for (int j = 3; j < Math.Abs(bitmapStruct.bmWidthBytes); j += 4)
{
// Stride here is fine since we know we're doing this on the whole image.
unsafe
{
byte* candidate = unchecked(((byte*)bitmapStruct.bmBits) + (i * bitmapStruct.bmWidthBytes) + j);
if (*candidate != 0)
{
return true;
}
}
}
}
}
return false;
}
}
}
1 change: 1 addition & 0 deletions Ribbon/Ribbon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<Compile Include="RibbonUIApplication.cs" />
<Compile Include="Controls\RibbonSpinner.cs" />
<Compile Include="UICollectionChangedEventArgs.cs" />
<Compile Include="UIImage.cs" />
<Compile Include="Util.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
25 changes: 5 additions & 20 deletions Ribbon/RibbonCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net7.0-windows;net6.0-windows;net40</TargetFrameworks>
<TargetFrameworks>net8.0-windows;net7.0-windows;net6.0-windows;net40</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon />
Expand All @@ -25,14 +25,14 @@
<Company>RibbonLib</Company>
<PackageIcon>Ribbon64.png</PackageIcon>
<Title>Windows Ribbon Library for WinForms</Title>
<Description>Windows Ribbon for WinForms is a .NET wrapper for Windows Ribbon control. It will allow WinForms developers to use Microsoft Windows Ribbon control in their WinForms applications.
<Description>Windows Ribbon for WinForms is a .NET wrapper for Windows Ribbon Framework. It will allow WinForms developers to use Microsoft Windows Ribbon Framework in their WinForms applications.
Former developers: Arik Poznanski, Bernhard Elbl
</Description>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageReleaseNotes>
See CHANGELOG.md
</PackageReleaseNotes>
<PackageTags>ribbon;winforms;windows-forms;RibbonTools</PackageTags>
<PackageTags>Ribbon Framework;ribbon;winforms;windows-forms;RibbonTools</PackageTags>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

Expand All @@ -41,27 +41,12 @@
<Deterministic>false</Deterministic>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU' and ('$(TargetFramework)' == 'net7.0-windows' or '$(TargetFramework)' == 'net6.0-windows')">
<DefineConstants>DEBUG;Core</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU' and '$(TargetFramework)' == 'net40'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU' and '$(TargetFramework)' == 'net7.0-windows'">
<DefineConstants>TRACE;Core</DefineConstants>
<DocumentationFile>.\bin\Release\net7.0-windows\Ribbon.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU' and '$(TargetFramework)' == 'net6.0-windows'">
<DefineConstants>TRACE;Core</DefineConstants>
<DocumentationFile>.\bin\Release\net6.0-windows\Ribbon.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU' and '$(TargetFramework)' == 'net40'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>.\bin\Release\net40\Ribbon.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup>
Expand Down
Loading

0 comments on commit 4b73713

Please sign in to comment.