President Trump was already revved up when he emerged from his limousine to visit NATO’s new headquarters in Brussels last May. He had just met France’s recently elected president, Emmanuel Macron, whom he greeted with a white-knuckle handshake and a complaint that Europeans do not pay their fair share of the alliance’s costs.

On the long walk through the NATO building’s cathedral-like atrium, the president’s anger grew. He looked at the polished floors and shimmering glass walls with a property developer’s eye. (“It’s all glass,” he said later. “One bomb could take it out.”) By the time he reached an outdoor plaza where he was to speak to the other NATO leaders, Mr. Trump was fuming, according to two aides who were with him that day.

He was there to dedicate the building, but instead he took a shot at it.

“I never asked once what the new NATO headquarters cost,” Mr. Trump told the leaders, his voice thick with sarcasm. “I refuse to do that. But it is beautiful.” His visceral reaction to the $1.2 billion building, more than anything else, colored his first encounter with the alliance, aides said.