Why don't I see any text on screen?

CDev-8220 445 Reputation points
2025-08-15T23:23:35.6833333+00:00

I don't think I missed any code.

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);

HWND _hWnd = nullptr;
HWND _childhWnd = nullptr;
HINSTANCE _hInstance = nullptr;

int APIENTRY wWinMain(
    _In_ HINSTANCE hInstance, 
    _In_opt_ HINSTANCE hPrevInstance, 
    _In_ LPWSTR lpCmdLine, 
    _In_ int nCmdShow)
{
	_hInstance = hInstance;
	// The main window class name.
	const wchar_t szWindowClass[] = L"Win32DesktopApp";
	WNDCLASSEX windowClass = { };
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.lpfnWndProc = WindowProc;
	windowClass.hInstance = hInstance;
	windowClass.lpszClassName = (PCSTR)szWindowClass;
	windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	windowClass.hIconSm = LoadIcon(windowClass.hInstance, IDI_APPLICATION);
	if (RegisterClassEx(&windowClass) == NULL) {
		MessageBox(NULL, "Windows registration failed!", "Error", NULL);
		return 0;
	}
	_hWnd = CreateWindow(
		(PCSTR)szWindowClass,
		"Windows c++ Win32 Desktop App",
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		nullptr,
		nullptr,
		hInstance,
		nullptr
	);
	if (_hWnd == nullptr) {
		MessageBox(NULL, "Call to CreateWindow failed!", "Error", NULL);
		return 0;
	}
	// Begin XAML Island section.
	/* This initialization is necessary before calling WinRT APIs in non - UI
	applications, such as console applications, to avoid runtime errors. */
	winrt::init_apartment(winrt::apartment_type::single_threaded);
	if (winrt::/*Windows::Foundation::Metadata::*/ApiInformation::IsApiContractPresent(
		L"Windows.Foundation.UniversalApiContract", 8))
	{
		//auto manager = winrt::WindowsXamlManager::InitializeForCurrentThread();
		/* Initialize the XAML framework's core window for the current thread
		This class represents the UWP XAML framework that provides a single
		static InitializeForCurrentThread method that initializes the UWP XAML
		framework on the current thread in the desktop app. */
		winrt::WindowsXamlManager winxamlmanager =
			winrt::WindowsXamlManager::InitializeForCurrentThread();

		// Safe to use DesktopWindowXamlSource
		/* This DesktopWindowXamlSource is the object that enables a non-UWP
		desktop application to host WinRT XAML controls in any UI element that
		is associated with a window handle (HWND)
		This class represents an instance of UWP XAML content that you are
		hosting in your desktop app. The most important member of this class
		is the Content property. You assign this property to a
		Windows.UI.Xaml.UIElement that you want to host. This class also has
		other members for routing keyboard focus navigation into and out of
		the XAML Islands. */
		winrt::DesktopWindowXamlSource desktopSource;

		/* Get handle to the core window */
		auto interop =
			/* This COM interface provides the AttachToWindow method, which
			you use to attach a XAML Island in your app to a parent UI element.
			Every DesktopWindowXamlSource object implements this interface. */
			desktopSource.as<IDesktopWindowXamlSourceNative>();

		/* Parent the DesktopWindowXamlSource object to the current window */
		winrt::check_hresult(interop->AttachToWindow(_hWnd));

		/* Initialize a window handler for the XAML Island which is a child
		window that contains XAML */
		unique_ptr<HWND> hWndXamlIsland = make_unique<HWND>(nullptr);

		/* Get the new child window's HWND */
		interop->get_WindowHandle(&*hWndXamlIsland);

		/* Update the XAML Island window size because initially it is 0,0
		SetWindowPos changes the size, position, and Z order of a child,
		pop-up, or top-level window. These windows are ordered according
		to their appearance on the screen. The topmost window receives the
		highest rank and is the first window in the Z order. */
		SetWindowPos(*hWndXamlIsland, nullptr, 200, 100, 800, 200, SWP_SHOWWINDOW);

		// Create the XAML content.
		winrt::/*Windows::UI::Xaml::Controls::*/StackPanel xamlContainer;
		xamlContainer.Background(
			winrt::/*Windows::UI::Xaml::Media::*/SolidColorBrush{
				winrt::/*Windows::UI::*/Colors::LightGray()
			}
		);
		winrt::/*Windows::UI::Xaml::Controls::*/TextBlock tb;
		tb.Text(L"Hello World from Xaml Islands!");
		tb.VerticalAlignment(winrt::/*Windows::UI::Xaml::*/VerticalAlignment::Center);
		tb.HorizontalAlignment(winrt::/*Windows::UI::Xaml::*/HorizontalAlignment::Center);
		tb.FontSize(64);
		xamlContainer.Children().Append(tb);
		xamlContainer.UpdateLayout();
		desktopSource.Content(xamlContainer);
		// End XAML Island section.
	}
	else {
		MessageBox(
			nullptr,
			"This feature requires Windows 10 v1903 (18362) or later",
			"Unsupported OS",
			MB_ICONERROR | MB_OK
		);
	}
	
	ShowWindow(_hWnd, nCmdShow);
	UpdateWindow(_hWnd);

	// Message loop:
	MSG msg = { };
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT messageCode, WPARAM wParam, LPARAM lParam) {
	PAINTSTRUCT ps{};
	HDC hdc{};
	wchar_t greeting[] = L"Hello World in Win32!";
	RECT rcClient{};

	switch (messageCode) {
	/*case WM_PAINT:
		if (hWnd == _hWnd)
		{
			hdc = BeginPaint(hWnd, &ps);
			TextOut(hdc, 300, 5, (PCSTR)greeting, wcslen(greeting));
			EndPaint(hWnd, &ps);
		}
		break;*/
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
		// Create main window
	case WM_CREATE:
		_childhWnd = CreateWindowEx(
			0,
			"ChildWClass",
			nullptr,
			WS_CHILD | WS_BORDER,
			0, 0, 0, 0,
			hWnd,
			nullptr,
			_hInstance,
			nullptr
		);
		return 0;
		// Main window changed size
	case WM_SIZE:
		// Get the dimensions of the main window's client
		// area, and enumerate the child windows. Pass the
		// dimensions to the child windows during enumeration.
		GetClientRect(hWnd, &rcClient);
		MoveWindow(_childhWnd, 200, 200, 400, 500, TRUE);
		ShowWindow(_childhWnd, SW_SHOW);
		return 0;
		// Process other messages.
	default:
		return DefWindowProc(hWnd, messageCode, wParam, lParam);
		break;
	}

	return 0;
}

window

Why am I not seeing the text "Hello World from Xaml Islands!" on the screen?

The only thing I did not do is install Microsoft.Toolkit.Win32.UI.SDK. Do I need that?

Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Darran Rowe 2,346 Reputation points
    2025-08-16T13:10:22.5466667+00:00

    The most obvious reason why nothing is showing is that there is nothing to show.

    The DesktopWindowXamlSource is scoped to the if block, so it will be destroyed when the if block finishes execution. By the time the main window is shown, all traces of Xaml in the application are gone. At least make WindowXamlManager and DesktopWindowXamlSource global if you are intent on keeping this code layout.

    The DesktopWindowXamlSource instance's lifetime must last as long as the window that it is being displayed in. It is therefore much more beneficial to have it stored as part of the window data and use WM_CREATE/WM_DESTROY to do what they are meant to do here.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.