1

My App in WPF is a kind of system monitor, so I want to have it always visible, even in each Virtual Desktop of Windows 10. Is it possible using only C#?

Brummell
  • 211
  • 2
  • 17
  • What have you tried so far? Could you have it appear in the system tray so you could access it all the time? – Wai Ha Lee Nov 07 '17 at 20:06
  • No, I don't like using the TryIcon and I want it always visible on the desktop (on the top of desktop); like a system clock. – Brummell Nov 07 '17 at 20:10

2 Answers2

4

Looks like all you have to do is set WS_EX_TOOLWINDOW on your main window. Per https://superuser.com/questions/950960/pin-applications-to-multiple-desktops-in-windows-10

Example code:

[DllImport( "user32.dll", EntryPoint = "GetWindowLongPtr" )]
public static extern IntPtr GetWindowLongPtr( IntPtr hWnd, GWL nIndex );

[DllImport( "user32.dll", EntryPoint = "SetWindowLongPtr" )]
public static extern IntPtr SetWindowLongPtr( IntPtr hWnd, GWL nIndex, IntPtr dwNewLong );

const long WS_EX_TOPMOST = 0x00000008L;

public enum GWL : int
{
    GWL_WNDPROC = (-4),
    GWL_HINSTANCE = (-6),
    GWL_HWNDPARENT = (-8),
    GWL_STYLE = (-16),
    GWL_EXSTYLE = (-20),
    GWL_USERDATA = (-21),
    GWL_ID = (-12)
}

public static void SetToolWindow( Window window )
{
    var wih = new WindowInteropHelper( window );
    var style = GetWindowLongPtr( wih.Handle, GWL.GWL_EXSTYLE );
    style = new IntPtr( style.ToInt64() | WS_EX_TOPMOST );
    SetWindowLongPtr( wih.Handle, GWL.GWL_EXSTYLE, style );
}
Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35
  • It is for "C" language (low level), not "C#". – Brummell Nov 07 '17 at 22:08
  • 1
    Ah, you're building for 32 bit. There are different functions under 32 as opposed at 64 bit. Check out https://stackoverflow.com/questions/3343724/how-do-i-pinvoke-to-getwindowlongptr-and-setwindowlongptr-on-32-bit-platforms – Joel Lucsy Nov 08 '17 at 22:02
  • I've tryed now with 64b, but does not work. MainWindow_2 myWindow = new MainWindow_2(); SetToolWindow(myWindow); myWindow.Show(); – Brummell Nov 08 '17 at 22:59
  • Excuse me, I explain better. Changing CPU target, do not launch excep., but nothing another effect. – Brummell Nov 08 '17 at 23:15
1

I found wrapper .Net 4.6 compatible: VirtualDesktop

Nice solution! TY all ;-)

Brummell
  • 211
  • 2
  • 17