Causes
Recently encountered a project, need to display some information on the left side of the computer, so found some information on the Internet, successfully implemented on the desktop to draw information.
Code
#include"stdafx.h"#include<iostream>#include<windows.h>#include<io.h>#include<map>#include<fstream>#include<atlimage.h>using namespacestd;voidDraw () {//get a DC for drawing, I'm going to use the desktop right here.HDC HDC =GETWINDOWDC (GetDesktopWindow ()); //Create a solid line brush with a red 1 pixel widthHpen hpen1 = CreatePen (Ps_solid,1, RGB (255,0,0)); //create a broken brush with a green 5-pixel width, if you want to create other kinds of brushes see MSDNHpen hpen2 = CreatePen (Ps_dash,5, RGB (0,255,0)); //Create a solid blue paint brushHbrush hbrush1 = CreateSolidBrush (RGB (0,0,255)); //create a transparent paint brush, if you want to create other kinds of brushes see MSDNHbrush HBRUSH2 =(Hbrush) getstockobject (Null_brush); //Select Hpen1 and HBRUSH1 into HDC and save the original HDC brush and BrushHpen Hpen_old =(Hpen) SelectObject (hdc, HPEN1); Hbrush Hbrush_old=(Hbrush) SelectObject (hdc, HBRUSH1); //draw a rectangle with a width of 200 pixels and a height of 50 pixels at (40,30)Rectangle (HDC, +, -, ++ $, -+ -); //change Hpen1 and Hbrush1, and then draw a rectangle at (40,100) to see what the difference is .SelectObject (hdc, HPEN2); SelectObject (hdc, HBRUSH2); Rectangle (HDC, +, -, ++ $, -+ -); //draw an ellipse and seeEllipse (HDC, +, $, ++ $, $+ -); //Draw a line (0,600) to (800,0) to seeMovetoex (HDC,0, -, NULL); LineTo (HDC, -,0); //at (700,500) draw a yellow dot, but this point is only one pixel size, you look carefully to findSetPixel (HDC, the, -, RGB (255,255,0)); //text,//parameters: Desktop handle, xy coordinate, text, text widthTextouta (HDC, the, -,"ha ha haha",6); //restore the original brush and paint brushSelectObject (hdc, hpen_old); SelectObject (hdc, hbrush_old);}intMain () { while(true) {draw (); Sleep ( -); } System ("prase");}
C + + draws text graphics on window desktop