Is it possible to add a progress bar to the taskbar icon of a Windows Forms App? [duplicate] - vb.net

Windows 7 has an AWESOME new feature that applications can report the progress of the current activity through the status bar. For example, when copying file(s) using Windows Explorer, a progress bar is layered on top of the application icon in the task bar and the progress is shown as it updates.
What is the API for exposing the progress bar? Is there MSDN documentation on it?

For below .NET 4, or WinForms in any .NET version
Using the Windows API Code Pack from Microsoft (as Keeron mentioned), it's really simple. You just need to use the TaskbarManager. E.g.
To start the progress:
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
To update the progress:
TaskbarManager.Instance.SetProgressValue(currentValue, maxProgressValue);
And when when you're done, to end the progress:
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
There is more you can do, but that should get you started and might be all you need.
For .NET 4 and above with WPF
You can use System.Windows.Shell.TaskbarItemInfo. E.g. in the Xaml for your main window, you'll need to add:
<Window.TaskbarItemInfo>
<TaskbarItemInfo x:Name="taskBarItemInfo" />
</Window.TaskbarItemInfo>
Then to update the progress, you would do something like:
taskBarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
for (int i = 0; i < 100; i++)
{
taskBarItemInfo.ProgressValue = i / 100.0;
Thread.Sleep(50); // whatever the 'work' really is
}
taskBarItemInfo.ProgressState = TaskbarItemProgressState.None;
Don't forget that if you're doing the 'work' on a background thread (which is probably a good idea for long running tasks), you will need to switch back to the UI thread to update the taskbar.

There's a good article in MSDN magazine about the new taskbar APIs. And yes, the feature is awesome :-)
Essentially, it's all about implementing IFileOperation. There's a good article about using it in managed code here.

If you plan to use other Windows 7 Taskbar features, another approach would be to use the library from Microsoft: Windows API Code Pack for .NET Framework which is no longer available at the old link, but can be found on nuget.

I've written an article about implementing the Windows 7 Taskbar progress API in C# (see: Windows 7 Taskbar Progress Bar with C# and .NET). The control is open source (BSD) and has example projects for C# and VB.NET.
This way you don't have to convert the C++ code from scratch.

Actually I use Telerik's RadWindow which you cannot just use <telerik:RadWindow.TaskbarItemInfo>. So I use this workaround for net6.0-windows WPF:
In code behind file I made a property:
public Lazy<TaskbarItemInfo> TaskbarItemInfo { get; set; } = new Lazy<TaskbarItemInfo>(() =>
{
return System.Windows.Application.Current.MainWindow.TaskbarItemInfo = new TaskbarItemInfo();
});
In method part of BackgroundWorker
private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
I set the value of the progress:
TaskbarItemInfo.Value.ProgressState = TaskbarItemProgressState.Normal;
TaskbarItemInfo.Value.ProgressValue = (double)progressUserState.ProgressInPercent / 100;
In
private void WorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
I reset the state:
TaskbarItemInfo.Value.ProgressValue = 0;
TaskbarItemInfo.Value.ProgressState = TaskbarItemProgressState.None;

Related

Remove the height limit of an app targeting Windows 8.1, but running on Windows 10?

My question sounds simple: is it possible to remove the height limit of an app, if it is targeting Windows 8.1, but is also distributed to Windows 10 devices? Or is it my only choice just to upgrade project to target Windows 10, and distribute a separate package for it?
A related question:
How to specify initial window size for Windows 8.1 app running on Windows 10
..and an answer suggests "You could use reflection to call part of windows 10 sdk at runtime within your windows 8.1 app". Well, I have turned the whole Internet inside out, and I didn't find any normal explanation of this mystical method.
The method you've mentioned is use reflection to call ApplicationView.SetPreferredMinSize method at run time within Windows 8.1 app like following:
var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
var setPreferredMinSizeMethod = appView.GetType().GetRuntimeMethod("SetPreferredMinSize", new Type[] { typeof(Size) });
if (setPreferredMinSizeMethod != null)
{
setPreferredMinSizeMethod.Invoke(appView, new object[] { new Size(300, 300) });
}
Using this approach, you can reset the preferred min size and remove the default 768 height limit.
But please note that on Windows 10, Windows 8.1 app's ApplicaitonView is mainly controlled by system. If not necessary, please do not use reflection to do this.

Trouble plotting with ILNumerics in LinqPad - ILPanel think it is in design mode

I'm having some trouble using ILNumerics in LinqPad. I have the following code in LinqPad:
void Main()
{
var scene = new ILScene {
new ILPlotCube(twoDMode: false) {
new ILSurface(ILSpecialData.sincf(40, 60, 2.5f)) {
Wireframe = { Color = Color.FromArgb(50, Color.LightGray) },
Colormap = Colormaps.Jet
}
}
};
scene.First<ILPlotCube>().Rotation = Matrix4.Rotation(new Vector3(1f, 0.23f, 1f), 0.7f);
scene.Camera.Add(new ILSphere());
var panel = new ILPanel { Scene = scene };
PanelManager.DisplayControl(panel);
}
This code results in a big blue circle (with the text "ILNumerics ILPanel (OpenGL)" in the center) in the "custom" linqpad tab. The "Results" tab in linqpad contains the following text:
Determining Design Mode...
Entry Assembly: (null)
CurrentTypeAssembly: ILNumerics32, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null
Loaded Assemblies:
Design Mode: True
Questions:
Is possible render this as a WPF element instead of a WinForms control? (I guess this will render the plot successfully)
Alternatively; is it possible to "trick" the ILPanel to think it isn't being rendered in design mode?
1) ILNumerics targets Winforms only. But you may try to use a WPF WindowsFormsHost container? I don't have any experience with it and do not expect an improvement for your situation though.
2) The way ILNumerics is checking DesignMode right now: if the executing assembly (ILNumerics.dll) is not in the list of references of the entry assembly (LinqPad), DesignMode is considered. Therefore, I can see 2 "tricks":
Make LinqPad depend on ILNumerics. Probably not the best solution though.
Patch ILNumerics: Alter the helper method refered to by Joe Albahari to return false instead.
The second "trick" may could lead us to a future solution. I don't know, if the "normal" DesignMode property does work in conjunction with LinqPad either? Maybe we could combine the existing method with a settings switch for all uncommon cases.
Yes, you can render WPF controls in LINQPad - either by using PanelManager.DisplayWpfElement (or just by dumping it).
I don't think it will help you though, because ILNumerics uses Windows Forms only. It doesn't reference any of the WPF libraries.
I don't know whether it's possible to trick ILNumerics into thinking it's not in design mode. Take a look in Reflector at ILNumerics.Drawing.ILHelper.IsDesignMode. It's doing something dodgy with referenced assemblies. I don't know why they don't just check the control's DesignMode property - that's the normal way of doing it.

Microsoft AdControl stealing focus - Windows 8 MonoGame

I am currently writing a simple snake clone game for Windows 8 using MonoGame. I am using the XAML - MonoGame template and trying to include advertising support. I have found an issue, pretty sure it's with the AdControl itself, not MonoGame, however it is stealing keyboard focus every time an ad is loaded.
I have tried to reinitialize the MonoGame 'MetroGameWindow' instance to try and get focus back with no luck. Eg,
void GamePage_LostFocus(object sender, RoutedEventArgs e)
{
MetroGameWindow.Instance.Initialize(Window.Current.CoreWindow,this)
// 'this' is 'GamePage' which inherits from 'SwapChainBackgroundPanel'
}
Does any one know any workarounds for this problem? Any help would be appreciated.
This ia a known problem with AdControl. As of now best solution is to set IsEnabled property of AdControl to false. Doing so will prevent AdControl from taking focus on ad reloads while remaining clickable. See following discussion on bing ads forum: http://community.bingads.microsoft.com/ads/en/publisher/f/63/t/73548.aspx

Add dropdown to settings charm in Windows 8 Metro (C#)

I found how can I add command (looks like link) to settings charm:
SettingsPane.GetForCurrentView().CommandsRequested += MainPage_CommandsRequested;
void MainPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
var cmd = new SettingsCommand("feedback", "Send feedback", new Windows.UI.Popups.UICommandInvokedHandler(x =>
{
App.ShowSendFeedback();
}));
args.Request.ApplicationCommands.Clear();
args.Request.ApplicationCommands.Add(cmd);
}
Now I need adding dropdown list for language selection to settings charm. How can I achieve it?
See also this blog post, which explains how you can display a custom user control whenever one of your settings are selected.
Basically, he moves an off-screen user control on-screen and then hides it again when user interaction is done. To me this feels kind of hack-ish... But I guess that's where WinRT is right now.
You can't add it directly to the Win8 UI. The idea is to publish 'command' links into the Win8 UI and then, when they are clicked, your app gets notified. At that point, you show your own UI with whatever widgets you want in it. See any of the Settings samples in the SDK for an example.

How to play a sound in Silverlight without XAML

Looks like MediaElement requires XAML to work.
So this simple code doesn't work :
MediaElement me = new MediaElement();
me.Volume = 1;
me.AutoPlay = false;
me.Source = new Uri("http://www.robtowns.com/music/blind_willie.mp3");
me.Play();
Is there another way to play sound in Silverlight 4 with MVVM - or do I have to find a placeholder for the media element in my XAML ?
This thread describes how to use XNA for Win Phone 7 - but I'm talking regular Silverlight
http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/60e7e4b4-31dc-4a81-bdfb-e2c80e761a1c
The control still needs to be in the visual tree to operate. If you don't want it to actually appear, you could have it collapsed. But you'll need to do something like:
me.Visibility = Visibility.Collapsed;
LayoutRoot.Children.Add(me);