版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第WPF實現(xiàn)基礎控件之托盤的示例代碼項目使用MIT開源許可協(xié)議。
新建NotifyIcon自定義控件繼承自FrameworkElement。
創(chuàng)建托盤程序主要借助與Win32API:
注冊窗體對象RegisterClassEx。注冊消息獲取對應消息標識IdRegisterWindowMessage。創(chuàng)建窗體(本質上托盤在創(chuàng)建時需要一個窗口句柄,完全可以將主窗體的句柄給進去,但是為了更好的管理消息以及托盤的生命周期,通常會創(chuàng)建一個獨立不可見的窗口)CreateWindowEx。
以下2點需要注意:
托盤控件的ContextMenu菜單MenuItem在使用binding時無效,是因為DataContext沒有帶過去,需要重新賦值一次。托盤控件發(fā)送ShowBalloonTip消息通知時候需新建Shell_NotifyIcon。Nuget最新Install-PackageWPFDevelopers-preview
示例代碼
1)NotifyIcon.cs代碼如下:
using
System;
using
System.IO;
using
System.Runtime.InteropServices;
using
System.Threading;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Controls.Primitives;
using
System.Windows.Data;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Imaging;
using
WPFDevelopers.Controls.Runtimes;
using
WPFDevelopers.Controls.Runtimes.Interop;
using
WPFDevelopers.Controls.Runtimes.Shell32;
using
WPFDevelopers.Controls.Runtimes.User32;
namespace
WPFDevelopers.Controls
public
class
NotifyIcon
:
FrameworkElement,
IDisposable
{
private
static
NotifyIcon
NotifyIconCache;
public
static
readonly
DependencyProperty
ContextContentProperty
=
DependencyProperty.Register(
"ContextContent",
typeof(object),
typeof(NotifyIcon),
new
PropertyMetadata(default));
public
static
readonly
DependencyProperty
IconProperty
=
DependencyProperty.Register("Icon",
typeof(ImageSource),
typeof(NotifyIcon),
new
PropertyMetadata(default,
OnIconPropertyChanged));
public
static
readonly
DependencyProperty
TitleProperty
=
DependencyProperty.Register("Title",
typeof(string),
typeof(NotifyIcon),
new
PropertyMetadata(default,
OnTitlePropertyChanged));
public
static
readonly
RoutedEvent
ClickEvent
=
EventManager.RegisterRoutedEvent("Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(NotifyIcon));
public
static
readonly
RoutedEvent
MouseDoubleClickEvent
=
EventManager.RegisterRoutedEvent("MouseDoubleClick",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(NotifyIcon));
private
static
bool
s_Loaded
=
false;
private
static
NotifyIcon
s_NotifyIcon;
//這是窗口名稱
private
readonly
string
_TrayWndClassName;
//這個是窗口消息名稱
private
readonly
string
_TrayWndMessage;
//這個是窗口消息回調(窗口消息都需要在此捕獲)
private
readonly
WndProc
_TrayWndProc;
private
Popup
_contextContent;
private
bool
_doubleClick;
//圖標句柄
private
IntPtr
_hIcon
=
IntPtr.Zero;
private
ImageSource
_icon;
private
IntPtr
_iconHandle;
private
int
_IsShowIn;
//托盤對象
private
NOTIFYICONDATA
_NOTIFYICONDATA;
//這個是傳遞給托盤的鼠標消息id
private
int
_TrayMouseMessage;
//窗口句柄
private
IntPtr
_TrayWindowHandle
=
IntPtr.Zero;
//通過注冊窗口消息可以獲取唯一標識Id
private
int
_WmTrayWindowMessage;
private
bool
disposedValue;
public
NotifyIcon()
{
_TrayWndClassName
=
$"WPFDevelopers_{Guid.NewGuid()}";
_TrayWndProc
=
WndProc_CallBack;
_TrayWndMessage
=
"TrayWndMessageName";
_TrayMouseMessage
=
(int)WM.USER
+
1024;
Start();
if
(Application.Current
!=
null)
{
//Application.Current.MainWindow.Closed
+=
(s,
e)
=
Dispose();
Application.Current.Exit
+=
(s,
e)
=
Dispose();
}
NotifyIconCache
=
this;
}
static
NotifyIcon()
{
DataContextProperty.OverrideMetadata(typeof(NotifyIcon),
new
FrameworkPropertyMetadata(DataContextPropertyChanged));
ContextMenuProperty.OverrideMetadata(typeof(NotifyIcon),
new
FrameworkPropertyMetadata(ContextMenuPropertyChanged));
}
private
static
void
DataContextPropertyChanged(DependencyObject
d,
DependencyPropertyChangedEventArgs
e)
=
((NotifyIcon)d).OnDataContextPropertyChanged(e);
private
void
OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs
e)
{
UpdateDataContext(_contextContent,
e.OldValue,
e.NewValue);
UpdateDataContext(ContextMenu,
e.OldValue,
e.NewValue);
}
private
void
UpdateDataContext(FrameworkElement
target,
object
oldValue,
object
newValue)
{
if
(target
==
null
||
BindingOperations.GetBindingExpression(target,
DataContextProperty)
!=
null)
return;
if
(ReferenceEquals(this,
target.DataContext)
||
Equals(oldValue,
target.DataContext))
{
target.DataContext
=
newValue
this;
}
}
private
static
void
ContextMenuPropertyChanged(DependencyObject
d,
DependencyPropertyChangedEventArgs
e)
{
var
ctl
=
(NotifyIcon)d;
ctl.OnContextMenuPropertyChanged(e);
}
private
void
OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs
e)
=
UpdateDataContext((ContextMenu)e.NewValue,
null,
DataContext);
public
object
ContextContent
{
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年英語閱讀理解與翻譯能力專項試題
- 2026年物流法規(guī)與運輸合規(guī)案例試題
- 未來五年紡織科學服務企業(yè)數(shù)字化轉型與智慧升級戰(zhàn)略分析研究報告
- 未來五年集群通信系統(tǒng)設備企業(yè)縣域市場拓展與下沉戰(zhàn)略分析研究報告
- 未來五年地籍測繪服務企業(yè)縣域市場拓展與下沉戰(zhàn)略分析研究報告
- 未來五年軟木廢料企業(yè)數(shù)字化轉型與智慧升級戰(zhàn)略分析研究報告
- 未來五年畜禽肉雜碎企業(yè)數(shù)字化轉型與智慧升級戰(zhàn)略分析研究報告
- 未來五年證券信息軟件企業(yè)ESG實踐與創(chuàng)新戰(zhàn)略分析研究報告
- 2025年部編版五年級道德上冊期末測試
- 2025年化工生產安全管理與應急處理手冊
- 棄土場規(guī)范規(guī)章制度
- 2026年水下機器人勘探報告及未來五至十年深海資源報告
- 安徽省蕪湖市鳩江區(qū)2024-2025學年高一上學期期末考試生物試卷
- 2025年對中國汽車行業(yè)深度變革的觀察與思考報告
- 無糾紛自愿離婚協(xié)議書
- 四川省高等教育自學考試畢業(yè)生登記表【模板】
- 專題五 以新發(fā)展理念引領高質量發(fā)展
- GB/T 22417-2008叉車貨叉叉套和伸縮式貨叉技術性能和強度要求
- GB/T 20145-2006燈和燈系統(tǒng)的光生物安全性
- GB/T 1.1-2009標準化工作導則 第1部分:標準的結構和編寫
- 長興中學提前招生試卷
評論
0/150
提交評論