How to build high DPI aware native Windows desktop applications

If you’re developing native applications for Windows using Win32 or MFC and you want to support high DPIs so that the application looks crisp on any display, you have to do a lot of things by hand. That is because the technologies for building native UIs, that is MFC, GDI, GDI+, do not provide DPI scaling support. In this article, I will walk through some of the problems of supporting DPI scaling and the solutions for them.

Initializing statement for if/switch/foreach

There are several statements in C++ whose syntax was modified in recent versions of the standard. I refer here to the if and switch statements that were modified in C++17 to include initializing statements, and the range-based for loop that supports the same as of C++20. Their general form is shown in the following table:

Express one of multiple options in a nice way

We often find ourselves writing if statements where a variable is compared with several values either to check if it matches one of them or that it doesn’t match any. Here is an example:

int option = ...;

// at lease a value is matched
if (option == 12 || option == 23 || option == 42)
{
   std::cout << "it's a good option\n";
}

// no value is matched
if (option != 12 && option != 23 && option != 42)
{
   std::cout << "it's a bad option\n";
}

This example has three comparison values for each case, but it could be 5, or 10, or any number. If it’s too many then perhaps a different approach should be taken. However, the question is, how do we express this in a simpler way in C++, rather than a long if condition?

The choice between typename and class

When working with C++ templates, you have probably seen typename and class used interchangeably. Is there a difference between them? This post will explain when these two keywords can be used in templates. Let’s consider the following example: In this context, when declaring a type template parameter, there is no difference, they are interchangeable. They…

Using Microsoft Edge in a native Windows desktop app – part 4

In the previous articles, we learned how to perform navigation in a Windows desktop application and how navigation events work. However, until recently, it was not possible to perform POST or GET request using custom headers or content. This feature was added in version 705.50. In this fourth article of the series, we will look in detail at how to perform POST requests with custom headers and content.

Modernizing legacy code

In the past decade and a half I’ve been working with large legacy code bases started in early ’90s. Therefore, I had to deal with lots of code using old styles and conventions such raw pointers, void pointers, declaring all variables before using them, public data members accessed from everywhere, and many others. I believe in change and therefore I’m trying to make as many changes as possible. Of course, this is not always possible, or desirable (due to various constraints). Moreover, nobody will stop a large project for months or years to modernize the code. However, applying small but incremental changes is always possible, and over time, large code-bases can improve. This is a strategy I’m constantly applying to parts of code that I have to modify. In this blog post I will be listing a series of improvements you can do with old C++ code in order to modernize and improve it.

The little functions that matter

Starting with C++20, some very useful functions for searching have been added to some standard containers, such as std::map, std::set, and std::string. These have been required for a long time and it’s good to see that the committee finally agreed upon their value. I hope this is the beginning of some wonderful additions.

Why I like C++ attributes

Attributes are an underrated feature of the C++ language, in my opinion. I am saying this because I rarely see attributes used in code or samples featured in articles, videos, or talks. Although some of the standard attributes are targeted towards library implementers or address a limited number of scenarios (such as [[no_unique_address]], [[noreturn]], or [[carries_dependency]]), there are several that are quite useful in many situations. I refer here to [[nodiscard]], [[maybe_unused]], and [[deprecated]], which are the attributes I will talk about in this post.

C++20 books

The C++20 standard is complete and is supposed to be published later this year after the voting of the final draft takes place. However, there are books already with C++20 content. In this blog post I present a list of them. The C++ Standard Library, 3rd edition – Rainer Grimm Rainer is an author, consultant,…