WPF - Taskbar Notification ball

Hi I'm developing an wpf application and then I need to put in the taskbar a notification ball as in the image bellow

enter image description here

I am using this follow code

<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>

But I didn't find any thing about this on the documentation

The question is: Is there some way to do this ? if yes, I can use this code or Another Library ? Maybe I am referring this as Notification Ball cause I don't know its name technically

Upvotes: 2

Views: 3480

Answers (3)

Well after seaching for a while I really realize that I couldn't do this using an preset property so I had to draw an icon in my xaml and then render it as Icon

In my xaml I use this, I set the canvas Index -1; to make it hidden

        <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>

The code I used is this follow one

        //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
        }

This way I was able to creat a new icon with the NotificationBall The result was real great as in this pic bellow

enter image description here

Upvotes: 1

Andy
Andy

Reputation: 12276

If you don't care for p/invoke then you can use the windows forms notifyicon mechanism as explained here: http://www.abhisheksur.com/2012/08/notifyicon-with-wpf-applications.html

You didn't ask about any other functionality, but this implementation is wpf specific and does all sorts of fancy stuff as well: https://www.codeproject.com/Articles/36468/WPF-NotifyIcon#show_hide

You can also add action center messages by using windows 8+ sdk dll. This is quite nice if you want to show notification messages. They have optional pictures and a variety of formats. As one is added a notification appears in the action center. That might conceivably obviate your requirement to change your app icon.

Upvotes: 2

Pavan Chandaka
Pavan Chandaka

Reputation: 12801

May be there are other ways to do it.

But I know an option to do it in code using WINAPI

There is function called Shell_NotifyIcon in winAPI. You have to import shell32.dll

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

This is a good link to begin with.

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

You can easily find the code by searching for using shell_notifyicon in c#.

Upvotes: 1

Related Questions