6

So in UWP, we can use

ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(500, 500));

but

ApplicationView.GetForCurrentView();

returns null in WinUI 3. Is there a way to set the minimum size for a window

3 Answers 3

3

The recommended solution, implemented on GitHub, is to use P/Invoke to call the window subclassing functions SetWindowSubclass and DefSubclassProc:

  1. Write a static WndProc method that handles WM_GETMINMAXINFO, then forwards any other window messages to the original WndProc, via DefSubclassProc (or CallWindowProc in the examples), although note the disadvantages).
  2. Register your new WndProc via SetWindowSubclass (or SetWindowLongPtr in the examples).

This comes from an issue on the XAML GitHub repo.

See this similar question and this other similar question for more info.

Sign up to request clarification or add additional context in comments.

Comments

3

If anyone is wanting a modern solution, please add the following to your MainWindow():

    OverlappedPresenter presenter = OverlappedPresenter.Create();
    presenter.PreferredMinimumWidth = 300;
    presenter.PreferredMinimumHeight = 400;
    this.AppWindow.SetPresenter(presenter);

1 Comment

Good solution. This is what I was looking for.....no pointers and kernel calls.
0

This way you don't need to recreate the OverlappedPresenter:

public MainWindow()
{
    InitializeComponent();

    if (this.AppWindow.Presenter is OverlappedPresenter presenter)
    {
        presenter.PreferredMinimumWidth = 300;
        presenter.PreferredMinimumHeight = 400;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.