Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions Files.Launcher/Win32API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ public static (string icon, string overlay, bool isCustom) GetFileIconAndOverlay
if (hres == HRESULT.S_OK)
{
using var image = GetBitmapFromHBitmap(hbitmap);
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
iconStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
if (image != null)
{
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
iconStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
}
}
//Marshal.ReleaseComObject(fctry);
}
Expand Down Expand Up @@ -191,19 +194,23 @@ public static void SetVolumeLabel(string driveName, string newLabel)

private static Bitmap GetBitmapFromHBitmap(HBITMAP hBitmap)
{
Bitmap bmp = hBitmap.ToBitmap();

if (Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32)
try
{
Bitmap bmp = hBitmap.ToBitmap();
if (Image.GetPixelFormatSize(bmp.PixelFormat) < 32)
{
return bmp;
}
if (IsAlphaBitmap(bmp, out var bmpData))
{
return GetAlphaBitmapFromBitmapData(bmpData);
}
return bmp;
}

if (IsAlphaBitmap(bmp, out var bmpData))
catch
{
return GetAlphaBitmapFromBitmapData(bmpData);
return null;
}

return bmp;
}

private static Bitmap GetAlphaBitmapFromBitmapData(BitmapData bmpData)
Expand Down
26 changes: 24 additions & 2 deletions Files.Launcher/Win32API_ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,13 @@ private static void EnumMenuItems(
container.Dispose();
continue;
}
if (mii.hbmpItem != HBITMAP.NULL)
if (mii.hbmpItem != HBITMAP.NULL && !Enum.IsDefined(typeof(HBITMAP_HMENU), ((IntPtr)mii.hbmpItem).ToInt64()))
{
var bitmap = GetBitmapFromHBitmap(mii.hbmpItem);
menuItem.Icon = bitmap;
if (bitmap != null)
{
menuItem.Icon = bitmap;
}
}
if (mii.hSubMenu != HMENU.NULL)
{
Expand Down Expand Up @@ -429,5 +432,24 @@ public void Dispose()
}
}
}

// There is usually no need to define Win32 COM interfaces/P-Invoke methods here.
// The Vanara library contains the definitions for all members of Shell32.dll, User32.dll and more
// The ones below are due to bugs in the current version of the library and can be removed once fixed
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-menuiteminfoa
private enum HBITMAP_HMENU : long
{
HBMMENU_CALLBACK = -1,
HBMMENU_MBAR_CLOSE = 5,
HBMMENU_MBAR_CLOSE_D = 6,
HBMMENU_MBAR_MINIMIZE = 3,
HBMMENU_MBAR_MINIMIZE_D = 7,
HBMMENU_MBAR_RESTORE = 2,
HBMMENU_POPUP_CLOSE = 8,
HBMMENU_POPUP_MAXIMIZE = 10,
HBMMENU_POPUP_MINIMIZE = 11,
HBMMENU_POPUP_RESTORE = 9,
HBMMENU_SYSTEM = 1
}
}
}
17 changes: 9 additions & 8 deletions Files/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation.Collections;
Expand Down Expand Up @@ -219,15 +220,15 @@ public virtual void SetShellContextmenu(MenuFlyout menuFlyout, bool shiftPressed
var maxItems = AppSettings.ShowAllContextMenuItems ? int.MaxValue : shiftPressed ? 6 : 4;
if (Connection != null)
{
var response = Connection.SendMessageAsync(new ValueSet()
var response = Task.Run(() => Connection.SendMessageAsync(new ValueSet()
{
{ "Arguments", "LoadContextMenu" },
{ "FilePath", IsItemSelected ?
string.Join('|', selectedItems.Select(x => x.ItemPath)) :
ParentShellPageInstance.FilesystemViewModel.CurrentFolder.ItemPath},
{ "ExtendedMenu", shiftPressed },
{ "ShowOpenMenu", showOpenMenu }
}).AsTask().Result;
{ "Arguments", "LoadContextMenu" },
{ "FilePath", IsItemSelected ?
string.Join('|', selectedItems.Select(x => x.ItemPath)) :
ParentShellPageInstance.FilesystemViewModel.CurrentFolder.ItemPath},
{ "ExtendedMenu", shiftPressed },
{ "ShowOpenMenu", showOpenMenu }
}).AsTask()).Result;
if (response.Status == AppServiceResponseStatus.Success
&& response.Message.ContainsKey("Handle"))
{
Expand Down
2 changes: 1 addition & 1 deletion Files/UserControls/NavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@
<MenuFlyoutItem
x:Name="NewFile"
x:Uid="BaseLayoutContextFlyoutNewFile"
Command="{x:Bind NewFileInvokedCommand}"
Command="{x:Bind NewFileInvokedCommand, Mode=OneWay}"
CommandParameter="{x:Null}"
IsEnabled="{x:Bind CanCreateFileInPage, Mode=OneWay}"
Text="File">
Expand Down