How to Rotate Your Screen With C/C++?

Start

Today, I maked a video about rotate computer screen with C/C++, but I can not push my code in Bilibili, so I am pushing my code in here.
Note:You should including “windows.h” and “math.h”, defining a constant called PI and its value is “acos(-1.0)”.

First : Upper-Left

First, the screen rotates around the vertex in the upper-left corner.
Below is the code about rotating clockwise:

1
2
3
4
POINT pos[3];
pos[0].x = 0, pos[0].y = 0;
pos[1].x = cos(a) * w, pos[1].y = sin(a) * w;
pos[2].x = (-1) * (sin(a) * h), pos[2].y = cos(a) * h;

Below is the code for counterclockwise rotation:

1
2
3
4
POINT pos[3];
pos[0].x = 0, pos[0].y = 0;
pos[1].x = cos(a) * w, pos[1].y = (-1) * (sin(a) * w);
pos[2].x = sin(a) * h, pos[2].y = cos(a) * h;

Second : Upper-Right

Second,the screen rotates around the vertex in the upper-right corner.
Below is the code about rotating clockwise:

1
2
3
4
POINT pos[3];
pos[0].x = w - (cos(a) * w), pos[0].y = (-1) * (sin(a) * w);
pos[1].x = w, pos[1].y = 0;
pos[2].x = w - (cos(a) * w) + (sin(a) * h), pos[2].y = (cos(a) * h) - (sin(a) * w);

Below is the code for counterclockwise rotation:

1
2
3
4
POINT pos[3];
pos[0].x = w - (cos(a) * w), pos[0].y = sin(a) * w;
pos[1].x = w, pos[1].y = 0;
pos[2].x = w - (cos(a) * w) + (sin(a) * h), pos[2].y = (cos(a) * h) + (sin(a) * w);

Third : Bottom-Left

Third,the screen rotates around the vertex in the bottom-left corner.
Below is the code about rotating clockwise:

1
2
3
4
POINT pos[3];
pos[0].x = sin(a) * h, pos[0].y = h - (cos(a) * h);
pos[1].x = (sin(a) * h) + (cos(a) * w), pos[1].y = h - (cos(a) * h - sin(a) * w);
pos[2].x = 0, pos[2].y = h;

Below is the code for counterclockwise rotation:

1
2
3
4
POINT pos[3];
pos[0].x = (-1) * (sin(a) * h), pos[0].y = h - (cos(a) * h);
pos[1].x = (cos(a) * w) - (sin(a) * h), pos[1].y = h - (cos(a) * h - sin(a) * w);
pos[2].x = 0, pos[2].y = h;

Fourth : Bottom-Right

Fourth,the screen rotates around the vertex in the bottom-right corner.
Below is the code about rotating clockwise:

1
2
3
4
POINT pos[3];
pos[0].x = w - (cos(a) * w - sin(a) * h), pos[0].y = h - (cos(a) * h + sin(a) * w);
pos[1].x = w + (sin(a) * h), pos[1].y = h - (cos(a) * h);
pos[2].x = w - (cos(a) * w), pos[2].y = h - (sin(a) * w);

Below is the code for counterclockwise rotation:

1
2
3
4
POINT pos[3];
pos[0].x = w - (cos(a) * w + sin(a) * h), pos[0].y = h - (cos(a) * h - sin(a) * w);
pos[1].x = w - (sin(a) * h), pos[1].y = h - (cos(a) * h);
pos[2].x = w - (cos(a) * w), pos[2].y = h + (sin(a) * w);

Usage

So how to use this code in our project?
You should use the PlgBlt Function to draw the desired bitmap based on the coordinates:

1
2
3
4
5
6
7
HDC hdc = GetDC(0);
int w = GetSystemMetrics(0), h = GetSystemMetrics(1);
POINT pos[3];
//put you rotate coordinates code in here
PlgBlt(hdc, pos, hdc, 0, 0, w, h, 0, 0, 0);
ReleaseDC(0,hdc);
DeleteObject(hdc);

Ending

So that’s it for today, I will push my code in this link, bye!


How to Rotate Your Screen With C/C++?
http://hopejieshuo.github.io/2023/01/09/Screen-Rotate/
Author
hopejieshuo
Posted on
2023-01-09
Licensed under