1

I have a requirement at work for a WPF stock-ticker-like application. I was thinking of using this as a starting point: http://www.jarloo.com/rumormill4

But the one thing it does not do is dock to the top of the desktop window - AND - push down any other windows, maximized or otherwise. This app must own a small bit of vertical real estate at the top of the screen, full width. I scoured WPF posts, but cannot find an example. I've seen a third party solution, so I know it's possible. Window.Topmost almost achieves this behavior, but just covers/obscures anything under it. Any suggestions? The image below demonstrates the current behavior. The WPF window sits on top of VS, which is problem.

Example of Visual studio obscured by toolbar

Michael K
  • 822
  • 11
  • 13
  • Could you present the problem with screenshot (what you want) as well as post your attempted code/xaml? The link doesn't tell much about issue and we can't debug "words". What is "top" or "stock-ticker-like"? Custom taskbar? – Sinatr Aug 31 '18 at 14:02
  • Yes, my mistake was the search criteria. Reserving an area on the screen was what I am after. I think this will work. Many thanks – Michael K Aug 31 '18 at 14:35
  • 1
    Google "wpf shappbarmessage" to find the code you need. Top hit is an [SO question](https://stackoverflow.com/questions/75785/how-do-you-do-appbar-docking-to-screen-edge-like-winamp-in-wpf) from 2008. – Hans Passant Aug 31 '18 at 15:00

1 Answers1

0

Set your XAML as the following:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        WindowStartupLocation="Manual"
        Title="MainWindow" SizeToContent="Height" ResizeMode="NoResize" Topmost="True"
        WindowState="Maximized">
    <Grid>
        <TextBlock Text="Hi there!" FontSize="36"/>
    </Grid>
</Window>

We set SizeToContent = Height to achieve the height auto effect. WindowState=Maximized to achieve full width for the window.

Then in the codebehind use the following code to avoid that the user moves the window around:

public partial class MainWindow
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xF010;

        public MainWindow()
        {
            InitializeComponent();

            SourceInitialized += OnSourceInitialized;
            Left = 0;
            Top = 0;
        }

        private void OnSourceInitialized(object sender, EventArgs e)
        {
            var helper = new WindowInteropHelper(this);
            var source = HwndSource.FromHwnd(helper.Handle);
            source.AddHook(WndProc);
        }

        private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {

            switch (msg)
            {
                case WM_SYSCOMMAND:
                    int command = wParam.ToInt32() & 0xfff0;
                    if (command == SC_MOVE)
                    {
                        handled = true;
                    }
                    break;
                default:
                    break;
            }
            return IntPtr.Zero;
        }
    }

I must say that I took the last part from this answer by @Thomas Levesque.

The result is a nice top docked window with full width

taquion
  • 2,667
  • 2
  • 18
  • 29
  • 1
    This definitely stops the user from moving the window around. That's good, but it does not reserve it's area. So, another application, can be obscured by this window. I need any other window(outside of the WPF app) to honor the area's position and not fill the area underneath it. – Michael K Aug 31 '18 at 14:48
  • Hi Michael K, I am trying to achieve something similar and was wondering if you found a solution to it. If yes, it would be much appreciated if you could share it with the community. Thanks in advance:) – Krishna Adhikari Jan 23 '20 at 00:03