From 4e5769669092ede4457782be3cb06451fc137c77 Mon Sep 17 00:00:00 2001 From: kotoneme <43395785+kotoneme@users.noreply.github.com> Date: Sun, 23 Sep 2018 12:41:19 +0800 Subject: [PATCH 1/3] Show desk band after registration (#32) Add option to show deskband after registration --- src/CSDeskBand/CSDeskBandImpl.cs | 53 +++++++++++++++++-- .../CSDeskBandRegistrationAttribute.cs | 5 ++ src/CSDeskBand/CSDeskband.csproj | 1 + src/CSDeskBand/Interop/COM/ITrayDeskband.cs | 23 ++++++++ src/Sample.Win/UserControl1.cs | 2 +- src/Sample.Wpf/UserControl1.xaml.cs | 2 +- 6 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 src/CSDeskBand/Interop/COM/ITrayDeskband.cs diff --git a/src/CSDeskBand/CSDeskBandImpl.cs b/src/CSDeskBand/CSDeskBandImpl.cs index dbd1de5..ac26724 100644 --- a/src/CSDeskBand/CSDeskBandImpl.cs +++ b/src/CSDeskBand/CSDeskBandImpl.cs @@ -49,10 +49,10 @@ private void Options_PropertyChanged(object sender, System.ComponentModel.Proper } _logger.Debug("Deskband options have changed"); - var parent = (IOleCommandTarget) _parentSite; + var parent = (IOleCommandTarget)_parentSite; // Set pvaln to the id that was passed in SetSite // When int is marshalled to variant, it is marshalled as VT_i4. See default marshalling for objects - parent.Exec(ref CGID_DeskBand, (uint) tagDESKBANDCID.DBID_BANDINFOCHANGED, 0, IntPtr.Zero, IntPtr.Zero); + parent.Exec(ref CGID_DeskBand, (uint)tagDESKBANDCID.DBID_BANDINFOCHANGED, 0, IntPtr.Zero, IntPtr.Zero); } public int GetWindow(out IntPtr phwnd) @@ -223,6 +223,46 @@ public static void Register(Type t) subKey.CreateSubKey(ComponentCategoryManager.CATID_DESKBAND.ToString("B")); Console.WriteLine($"Succesfully registered deskband `{GetToolbarName(t)}` - GUID: {guid}"); + + if (GetToolbarRequestToShow(t)) + { + Console.WriteLine($"Request to show deskband."); + ///https://www.pinvoke.net/default.aspx/Interfaces.ITrayDeskband + ITrayDeskband csdeskband = null; + try + { + Type trayDeskbandType = System.Type.GetTypeFromCLSID(new Guid("E6442437-6C68-4f52-94DD-2CFED267EFB9")); + Guid deskbandGuid = t.GUID; + csdeskband = (ITrayDeskband)Activator.CreateInstance(trayDeskbandType); + if (csdeskband != null) + { + csdeskband.DeskBandRegistrationChanged(); + + if (csdeskband.IsDeskBandShown(ref deskbandGuid) == HRESULT.S_FALSE) + { + if (csdeskband.ShowDeskBand(ref deskbandGuid) != HRESULT.S_OK) + { + Console.WriteLine($"Error while trying to show deskband."); + } + if(csdeskband.DeskBandRegistrationChanged() == HRESULT.S_OK) + { + Console.WriteLine($"The deskband was Succesfully shown with taskbar.{Environment.NewLine}You may see the alert notice box from explorer call."); + } + } + } + } + catch (Exception e) + { + Console.WriteLine($"Error while trying to show deskband: {e.ToString()}"); + } + finally + { + if (csdeskband != null && Marshal.IsComObject(csdeskband)) + { + Marshal.ReleaseComObject(csdeskband); + } + } + } } catch (Exception) { @@ -257,12 +297,17 @@ internal static string GetToolbarName(Type t) return t.GetCustomAttribute(true)?.Name ?? t.Name; } + internal static bool GetToolbarRequestToShow(Type t) + { + return t.GetCustomAttribute(true)?.ShowDeskBand ?? false; + } + public int QueryContextMenu(IntPtr hMenu, uint indexMenu, uint idCmdFirst, uint idCmdLast, QueryContextMenuFlags uFlags) { _logger.Debug($"Was queried for context menu - index: {indexMenu} cmd first: {idCmdFirst}, cmd last: {idCmdLast}, flags: {uFlags}"); if (uFlags.HasFlag(QueryContextMenuFlags.CMF_DEFAULTONLY)) { - return HRESULT.MakeHResult((uint) HRESULT.S_OK, 0, 0); + return HRESULT.MakeHResult((uint)HRESULT.S_OK, 0, 0); } _menutStartId = idCmdFirst; @@ -358,7 +403,7 @@ public int Save(object pStm, bool fClearDirty) public void CloseDeskBand() { - var bandSite = (IBandSite) _parentSite; + var bandSite = (IBandSite)_parentSite; bandSite.RemoveBand(_id); } diff --git a/src/CSDeskBand/CSDeskBandRegistrationAttribute.cs b/src/CSDeskBand/CSDeskBandRegistrationAttribute.cs index b956047..b6f0ae5 100644 --- a/src/CSDeskBand/CSDeskBandRegistrationAttribute.cs +++ b/src/CSDeskBand/CSDeskBandRegistrationAttribute.cs @@ -15,5 +15,10 @@ public sealed class CSDeskBandRegistrationAttribute : Attribute /// The name is used to select the deskband from the toolbars menu. /// public string Name { get; set; } + + /// + /// Request ShowDeskBand after succesfully registered deskband + /// + public bool ShowDeskBand { get; set; } } } \ No newline at end of file diff --git a/src/CSDeskBand/CSDeskband.csproj b/src/CSDeskBand/CSDeskband.csproj index 3fdc786..149cfc7 100644 --- a/src/CSDeskBand/CSDeskband.csproj +++ b/src/CSDeskBand/CSDeskband.csproj @@ -97,6 +97,7 @@ + diff --git a/src/CSDeskBand/Interop/COM/ITrayDeskband.cs b/src/CSDeskBand/Interop/COM/ITrayDeskband.cs new file mode 100644 index 0000000..9fe5615 --- /dev/null +++ b/src/CSDeskBand/Interop/COM/ITrayDeskband.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace CSDeskBand.Interop.COM +{ + [ComImport, Guid("6D67E846-5B9C-4db8-9CBC-DDE12F4254F1"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + public interface ITrayDeskband + { + [PreserveSig] + int ShowDeskBand([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid); + [PreserveSig] + int HideDeskBand([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid); + [PreserveSig] + int IsDeskBandShown([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid); + [PreserveSig] + int DeskBandRegistrationChanged(); + } +} diff --git a/src/Sample.Win/UserControl1.cs b/src/Sample.Win/UserControl1.cs index 4afb33a..5fbd37d 100644 --- a/src/Sample.Win/UserControl1.cs +++ b/src/Sample.Win/UserControl1.cs @@ -11,7 +11,7 @@ namespace Sample.Win /// [ComVisible(true)] [Guid("5731FC61-8530-404C-86C1-86CCB8738D06")] - [CSDeskBandRegistration(Name = "Sample Winforms Deskband")] + [CSDeskBandRegistration(Name = "Sample Winforms Deskband", ShowDeskBand = true)] public partial class UserControl1 : CSDeskBandWin { private readonly WinEventDelegate _delegate; diff --git a/src/Sample.Wpf/UserControl1.xaml.cs b/src/Sample.Wpf/UserControl1.xaml.cs index e7d1b25..743cc07 100644 --- a/src/Sample.Wpf/UserControl1.xaml.cs +++ b/src/Sample.Wpf/UserControl1.xaml.cs @@ -15,7 +15,7 @@ namespace Sample.Wpf /// [ComVisible(true)] [Guid("89BF6B36-A0B0-4C95-A666-87A55C226986")] - [CSDeskBandRegistration(Name = "Sample WPF Deskband")] + [CSDeskBandRegistration(Name = "Sample WPF Deskband", ShowDeskBand = true)] public partial class UserControl1 : INotifyPropertyChanged { private Orientation _taskbarOrientation; From d5c7577496e2bc0b39af8686d8349ec2517af6a4 Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Wed, 3 Oct 2018 19:13:42 -0700 Subject: [PATCH 2/3] Update readme with better installation instructions - Add example for ShowDeskBand option for the CSDeskBandRegistration attribute --- README.md | 14 +++++++++----- build/install-examples.cmd | 6 ------ build/uninstall-examples.cmd | 6 ------ tools/install-example.bat | 23 +++++++++++++++++++++++ 4 files changed, 32 insertions(+), 17 deletions(-) delete mode 100644 build/install-examples.cmd delete mode 100644 build/uninstall-examples.cmd create mode 100644 tools/install-example.bat diff --git a/README.md b/README.md index 8701cb4..f3d4577 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,10 @@ Use `CSDeskBandWin` for winforms or `CSDeskBandWpf` for wpf - For a winforms usercontrol, inherit the `CSDeskBandWin` base class. _See Sample.Win_ - For a wpf usercontol, Set `CSdeskBandWpf` as the root element in the XAML. _See Sample.Wpf_ -Add `[ComVisible(true)]`, `[Guid("xx-xx-xx-xx-xx")]`, `[CSDeskBandRegistration()]` attributes to the class +Add `[ComVisible(true)]`, `[Guid("xx-xx-xx-xx-xx")]`, `[CSDeskBandRegistration()]` attributes to the class. +The `CSDeskBandRegistration` attribute allows you to configure: +- Name : The name of the deskband shown in the toolbars menu +- ShowDeskBand : True if the deskband should be shown automatically after registration ```C# @@ -31,7 +34,7 @@ using CSDeskBand; [ComVisible(true)] [Guid("5731FC61-8530-404C-86C1-86CCB8738D06")] -[CSDeskBandRegistration(Name = "Sample Winforms Deskband")] +[CSDeskBandRegistration(Name = "Sample Winforms Deskband", ShowDeskBand = True)] public partial class UserControl1 : CSDeskBandWin { ... @@ -43,8 +46,7 @@ Now you are ready to start working on it like a normal user control. **Patch notes will be in the [release](https://github.com/dsafa/CSDeskBand/releases) page** ### Deskband Installation -You need to start an elevated command prompt and be able to use `regasm.exe` -An easy way to do this is use the Developer Command Prompt for Visual Studio. Make sure that you use the correct version of regasm that matches your platform (x86/x64). +You need to start an elevated command prompt and be able to use `regasm.exe`. Make sure that you use the correct version of regasm that matches your platform (x86/x64). ``` cd Sample.Win\bin\Debug @@ -61,8 +63,10 @@ regasm Sample.Win.dll ``` _Note that GAC installation requires the assemblies to be [Strong-Named](https://docs.microsoft.com/en-us/dotnet/framework/app-domains/strong-named-assemblies)_ +Here is an [example .bat file](./tools/install-example.bat) for installing a deskband. + ## Examples -There are examples included for Winforms and WPF in the Sample.Win and Sample.Wpf projects +There are example deskbands included for Winforms and WPF in the [Sample.Win](https://github.com/dsafa/CSDeskBand/tree/master/src/Sample.Win) and [Sample.Wpf](https://github.com/dsafa/CSDeskBand/tree/master/src/Sample.Wpf) projects. ### Compatibility Tested on Windows 10 x64 diff --git a/build/install-examples.cmd b/build/install-examples.cmd deleted file mode 100644 index 295017b..0000000 --- a/build/install-examples.cmd +++ /dev/null @@ -1,6 +0,0 @@ -@echo off - -%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\regasm.exe /nologo /codebase "%~dp0..\src\Sample.Win\bin\Release\Sample.Win.dll" -%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\regasm.exe /nologo /codebase "%~dp0..\src\Sample.Wpf\bin\Release\Sample.Wpf.dll" - -pause diff --git a/build/uninstall-examples.cmd b/build/uninstall-examples.cmd deleted file mode 100644 index 828f1d8..0000000 --- a/build/uninstall-examples.cmd +++ /dev/null @@ -1,6 +0,0 @@ -@echo off - -%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\regasm.exe /nologo /unregister "%~dp0..\src\Sample.Win\bin\Release\Sample.Win.dll" -%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\regasm.exe /nologo /unregister "%~dp0..\src\Sample.Wpf\bin\Release\Sample.Wpf.dll" - -pause diff --git a/tools/install-example.bat b/tools/install-example.bat new file mode 100644 index 0000000..8b438f7 --- /dev/null +++ b/tools/install-example.bat @@ -0,0 +1,23 @@ +@echo OFF +title Install DeskBand +@echo ON +@setlocal enableextensions +@cd /d "%~dp0" + +rem Check permissions +net session >nul 2>&1 +if %errorLevel% == 0 ( + echo Administrative permissions confirmed. +) else ( + echo Please run this script with administrator permissions. + pause + goto EXIT +) + +if defined %PROGRAMFILES(x86)% ( + rem use /unregister to uninstall + %SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\regasm.exe /nologo /codebase "Sample.Win.dll" +) else ( + %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\regasm.exe /nologo /codebase "Sample.Win.dll" +) +pause \ No newline at end of file From 6f066913ad71a357784a89b6fc28ce6fe72c0d9c Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Wed, 3 Oct 2018 19:15:21 -0700 Subject: [PATCH 3/3] Update nuget dependencies --- src/CSDeskBand.Win/CSDeskband.Win.nuspec | 2 +- src/CSDeskBand.Wpf/CSDeskband.Wpf.nuspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CSDeskBand.Win/CSDeskband.Win.nuspec b/src/CSDeskBand.Win/CSDeskband.Win.nuspec index 4c64a48..689c0d0 100644 --- a/src/CSDeskBand.Win/CSDeskband.Win.nuspec +++ b/src/CSDeskBand.Win/CSDeskband.Win.nuspec @@ -12,7 +12,7 @@ Copyright 2018 Deskband - + diff --git a/src/CSDeskBand.Wpf/CSDeskband.Wpf.nuspec b/src/CSDeskBand.Wpf/CSDeskband.Wpf.nuspec index 323189a..8b76483 100644 --- a/src/CSDeskBand.Wpf/CSDeskband.Wpf.nuspec +++ b/src/CSDeskBand.Wpf/CSDeskband.Wpf.nuspec @@ -12,7 +12,7 @@ Copyright 2018 Deskband - +