Realize tablecloth automatic update in C ++ Builder

zhaozj2021-02-16  67

In C Builder to implement the automatic replacement function of the tablecloth, I think two key points, one of the one you want to "automatically", you can save the timer, we can use the Timer component to complete, this is also resolved, The second question may be more troublesome. How do we set a tablecloth, it seems that there is no component in the BCB, then we have to be self-reliant, although I am just a rookie, but this problem is still can't be, it is mainly The Windows API function is used in the SystemParametersInfo, which is as follows: Bool SystemParametersInfo (

Uint uiaction, // system parameter to query or set

UINT UIPARAM, // Depends on action to be taken

Pvoid ​​Pvparam, // Depends on action to be taken

Uint Fwinini // User Profile Update Flag

);

It is a very heavyweight function, mainly used to set or get the relevant parameters on the system (such as "how to set the tablecloth", as well as: system font, screen resolution, etc.), actually light from the literal It can also know its role. If you just call the SystemParametersInfo function, the problem is much simple, but you have to always use the default tablecloth style, so you should set the style of the tablecloth before setting the tablecloth, such as: Tablecloth display location, Whether to fill a desktop, then call the SystemParametersInfo function, this function sets the actual tablecloth pattern according to the tablecloth style setting in the registry.

Registry setting related to the tablecloth pattern: (located at the HKEY_CURRENT_USER / Control Panel / Desktop button)

The key name meaning the file name of the WallPaper tablecloth, but only bitmap WallPaperStyle sets the tablecloth in the BMP format, if it is 0, the tablecloth will appear in the center of the desktop in the center of the desktop, the tablecloth will fill the desktop if 2, put the tablecloth The picture size, the image is likely to distortion WallPapeRiginx if the wallpaperstyle is 0, this means that the image left upper corner X-axis WallPapeRiginy is 0, this means that the image left upper corner Y axis coordinate

Note: In fact, the above table also misses the TileWallPaper key, this set value is the highest, that is, when the value of TileWallpaper is 1, the tablecloth will be filled with the desktop, in order to simplify the program, generally Set to 0, then set the tablecloth pattern according to WallPaperStyle, which is a wise practice.

After understanding the settings of the registry associated with the tableclub, then let's enter the specific tablecloth setting!

#include

// Add a tablecloth style in the registry, set the tablecloth Tregistry * registry = new Tregistry; try {registry-> Openkey ("// Control Panel ////////////////////////////////////" , "0"); // set wallpaper pattern Registry-> WriteString ( "WallpaperStyle", wallStyle);} __finally {delete Registry;}} // set wallpaper SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, FileName.c_str (), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE Here, we have solved the most critical issues, and the rest of the setting timer is simple, just use a timer component. The code is as follows:

Void __fastcall tform1 :: timer1timer (TOBJECT * Sender) {if (listbox1-> items-> count <= 0) {RETURN;}

IF (index> = listbox1-> items-> count) {index = 0;} // If it is the JPG format, it converts it to the BMP format, saves it to the temporary folder (UpperCase (listbox1-> items- > Strings [index]). POS (". Jpg") || uppercase (ListBox1-> items-> strings [index]). POS (". Jpeg")) {ANSISTRING FILENAME = ExtractFileName (listbox1-> items-> Strings [index]); int LEN = filename.lastdelimiter ("."); Filename = filename.substring (0, len-1) ". Bmp"; unsigned int * size = new unsigned int (256); char * buffer = new char [256]; GetTempPath (* size, buffer); fileName = AnsiString (buffer) fileName; JPEGToBMP (ListBox1-> Items-> Strings [index], fileName); // set wallpaper SystemParametersInfo (SPI_SETDESKWALLPAPER, 0 , ListBox1-> Items-> Strings [index] .c_str (), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);} else {// set wallpaper SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, ListBox1-> Items-> Strings [index] .c_str (), SPIF_UPDATEINIFILE | Spif_sendwininichange);} // Rebuild system cache Rebuildi Concache (); index ;} In this way, we have completed the function of the tablecloth automatic replacement, but because it is limited to the BMP bitmap format, this always makes the tower of the tablecloth very distressed. We can Can't let it support other format graphics files? Of course, the principle is such that before setting the tablecloth, the files in other formats are now converted to a bitmap, and set the tablecloth with the SystemParametersInfo function. JPEG format conversion to bit Figure: #include

bool __fastcall TForm1 :: JPEGToBMP (AnsiString jpegFile, AnsiString bmpFile) {// TODO: Add your source code here TJPEGImage * jpeg = new TJPEGImage (); jpeg-> LoadFromFile (jpegFile); Graphics :: TBitmap * bitmap = new Graphics: : Tbitmap (); try {bitmap-> assign (jpeg); Bitmap-> Savetofile (bmpfile);} __finally {delete Jpeg; delete bitmap;} Return True;} Regret is that BCB does not directly support formats such as PNG, GIF Conversion to the bitmap, I hope to get support in the version after BCB6.0. If you want to perform a PNG, GIF format conversion, we can seek controls, this is the easiest way, there is a way you write a graphic format The transition program, this is more difficult. To first we must know the standards of various graphics formats, then write the algorithm for format conversion, this is the most important, finally coded. There is a little more, if you are enough, It may have been discovered that I used a RebuildIncache () function when I realize Timer1Timer (TOBJECT * Sender), and the comment is to rebuild the system cache. In fact, when you change the tablecloth, it may not be able to re-draw a tablecloth when you change the tablecloth. At this point, you need to manually press F5 to refresh, but our program can not call users to refresh, so I add a rebuildoncache () function to complete this feature, it is achieved. void __fastcall TForm1 :: RebuildIconCache () {// TODO: Add your source code here int IconW; IconW = GetSystemMetrics (SM_CXICON); TRegIniFile * Reg = new TRegIniFile ( "Control Panel // Desktop"); try {Reg-> WriteString ("WindowMetrics", "shell icon size", INTSTR (ICONW)); sendMessage (hwnd_broadcast, wm_settingchange, 0, 0);} __finally {delete reg;}} This as long as you change the tablecloth setting, broadcast the WM_SETTINGCHANGE window message, The system will resemble the tablecloth. Tip: If you want you to know the function of this program, you don't have to achieve your goals through the keyboard, http://www.zccfamily.com/zqget/ provides all the source code for this program, you can download it. The actual effect.

During reading this article, whether it is grateful (I have solved the problem for you) or your heart (this is your original, or to copy other places), or what evaluate this article, I Contact:

Mailto: zqget@msn.com

转载请注明原文地址:https://www.9cbs.com/read-22004.html

New Post(0)