Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclass WinUI Window to hook native win32 events #1687

Merged
merged 3 commits into from
Jul 20, 2021
Merged

Subclass WinUI Window to hook native win32 events #1687

merged 3 commits into from
Jul 20, 2021

Conversation

Redth
Copy link
Member

@Redth Redth commented Jul 15, 2021

To do some things in WinUI we need to use win32 API's still.

This adds support to subclass the Window and start listening for windows native messages, and exposes them as a lifecycle event for other things to use.

For instance you could now use this:

public void Configure(IAppHostBuilder appBuilder)
{
	appBuilder
		.UseMauiApp<App>()
		.ConfigureLifecycleEvents(lifecycle => {
#if WINDOWS
			lifecycle
				.AddWindows(windows => windows.OnNativeMessage((window, args) => {
					if (args.MessageId == WindowsNativeMessageIds.WM_DPICHANGED) {
						// The DPI has changed on the system
					}
				}));
#endif
	});
}

@Redth Redth requested a review from mattleibow July 15, 2021 16:33
@Redth Redth assigned jsuarezruiz and unassigned jsuarezruiz Jul 15, 2021
@Redth Redth requested a review from jsuarezruiz July 15, 2021 16:33
Comment on lines 3 to 9
public static class WindowsNativeMessageIds
{
public const int WM_DPICHANGED = 0x02E0;
public const int WM_DISPLAYCHANGE = 0x007E;
public const int WM_SETTINGCHANGE = 0x001A;
public const int WM_THEMECHANGE = 0x031A;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we want to do this? Just these? All? This casing? Pascal case?

Maybe for now we can keep this internal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we could just keep it internal for our own uses and maybe doc a url to a good way to find the id's.

I was torn between trying to provide some consts here for common events users may want, and exposing more individual events that propagate from these and get exposed in a more strongly named/typed way (like adding an OnThemeChanged to the lifecycle)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz jsuarezruiz mentioned this pull request Jul 16, 2021
2 tasks
@Redth Redth merged commit b80a399 into main Jul 20, 2021
@Redth Redth deleted the windows-win32 branch July 20, 2021 20:16
Comment on lines +66 to +68
var args = new WindowsNativeMessageEventArgs(hWnd, msg, wParam, lParam);

NativeMessage?.Invoke(this, args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth rewriting these lines to :

_NativeMessage?.Invoke(this, new WindowsNativeMessageEventArgs(hWnd, msg, wParam, lParam));

That way you don't allocate the event arguments unless there's a listener.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants