【问题标题】:WPF - Taskbar Notification ballWPF - 任务栏通知球
【发布时间】:2018-03-27 21:13:33
【问题描述】:

您好,我正在开发一个 wpf 应用程序,然后我需要在任务栏中放置一个通知球,如下图所示

我正在使用以下代码

<tb:TaskbarIcon x:Name="IconeDeNotificacao" 
                IconSource="../Images/icon-one.ico"
                    ToolTipText="ArquivarNfe"
                    TrayLeftMouseUp="ShowWindow_Click">

        <!-- Set a simple context menu  -->
        <!-- the data context of the context menu is the NotifyIcon itself (see more about this in DataBinding samples) -->


<tb:TaskbarIcon.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Abrir Sistema"
                      Command="{commands:ShowSampleWindowCommand}"
                      CommandParameter="TelaPrincipal">
                <MenuItem.Icon>
                    <fa:ImageAwesome Icon="PlayCircle" Width="10"></fa:ImageAwesome>
                </MenuItem.Icon>
            </MenuItem>

            <Separator />

            <MenuItem Header="Esconder Sistema"
                      Command="{commands:HideSampleWindowCommand}"
                      CommandParameter="TelaPrincipal">
                <MenuItem.Icon>
                    <fa:ImageAwesome Icon="Pause" Width="10"></fa:ImageAwesome>
                </MenuItem.Icon>
            </MenuItem>

            <Separator />

            <MenuItem Header="Configurações"
                      Command="{commands:ConfiguracoesWindowCommand}"
                      CommandParameter="TelaPrincipal">
                <MenuItem.Icon>
                    <fa:ImageAwesome Icon="Cog" Width="10"></fa:ImageAwesome>
                </MenuItem.Icon>
            </MenuItem>
            <Separator />
          </ContextMenu>
        </tb:TaskbarIcon.ContextMenu>
    </tb:TaskbarIcon>

但我在the documentation 上没有找到任何关于此的信息

问题是:有没有办法做到这一点? 如果是,我可以使用此代码或另一个库吗? 也许我将其称为通知球,因为从技术上讲我不知道它的名字

【问题讨论】:

标签: c# wpf xaml taskbar


【解决方案1】:

如果您不关心 p/invoke,那么您可以使用 windows 窗体 notifyicon 机制,如下所述: http://www.abhisheksur.com/2012/08/notifyicon-with-wpf-applications.html

您没有询问任何其他功能,但这个实现是 wpf 特定的,并且还做了各种花哨的东西: https://www.codeproject.com/Articles/36468/WPF-NotifyIcon#show_hide

您还可以使用 windows 8+ sdk dll 添加操作中心消息。如果您想显示通知消息,这非常好。他们有可选的图片和多种格式。添加一个通知时,操作中心会显示一条通知。可以想象,这可以避免您更改应用程序图标的要求。

【讨论】:

    【解决方案2】:

    可能还有其他方法可以做到这一点。

    但我知道使用 WINAPI 在代码中执行此操作的选项

    winAPI 中有一个叫做Shell_NotifyIcon 的函数。 你必须导入 shell32.dll

    [DllImport("shell32.dll")]
     static extern bool Shell_NotifyIcon(uint dwMessage,
        [In] ref NOTIFYICONDATA pnid);
    

    这是一个很好的开始链接。

    https://www.pinvoke.net/default.aspx/shell32.shell_notifyicon

    在c#中使用shell_notifyicon搜索即可轻松找到代码。

    【讨论】:

    • 嗯,这不正是我正在寻找的通知气球,但无论如何谢谢你:) 我投了你一票
    【解决方案3】:

    经过一段时间的搜索,我真的意识到我无法使用预设属性来做到这一点,所以我不得不在我的 xaml 中绘制一个图标,然后将其渲染为图标

    在我使用的 xaml 中,我将画布索引设置为 -1;把它隐藏起来

            <Canvas x:Name="CanvasIcon" HorizontalAlignment="Left" VerticalAlignment="Top" Width="32" Height="32" Panel.ZIndex="2">
                <Border x:Name="CanvasIconCount" Visibility="Hidden" CornerRadius="5" Width="18  " Height="19" Background="Red" Panel.ZIndex="1" Margin="5 0 0 10">
                    <Label  Foreground="White" Padding="0" FontSize="14" FontWeight="Medium" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">9</Label>
                </Border>
                <Image Margin="-4 0 0 0" Source="../Images/icon-one.ico" Width="32" Height="32"></Image>
            </Canvas>
    

    我使用的代码是这个跟随一个

            //gets the canvas content
            public static void SaveCanvas(Window window, Canvas canvas, int dpi)
            {
    
                var rtb = new RenderTargetBitmap(
                    (int)canvas.Width, //width 
                    (int)canvas.Height, //height 
                    dpi, //dpi x 
                    dpi, //dpi y 
                    PixelFormats.Pbgra32 // pixelformat 
                    );
                rtb.Render(canvas);
                SaveRTBAsPNG(rtb;
            }
    
            // convert the canvas to png and creat a transparent Icon
            private static void SaveRTBAsPNG(RenderTargetBitmap bmp)
            {
                PngBitmapEncoder enc = new PngBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bmp));
                MemoryStream iconStreamBlue = new MemoryStream();
                enc.Save(iconStreamBlue);
                Bitmap bmpa =(Bitmap)System.Drawing.Image.FromStream(iconStreamBlue);
                IntPtr Hicon = bmpa.GetHicon();
                Icon myIcon = System.Drawing.Icon.FromHandle(Hicon);
                //Set the icon to  the taskIconBar
                IconeDeNotificacao.Icon = myIcon; //set the new  icon
            }
    

    通过这种方式,我能够使用 NotificationBall 创建一个新图标 结果真的很棒,如下图所示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多