You can use either of the two methods to write text directly to the desktop.

Source: Internet
Author: User
Tags textout

If you open an application to directly display a rose and some touching text on your desktop, will it be cool? Here are two ways to directly write the text to your desktop, here we will only discuss how to display it to the desktop, and do not study how to change the font, color, and other functions. If you are interested, you can add them on your own.

Method 1:

This method is especially simple. You only need to use getdesktopwindow () to obtain the desktop handle.

First, create a dialog box program and add the following two lines of code in: oninitdialog:

Showwindow (sw_hide); // hide the application
Settimer (1,1000, null); // create a timer.

Then add showwindow (sw_hide) again in: onpaint );

Then add the on_timer message. Add the following code. (I love you on the computer screen)

Int X, Y;
X = rand () % getsystemmetrics (sm_cxscreen );

Y = rand () % getsystemmetrics (sm_cyscreen );

Cwindowdc CDC = getdomaintopwindow ();
CDC. setbkmode (transparent );
Cstring STR = "I Love You ";
Textout (CDC, X, Y, STR, str. getlength ());

 

OK, the code above has implemented the hiding of the window, and you can write I love you right away on your screen. Well, there is a small problem, that is, the window will suddenly disappear after a flash, so how can we avoid the phenomenon of window flickering? We can use the following method

// Add and process wm_windowposchanging messages

Void cxxdlg: onwindowposchanging (windowpos far * lpwndpos)

{

Lpwndpos-> flags & =
~ Swp_showwindow; // remove the swp_showwindow flag.

Cdialog: onwindowposchanging (lpwndpos); // todo: add your message handler code here

}

Of course, we need to manually add this message. We need to add two macros and function definitions. I will not elaborate on the details here. I should understand the basics of VC. Okay. The first method is that simple.

Method 2:

The principle is to create a window as large as the screen, but the window is transparent. You only need to write the words you want to write in a transparent window, the ws_ex_topmost | ws_ex_layered attribute of the extended window is used here. The former is that the window always belongs to the top layer, and the latter makes the window transparent. The Code is as follows. Added the following content based on the first lesson of Sun Xin's Tutorial:

# Include <windows. h> # include <stdio. h> # define ws_ex_layered 0x80000typedef bool (winapi * FPT) (hwnd, Int, bool, DWORD); fpt fp; lresult callback winsunproc (hwnd, // handle to window uint umsg, // message identifier wparam, // first message parameter lparam // second message parameter); int winapi winmain (hinstance, // handle to current instance hinstance hprevinstance, // ha Ndle to previous instance lpstr lpcmdline, // command line int ncmdshow // show state) {wndclass wndcls; wndcls. cbclsextra = 0; wndcls. cbwndextra = 0; wndcls. hbrbackground = (hbrush) getstockobject (white_brush); wndcls. hcursor = loadcursor (null, idc_cross); wndcls. hicon = loadicon (null, idi_error); wndcls. hinstance = hinstance; wndcls. lpfnwndproc = winsunproc; wndcls. lpszclassname = "weixin2003"; wndcls. lpszmenuna Me = NULL; wndcls. style = strong | strong; registerclass (& wndcls); hwnd = create1_wex (strong | ws_ex_toolwindow, "weixin2003", null, ws_popup | ws_sysmenu, 0, 0, getsystemmetrics (sm_cxscreen), getsystemmetrics (sm_cyscreen), null, null, hinstance, null); showwindow (hwnd, sw_shownormal); updatewindow (hwnd); MSG; while (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); Di Spatchmessage (& MSG);} return 0;} lresult callback winsunproc (hwnd, // handle to window uint umsg, // message identifier wparam, // first message parameter lparam // second message parameter) {hmodule hsad = loadlibrary ("user32.dll"); HDC; tchar STR [] = "I Love You "; switch (umsg) {Case wm_create: fp = (FPT) getprocaddress (hsad, "setlayeredwindowattributes"); freelibrary (hsad); FP (Hwnd, RGB (255,255,255),); settimer (hwnd, null); break; Case wm_timer: HDC = getdc (hwnd); textout (HDC, Rand () % getsystemmetrics (sm_cxscreen), Rand () % getsystemmetrics (sm_cyscreen), STR, sizeof (STR)-1); releasedc (hwnd, HDC); break; Case wm_char: tchar szchar [20]; sprintf (szchar, "char is % d", wparam); MessageBox (hwnd, szchar, "Weixin", 0); break; Case wm_lbuttondown: HDC; HDC = getdc (hwnd); Tex Tout (HDC, 0, 50, "computer programming language training", strlen ("computer programming language training"); releasedc (hwnd, HDC); break; Case wm_paint: HDC; paintstruct pS; HDC = beginpaint (hwnd, & PS); textout (HDC, "restoration training", strlen ("restoration training"); endpaint (hwnd, & PS); break; Case wm_close: If (idyes = MessageBox (hwnd, "is it true? "," Weixin ", mb_yesno) {destroywindow (hwnd);} break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, umsg, wparam, lparam);} return 0 ;}

The transparent effect depends on setlayeredwindowattributes.() Function, function usage can refer to msdn or refer to the http://baike.baidu.com/view/1329156.htm

OK. Both methods are very simple. You can give full play to your abilities and add other code to the desktop to write anything you want to write, and load various nice pictures.

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.