WPF實現(xiàn)基礎控件之托盤的示例代碼_第1頁
WPF實現(xiàn)基礎控件之托盤的示例代碼_第2頁
WPF實現(xiàn)基礎控件之托盤的示例代碼_第3頁
WPF實現(xiàn)基礎控件之托盤的示例代碼_第4頁
WPF實現(xiàn)基礎控件之托盤的示例代碼_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論