12

I develop with VS2010 in C# and I would like to create a WPF Window which have a taskbar text different from the Window title. The property Title set both the window title and the taskbar text. Is there a way to set them separatly?

Nicolas
  • 6,289
  • 4
  • 36
  • 51
  • 2
    Of course it sets both of them. How often do you see an application whose windows' title bars and taskbar items have different captions? How would you even know to which window a taskbar item corresponds? – Cody Gray - on strike Jan 07 '11 at 17:06
  • A reason would be that the taskbar title is truncated, so could end up the same for several windows. Eg if the windows are "Customer Address - ABC Corporation" and "Customer Address - ABCDEFG Inc" you might get "Customer Address - ABC" for both. Would be nice to be able to change it to "Customer Add." so the important part shows. – James_UK_DEV Apr 05 '16 at 08:34
  • ...of course the MSDN recommendation is to have the distinguishing bit first: "ABC Corporation - Customer Address" truncates to something like "ABC Corporation - Customer Ad". Which makes some sense for truncation, but looks odd that way round on the normal window title. – James_UK_DEV Apr 05 '16 at 09:40

3 Answers3

7

First, let me reinforce what Cody Gray said in both his answer and comment - this is non-standard behavior, and you should have a darn good reason for doing this.

That being said, I would take a nearly opposite approach to Cody's point #1. I would create a window WindowStyle set to None, and recreate the title bar (which could include the icon, your "pseudo-title," minimize, maximize, and close buttons, and perhaps even the standard Windows menu. You will also need to handle resizing (which can be done by setting ResizeMode to CanResizeWithGrip, but it adds a Grip control to the bottom of your window, which makes it look slightly different than a "normal" window).

The Title property of this window would then be the Title you want to show in the Taskbar, and the "pseudo-title" in the title bar you create would just be a Label or TextBlock bound to whatever you want your window to show.

It is a little complex, but really not too difficult to do. You will probably run into some Gotchas along the way (for instance, how the Window looks on different OS's or with different Windows themes applied). The nice thing is that it requires no Interop, and a majority of it can be attained using XAML only.

There are lots of examples online (here is one I picked at random).

Again, you'll have to decide if it is worth the effort to create a non-standard behavior. YMMV.

Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92
  • 1
    Thanks for your answer, it works nice and it's easy to do. I want to note that Microsoft makes WPF Shell Integartion to custom the windows chrome (http://code.msdn.microsoft.com/WPFShell). But most of the features are done for Windows 7 only. – Nicolas Jan 10 '11 at 10:08
4

Basically, you have two options:

  1. Draw the taskbar button yourself, rather than letting Windows handle it. This is actually reasonably simple, as far as owner drawing things goes.

  2. Manage two different forms/windows simultaneously. You'll need to create a hidden main window that will appear on the taskbar and own your second window. Your second window will be visible, display its own caption on its title bar, and contain your actual user interface, but won't show up on the taskbar (set its ShowInTaskbar property to "False"). You'll have to write code to show the second window whenever the first one is activated using the taskbar.

I recommend that before starting down either one of these paths, you carefully consider whether you really need this "functionality". It's difficult to tell what goes with what if you have what is effectively one window with different names in different places.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks for your answer. The second point seems pretty easy to do (I like the idea) but I don't want to have two windows. About the second point, do you know how to do it in wpf c#? I don't know how to override the IsIconic method and I'm afraid there is no window handle in wpf. – Nicolas Jan 07 '11 at 17:29
  • @Nicolas: I don't really have my wings as a WPF programmer, but the [`WindowInteropHelper` class](http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.aspx) allows you to interop with the unmanaged Windows API. Specifically, its [`Handle` property](http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.handle.aspx) will return an `hWnd` for a window. The risk with the first suggestion is that it may not work properly on newer versions of Windows (like 7) that have fundamentally altered the taskbar. I obviously haven't tested it. – Cody Gray - on strike Jan 07 '11 at 17:33
  • 2
    @Nicolas Option 2 is very not easy. @Cody is right that you need to show and hide the second window when action by the taskbar. You also need handle preview thumbails which I suspect will be a bridge too far. Delphi apps formerly used option 2 and thus allowed tasbar and main form captions to differ, but when Vista came out they stopped doing this. What's more, there was bug after bug relating to the main window and the fake window not synchronising properly. My advice is to roll with it and keep the captions the same. – David Heffernan Jan 07 '11 at 19:04
1

try to use this: http://www.codeguru.com/forum/showthread.php?t=3833 in conjunction with http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6b97a6de-0480-4339-8ed0-cb7cdb27bd83

The first one works fine for me in classical .NET form application when I have made window without title bar and want some text in task bar icon. The second one you need to handle low level WIN32 messages in WPF window (but this works only for top level one).

PAKO
  • 11
  • 1