摘要:本文主要向大家介绍了C#编程之C# Winform WPF DeskBand 窗体嵌入任务栏,在任务栏显示文字,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之C# Winform WPF DeskBand 窗体嵌入任务栏,在任务栏显示文字,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
最近写了个小程序,用于将固态硬盘的写入量等信息显示在任务栏,最开始使用Windows API也可以实现,但是当任务栏托盘增加的时候,会被遮盖,最终采用了DeskBand来实现,填了很多坑。
参考的GitHub地址:https://github.com/dsafa/CSDeskBand
DeskBand相关代码如下:
COLORREF:
// This code snippet was used by SharpShell.
//
using System.Drawing;
using System.Runtime.InteropServices;
namespace MyDiskInfo.Interop
{
[StructLayout(LayoutKind.Sequential)]
public struct COLORREF
{
public COLORREF(Color color)
{
Dword = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
}
public uint Dword;
public Color Color
{
get
{
return Color.FromArgb(
(int)(0x000000FFU & Dword),
(int)(0x0000FF00U & Dword) >> 8,
(int)(0x00FF0000U & Dword) >> 16);
}
}
}
}
View Code
DESKBANDINFO:
using System;
using System.Runtime.InteropServices;
namespace MyDiskInfo.Interop
{
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DESKBANDINFO
{
///
///
public DBIM dwMask;
///
public POINT ptMinSize;
///
public POINT ptMaxSize;
///
///
public POINT ptIntegral;
///
public POINT ptActual;
///
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public String wszTitle;
///
///
public DBIMF dwModeFlags;
///
///
public COLORREF crBkgnd;
///
[Flags]
public enum DBIF
{
///
DBIF_VIEWMODE_NORMAL = 0x0000,
///
DBIF_VIEWMODE_VERTICAL = 0x0001,
///
DBIF_VIEWMODE_FLOATING = 0x0002,
///
DBIF_VIEWMODE_TRANSPARENT = 0x0004
}
///
[Flags]
public enum DBIM
{
///
DBIM_MINSIZE = 0x0001,
///
DBIM_MAXSIZE = 0x0002,
///
DBIM_INTEGRAL = 0x0004,
///
DBIM_ACTUAL = 0x0008,
///
DBIM_TITLE = 0x0010,
///
DBIM_MODEFLAGS = 0x0020,
///
DBIM_BKCOLOR = 0x0040
}
///
[Flags]
public enum DBIMF : uint
{
///
DBIMF_NORMAL = 0x0000,
///
DBIMF_FIXED = 0x0001,
///
DBIMF_FIXEDBMP = 0x0004,
///
DBIMF_VARIABLEHEIGHT = 0x0008,
///
DBIMF_UNDELETEABLE = 0x0010,
///
DBIMF_DEBOSSED = 0x0020,
///
DBIMF_BKCOLOR = 0x0040,
///
DBIMF_USECHEVRON = 0x0080,
///
DBIMF_BREAK = 0x0100,
///
DBIMF_ADDTOFRONT = 0x0200,
///
DBIMF_TOPALIGN = 0x0400,
///
DBIMF_NOGRIPPER = 0x0800,
///
DBIMF_ALWAYSGRIPPER = 0x1000,
///
DBIMF_NOMARGINS = 0x2000
}
}
}
View Code
POINT:
// This code snippet was used by SharpShell.
//
using System.Runtime.InteropServices;
namespace MyDiskInfo.Interop
{
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct POINT
{
///
public int X;
///
public int Y;
}
}
View Code
RECT:
using System.Runtime.InteropServices;
namespace MyDiskInfo.Interop
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public int left, top, right, bottom;
public int Width()
{
return right - left;
}
public int Height()
{
return bottom - top;
}
public void Offset(int x, int y)
{
left += x;
right += x;
top += y;
bottom += y;
}
public void Set(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public bool IsEmpty()
{
return Width() == 0 && Height() == 0;
}
}
}
View Code
Attributes:
using System;
using System.Runtime.InteropServices;
namespace MyDiskInfo
{
///
[AttributeUsage(AttributeTargets.Class)]
public class DeskBandInfoAttribute : System.Attribute
{
private string _displayName;
private string _helpText;
public string DisplayName
{
get { return _displayName; }
}
public string HelpText
{
get { return _helpText; }
}
public DeskBandInfoAttribute() { }
public DeskBandInfoAttribute(string displayName, string helpText)
{
_displayName = displayName;
_helpText = helpText;
}
}
}
View Code
ComImport:
using System;
using System.Runtime.InteropServices;
namespace MyDiskInfo.Interop
{
///
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")]
public interface IObjectWithSite
{
///
///
A pointer to the IUnknown interface pointer of the site managing this object.
/// If NULL, the object should call Release on any existing site at which point the object no longer knows its site.
void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] Object pUnkSite);
///
///
The IID of the interface pointer that should be returned in ppvSite.
///
Address of pointer variable that receives the interface pointer requested in riid.
void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out Object ppvSite);
}
///
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("F1DB8392-7331-11D0-8C99-00A0C92DBFE8")]
public interface IInputObjectSite
{
///
///
The address of the IUnknown interface of the object gaining or losing the focus.
///
Indicates if the object has gained or lost the focus. If this value is nonzero, the object has gained the focus.
/// If this value is zero, the object has lost the focus.
///
[PreserveSig]
Int32 OnFocusChangeIS([MarshalAs(UnmanagedType.IUnknown)] Object punkObj, Int32 fSetFocus);
}
///
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("00000114-0000-0000-C000-000000000046")]
public interface IOleWindow
{
///
///
A pointer to a variable that receives the window handle.
///
[PreserveSig]
int GetWindow(out IntPtr phwnd);
///
///
TRUE if help mode should be entered; FALSE if it should be exited.
///
[PreserveSig]
int ContextSensitiveHelp(bool fEnterMode);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("012DD920-7B26-11D0-8CA9-00A0C92DBFE8")]
public interface IDockingWindow : IOleWindow
{
#region IOleWindow
[PreserveSig]
new int GetWindow(out IntPtr phwnd);
[PreserveSig]
new int ContextSensitiveHelp(bool fEnterMode);
#endregion
///
///
TRUE if the docking window object should show its window.
/// FALSE if the docking window object should hide its window and return its border space by calling SetBorderSpaceDW with zero values.
///
[PreserveSig]
int ShowDW([In] bool fShow);
///
///
Reserved. This parameter should always be zero.
///
[PreserveSig]
int CloseDW([In] UInt32 dwReserved);
///
///
Pointer to a RECT structure that contains the frame‘s available border space.
///
Pointer to the site‘s IUnknown interface. The docking window object should call the QueryInterface method for this interface, requesting IID_IDockingWindowSite.
/// The docking window object then uses that interface to negotiate its border space. It is the docking window object‘s responsibility to release this interface when it is no longer needed.
///
Reserved. This parameter should always be zero.
///
[PreserveSig]
int ResizeBorderDW(RECT prcBorder, [In, MarshalAs(UnmanagedType.IUnknown)] IntPtr punkToolbarSite, bool fReserved);
}
///
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EB0FE172-1A3A-11D0-89B3-00A0C90A90AC")]
public interface IDeskBand : IDockingWindow
{
#region IOleWindow
[PreserveSig]
new int GetWindow(out IntPtr phwnd);
[PreserveSig]
new int ContextSensitiveHelp(bool fEnterMode);
#endregion
#region IDockingWindow
[PreserveSig]
new int ShowDW([In] bool fShow);
[PreserveSig]
new int CloseDW([In] UInt32 dwReserved);
[PreserveSig]
new int ResizeBorderDW(RECT prcBorder, [In, MarshalAs(UnmanagedType.IUnknown)] IntPtr punkToolbarSite, bool fReserved);
#endregion
///
///
The identifier of the band, assigned by the container. The band object can retain this value if it is required.
///
The view mode of the band object. One of the following values: DBIF_VIEWMODE_NORMAL, DBIF_VIEWMODE_VERTICAL, DBIF_VIEWMODE_FLOATING, DBIF_VIEWMODE_TRANSPARENT.
///
Pointer to a DESKBANDINFO structure that receives the band information for the object. The dwMask member of this structure indicates the specific information that is being requested.
///
[PreserveSig]
int GetBandInfo(UInt32 dwBandID, DESKBANDINFO.DBIF dwViewMode, ref DESKBANDINFO pdbi);
}
///
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("79D16DE4-ABEE-4021-8D9D-9169B261D657")]
public interface IDeskBand2 : IDeskBand
{
#region IOleWindow
[PreserveSig]
new int GetWindow(out IntPtr phwnd);
[PreserveSig]
new int ContextSensitiveHelp(bool fEnterMode);
#endregion
#region IDockingWindow
[PreserveSig]
new int ShowDW([In] bool fShow);
[PreserveSig]
new int CloseDW([In] UInt32 dwReserved);
[PreserveSig]
new int ResizeBorderDW(RECT prcBorder, [In, MarshalAs(UnmanagedType.IUnknown)] IntPtr punkToolbarSite, bool fReserved);
#endregion
#region IDeskBand
[PreserveSig]
new int GetBandInfo(UInt32 dwBandID, DESKBANDINFO.DBIF dwViewMode, ref DESKBANDINFO pdbi);
#endregion
///
///
When this method returns, contains a BOOL indicating ability.
///
[PreserveSig]
int CanRenderComposited(out bool pfCanRenderComposited);
///
///
TRUE to enable the composition state; otherwise, FALSE.
///
[PreserveSig]
int SetCompositionState(bool fCompositionEnabled);
///
///
When this method returns, contains a BOOL that indicates state.
///
[PreserveSig]
int GetCompositionState(out bool pfCompositionEnabled);
}
}
View Code
DeskBand,这里和GitHub上的程序不同的是,DeskBand直接继承的ElementHost类,GitHub上的好像是多例的,我想要的是单例的,所以修改了一下。这里粘的代码是WPF的,如果你想实现Winform的,就让DeskBand继承UserControl,DeskBand代码如下:
// Copyright(c) 2017 Patrick Becker
//
// Visit the Project page for more information.
// https://github.com/patbec/TaskbarSampleExtension
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MyDiskInfo.Interop;
using System.Windows.Forms.Integration;
namespace MyDiskInfo
{
///
///
public class DeskBand : ElementHost, IObjectWithSite, IDeskBand2
{
private const int S_OK = 0;
private const int E_NOTIMPL = unchecked((int)0x80004001);
protected IInputObjectSite DeskBandSite;
public DeskBand()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Name = "DeskBand";
}
#region Properties
///
[Browsable(true)]
[DefaultValue("")]
public String Title { get; set; }
///
[Browsable(true)]
[DefaultValue(typeof(Size), "-1,-1")]
public Size MinSize { get; set; }
///
[Browsable(true)]
[DefaultValue(typeof(Size), "-1,-1")]
public Size MaxSize { get; set; }
///
[Browsable(true)]
[DefaultValue(typeof(Size), "-1,-1")]
public Size MinSizeVertical { get; set; }
///
[Browsable(true)]
[DefaultValue(typeof(Size), "-1,-1")]
public Size IntegralSize { get; set; }
#endregion
#region IObjectWithSite
public void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] Object pUnkSite)
{
if (DeskBandSite != null)
Marshal.ReleaseComObject(DeskBandSite);
DeskBandSite = (IInputObjectSite)pUnkSite;
}
public void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out Object ppvSite)
{
ppvSite = DeskBandSite;
}
#endregion
#region IDeskBand2
public virtual int CanRenderComposited(out bool pfCanRenderComposited)
{
pfCanRenderComposited = true;
return S_OK;
}
public int SetCompositionState(bool fCompositionEnabled)
{
fCompositionEnabled = true;
return S_OK;
}
public int GetCompositionState(out bool pfCompositionEnabled)
{
pfCompositionEnabled = false;
return S_OK;
}
public int GetBandInfo(uint dwBandID, DESKBANDINFO.DBIF dwViewMode, ref DESKBANDINFO pdbi)
{
if (pdbi.dwMask.HasFlag(DESKBANDINFO.DBIM.DBIM_MINSIZE))
{
// Support for a vertical taskbar
// Most examples have no support for a vertical taskbar. Who in hell uses their taskbar vertically? Me! Very practical on a 21:9 monitor.
if (dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_VERTICAL))
{
pdbi.ptMinSize.Y = this.MinSizeVertical.Width;
pdbi.ptMinSize.X = this.MinSizeVertical.Height;
}
else
{
pdbi.ptMinSize.X = this.MinSize.Width;
pdbi.ptMinSize.Y = this.MinSize.Height;
}
}
if (pdbi.dwMask.HasFlag(DESKBANDINFO.DBIM.DBIM_MAXSIZE))
{
if (dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_VERTICAL))
{
pdbi.ptMaxSize.Y = this.MaxSize.Width;
pdbi.ptMaxSize.X = this.MaxSize.Height;
}
else
{
pdbi.ptMaxSize.X = this.MaxSize.Width;
pdbi.ptMaxSize.Y = this.MaxSize.Height;
}
}
if (pdbi.dwMask.HasFlag(DESKBANDINFO.DBIM.DBIM_INTEGRAL))
{
if (dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_VERTICAL))
{
pdbi.ptIntegral.Y = this.IntegralSize.Width;
pdbi.ptIntegral.X = this.IntegralSize.Height;
}
else
{
pdbi.ptIntegral.X = this.IntegralSize.Width;
pdbi.ptIntegral.Y = this.IntegralSize.Height;
}
}
if (pdbi.dwMask.HasFlag(DESKBANDINFO.DBIM.DBIM_ACTUAL))
{
if (dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DESKBANDINFO.DBIF.DBIF_VIEWMODE_VERTICAL))
{
pdbi.ptActual.Y = this.Size.Width;
pdbi.ptActual.X = this.Size.Height;
}
else
{
pdbi.ptActual.X = this.Size.Width;
pdbi.ptActual.Y = this.Size.Height;
}
}
if (pdbi.dwMask.HasFlag(DESKBANDINFO.DBIM.DBIM_TITLE))
{
pdbi.wszTitle = this.Title;
}
pdbi.dwModeFlags = DESKBANDINFO.DBIMF.DBIMF_ALWAYSGRIPPER | DESKBANDINFO.DBIMF.DBIMF_NORMAL | DESKBANDINFO.DBIMF.DBIMF_VARIABLEHEIGHT;
pdbi.dwMask = pdbi.dwMask | DESKBANDINFO.DBIM.DBIM_BKCOLOR | DESKBANDINFO.DBIM.DBIM_TITLE; // Testen
return S_OK;
}
public int GetWindow(out IntPtr phwnd)
{
phwnd = Handle;
return S_OK;
}
public int ContextSensitiveHelp(bool fEnterMode)
{
return S_OK;
}
public int ShowDW([In] bool fShow)
{
if (fShow)
Show();
else
Hide();
return S_OK;
}
public virtual void ClearResources()
{
}
public int CloseDW([In] uint dwReserved)
{
ClearResources();
Dispose(true);
return S_OK;
}
public int ResizeBorderDW(RECT prcBorder, [In, MarshalAs(UnmanagedType.IUnknown)] IntPtr punkToolbarSite, bool fReserved)
{
return E_NOTIMPL;
}
#endregion
#region Register / Unregister
[ComRegisterFunctionAttribute]
public static void Register(Type t)
{
string guid = t.GUID.ToString("B");
DeskBandInfoAttribute[] deskBandInfo = (DeskBandInfoAttribute[])
t.GetCustomAttributes(typeof(DeskBandInfoAttribute), false);
// Register only the extension if the attribute DeskBandInfo is used.
if (deskBandInfo.Length == 1)
{
RegistryKey rkClass = Registry.ClassesRoot.CreateSubKey(@"CLSID\" + guid);
RegistryKey rkCat = rkClass.CreateSubKey("Implemented Categories");
string _displayName = t.Name;
string _helpText = t.Name;
if (deskBandInfo[0].DisplayName != null)
{
_displayName = deskBandInfo[0].DisplayName;
}
if (deskBandInfo[0].HelpText != null)
{
_helpText = deskBandInfo[0].HelpText;
}
rkClass.SetValue(null, _displayName);
rkClass.SetValue("MenuText", _displayName);
rkClass.SetValue("HelpText", _helpText);
// TaskBar
rkCat.CreateSubKey("{00021492-0000-0000-C000-000000000046}");
Console.WriteLine(String.Format("{0} {1} {2}", guid, _displayName, "successfully registered."));
}
else
{
Console.WriteLine(guid + " has no attributes");
}
}
[ComUnregisterFunctionAttribute]
public static void Unregister(Type t)
{
string guid = t.GUID.ToString("B");
DeskBandInfoAttribute[] deskBandInfo = (DeskBandInfoAttribute[])
t.GetCustomAttributes(typeof(DeskBandInfoAttribute), false);
if (deskBandInfo.Length == 1)
{
string _displayName = t.Name;
if (deskBandInfo[0].DisplayName != null)
{
_displayName = deskBandInfo[0].DisplayName;
}
Registry.ClassesRoot.CreateSubKey(@"CLSID").DeleteSubKeyTree(guid);
Console.WriteLine(String.Format("{0} {1} {2}", guid, _displayName, "successfully removed."));
}
else
{
Console.WriteLine(guid + " has no attributes");
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号