Keep WinUI 3 (Windows App SDK) window always at bottom - c#

I have the following window using Windows App SDK 1.1.5 and WinUI 3, I want to keep it always at the bottom, just above the desktop, m_AppWindow.MoveInZOrderAtBottom(); moves it to the very bottom as I intended, but once it is pressed it comes back to the foreground.
How can I prevent it from coming to the foreground once clicked? I suppose it may be related to the HWND handle.
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using WinRT.Interop;
namespace Widgets {
public sealed partial class MainWindow : Window {
private AppWindow m_AppWindow;
public MainWindow() {
this.InitializeComponent();
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.MoveInZOrderAtBottom();
}
private AppWindow GetAppWindowForCurrentWindow() {
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
}
}
This is just a slightly modified MainWindow.cs from the visual studio Blank App, Packaged (WinUI 3 in Desktop) C# template.
Thanks in advance.

You can try it this way.
public sealed partial class MainWindow : Window
{
private AppWindow m_AppWindow;
public MainWindow()
{
this.InitializeComponent();
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.Changed += M_AppWindow_Changed;
}
private void M_AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args)
{
if (args.DidZOrderChange is true)
{
m_AppWindow.MoveInZOrderAtBottom();
}
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
}

Related

Problem with modeless Form Window, which implements IWin32Window

I tried to follow the logic, described on Jeremy's post here:
https://jeremytammik.github.io/tbc/a/0088_revit_window_handle.htm
First - what is a goal.
The goal is to create a Win Form window, which would start with Revit Application and close with Revit Application. Window is in modeless dialog and displays the output of different actions/commands which are happening while working in Revit Document. Everything works fine for me, except for the part, that with each Revit Command new instance of Form Window is created. I would like to keep only one window which hides and shows, whenever any action is taken.
Below where I came so far - I hope anyone could be able to direct me which path to go....
Win Form Class:
public partial class CmdWindowHandleForm : Form
{
public string LabelText
{
get{ return _labelText.Text; }
set{ _labelText.Text = value; }
}
public CmdWindowHandleForm()
{
InitializeComponent();
}
}
partial class CmdWindowHandleForm
{
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this._labelText = new Label();
this._labelText.Text = string.Empty;
this.Controls.Add(this._labelText);
}
}
Class with WindowHandle, which is used as argument when Form.Show() method is called:
public class WindowHandle : IWin32Window
{
public IntPtr Handle
{
get{ return _handleWindow; }
}
IntPtr _handleWindow;
public WindowHandle(IntPtr handleWindow)
{
_handleWindow = handleWindow;
}
}
And lastly my static class of InfoConsole window, which would run along Revit app:
public static class InfoConsole
{
public static CmdWindowHandleForm Window
{
get{ return _window; }
}
static WindowHandle _hWndRevit = null;
static CmdWindowHandleForm _window = null;
public static void Show(string message)
{
if (_hWndRevit == null)
{
Process process = Process.GetCurrentProcess();
IntPtr h = process.MainWindowHandle;
_hWndRevit = new WindowHandle(h);
}
if(InfoConsole._window == null)
{
_window = new CmdWindowHandleForm();
_window.Show(_hWndRevit as IWin32Window);
}
else
{
_window.Visible = true;
}
_window.AddText(message);
}
}
And while running a command (implementing IExternalCommand - interface required to access Revit DB) method is simply called:
InfoConsole.Show(outputMessage);
Any help much appreciated...
Lukasz
The code you refer to is way out of date. It was published by The Building Coder in February 2009: Revit Window Handle and Modeless Dialogues.
Access to the Revit main window handle changed in 2018: Revit Window Handle and Parenting an Add-In Form.
This question was also raised and discussed in the Revit API discussion forum: Problem with modeless Form Window, which implements IWin32Window. Please refer to that for further, continued discussion and the final solution, still under development as I write this.

How To Set Default Window Mode to Full Screen in WinUI3?

namespace MyApp
{
public sealed partial class MainWindow : Window
{
AppWindow m_appWindow;
public MainWindow()
{
this.InitializeComponent();
m_appWindow = GetAppWindowForCurrentWindow();
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(myWndId);
}
private void SwitchPresenter_FullScreen(object sender, RoutedEventArgs e)
{
m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
}
}
}
SwitchPresenter_FullScreen function is works but how can i set my app's default window mode to full screen? Can i call SwitchPresenter_FullScreen while app starting?
No. Don't call SwitchPresenter_FullScreen directly. This method is for your UI controls (like CheckBox or ToggleButton).
Just add this line to your MainWindow constructor.
public MainWindow()
{
this.InitializeComponent();
m_appWindow = GetAppWindowForCurrentWindow();
m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen); // This line
}

Textbox readonly property not working in TextBox made by me

I have a textbox (TextBoxMoeda) made by me, but, even when the textbox property readonly is set true. nothing happens. Anyone knows how to implement the code to resolve this problem?. Please help me?
public class TextBoxMoeda : TextBox
{
private double dp;
private string fmt = string.Empty;
private int _CasasDecimais = 0;
[Category("TextBoxMoeda")]
public virtual bool SomenteLeitura
{
get => base.ReadOnly;
set
{
base.ReadOnly = value;
Invalidate();
}
}
//Code Continues .......
}
WPF
You want the IsReadOnly property, not the ReadOnly property.
For example:
Create a new WPF App (.NET Framework) project called DeleteMe
In MainWindow.xaml, delete the Grid
In MainWindow.xaml.cs, use this code:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace DeleteMe
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var tm = new TextBoxMoeda();
this.AddChild(tm);
tm.SomenteLeitura = true;
}
public class TextBoxMoeda : TextBox
{
[Category("TextBoxMoeda")]
public virtual bool SomenteLeitura
{
get => base.IsReadOnly;
set
{
base.IsReadOnly = value;
}
}
}
}
}
The textbox will be readonly (i.e. I can't type anything into the textbox).
WinForms
The code already appears to work in WinForms. These steps will produce a working project:
Create a new Windows Forms App project called DeleteMeAgain
Overwrite all of the Form1.cs code with this:
using System.ComponentModel;
using System.Windows.Forms;
namespace DeleteMeAgain
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var tm = new TextBoxMoeda();
this.Controls.Add(tm);
tm.SomenteLeitura = true;
}
public class TextBoxMoeda : TextBox
{
[Category("TextBoxMoeda")]
public virtual bool SomenteLeitura
{
get => base.ReadOnly;
set
{
base.ReadOnly = value;
Invalidate();
}
}
}
}
}
The textbox will be readonly (i.e. I can't type anything into the textbox).
Windows Form: User Control
Do this:
Create a new Windows Forms App called DeleteMe3
Set Target Framework to .NET Core 3.1 (Long-term support)
In your project, create a new UserControl with the filename TextBoxStack.cs
Overwrite all of the code for that user control with this:
using System.ComponentModel;
using System.Windows.Forms;
namespace DeleteMe3
{
public partial class TextBoxStack : TextBox
{
[Category("TextBoxStack")]
public virtual bool SomenteLeitura
{
get => base.ReadOnly;
set
{
base.ReadOnly = value;
Invalidate();
}
}
}
}
An error will appear. Go to the source of the error and delete that line.
Build the solution
Add your new user control to Form1
Select the new user control on the form
Change the SomenteLeitura property to True
Run the project. The textbox should be grey and readonly.

How to remove huge title bar from Xamarin.Forms WPF application?

I've got a Xamarin.Forms WPF application running, but there is a huge title bar.
I have tried the solution of adding NavigationPage.SetHasNavigationBar(this, false); inside my MainPage class's constructor
Here is the code:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
}
Still I am getting huge title bar size in WPF application, on the other hand, while running on UWP, it is working fine.
You could try to add below codes in your MainWindow.xaml.cs in wpf project:
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (!topBarsRemoved) RemoveTopBars();
}
private bool topBarsRemoved = false;
private void RemoveTopBars()
{
var topAppBar = this.Template.FindName("PART_TopAppBar", this) as FormsAppBar;
if (topAppBar != null)
(topAppBar.Parent as System.Windows.Controls.Grid)?.Children.Remove(topAppBar);
topBarsRemoved = true;
}

Open existing window in Gtk#

I have added a new window to the project and the name of the window is Actwindow. How can I open this window?
I tried using Actwindow=new Window(); but it does not work.
The window code is:
using System;
namespace myproject
{
public partial class Actwindow : Gtk.Window
{
public Actwindow () :
base (Gtk.WindowType.Toplevel)
{
this.Build ();
}
}
}
What am I doing wrong?
Actwindow myWindow = new Actwindow();
myWindow.Show(); // or similar

Categories