First, drag no title form:
Contains header files:
#include $ # @ 60; winuser.h $ # @ 62;
Add the following code to the onMouseDown event of the form or component:
IF (Button == Mbleft)
{
Releasecapture ();
SendMessage (handle, wm_nclbuttondown, htcaption, 0);
}
Second, pop up and close the optical drive:
Contains header files:
#include $ # @ 60; mmsystem.h $ # @ 62;
Add: in the oncreate event of the form:
McIndstring ("Open CDAUDIO ALIAS CD WAIT Shareable", 0,0,0);
1. Use when you pop up the optical drive:
McIndstring ("Set CD Door Open", 0, 0);
2. Use: when you want to turn off the drive:
MCISENDSTRING ("SET CD Door Closed", 0, 0);
Third, extraction icon:
Contains header files:
#include $ # @ 60; shellapi.h $ # @ 62;
example:
Ticon * icon = new ticon ();
Ansistring filename = "c: //windows//system/shell32.dll";
Int Totalicon;
// Get the total number of icons of the file shell32.dll
Totalicon = (int) Extracticon (Form1-> Handle, filename.c_str (), -1);
/ / Extract the first icon, 0 is the first, 1 is the second, the class push ...
Icon-> Handle = Extracticon (Form1-> Handle, FileName.c_STR (), 0);
/ / Save icon
Icon-> Savetofile ("C: //1.ico");
Fourth, set the top window (forever on the top):
Contains header files:
#include $ # @ 60; winuser.h $ # @ 62;
1, set the top window
Setwindowpos (Handle, Hwnd_topmost, 0, 0, 0, 0, SWP_NOMOVE || SWP_NOSIZE);
2, cancel the top window
Setwindowpos (Handle, Hwnd_Notopmost, 0, 0, 0, 0, SWP_NOMOVE || SWP_NOSize);
API skills (2)
12/3/2000 0: 0: 0 · · East Liang ·· Yesky
(1) irregular window
Windows is only a standard rectangular window. To create an irregular window, you need to call the API function to be invoked. Building an irregular window, typically establish an irregular area with the API function of the creating area, and then use the API function setWindowRGN to change the area of the window. These API functions are included in the C Builder in the header file WINGDI.H and WinUser.h, so use these API functions, you must first add statements that contain the header file in the program head:
Include $ # @ 60; wingdi.h $ # @ 62;
Include $ # @ 60; winuser.h $ # @ 62;
The setWindowRGN function can change the area of a window, which has three parameters. The first parameter hWnd is the window handle to set the area, the second parameter hrgn is the area that wants to set, the third parameter BredRaw is generally set to True, ie Heavy view of the window immediately. There are several API functions used to create a region, and there are three most commonly used:
1, the CreateRectrGN function, is used to create a rectangular area determined by X1, Y1, and X2, Y2 coordinate points. When the coordinate points X1 and Y1 are equal, X2 and Y2 are also equal, and a square is created.
example:
// Create a rectangular
HRGN HRECT = CreateRectrGN (0,0,400,200);
SetwindowRgn (Handle, HRECT, TRUE);
// Create a square
HRGN HRECT = CreateRectrGN (0, 0, 300, 300);
SetwindowRgn (Handle, HRECT, TRUE);
2, the CreateellLipTicRGN function, is used to create an ellipse of the rectangle determined by X1, Y1 and X2, Y2 coordinate points. Similarly, X1, Y1, and X2, the rectangle determined by the Y2 coordinate point is a square, the created is a circular shape.
example:
// Create an ellipse
HRGN Helliptic = CreateellLipTicRGN (0, 0, 400, 250);
SetWindowRgn (Handle, Helliptic, true);
// Create a circle
HRGN Helliptic = CreateellLipticRGN (0,0,400,400);
SetWindowRgn (Handle, Helliptic, true);
3, the Combinergn function, can combine two zones into a new area, there are four parameters, the first parameter hrgndest saves the merged new area, the second parameter hrgnsrc1, three parameters hrgnsrc2 for the two Region, the fourth parameter fncombinemode is a regional combination, and its value is one of the following combination:
Combination method description
RGN_and establishes the intersection of two regions
RGN_COPY Copy the copy of HRGNSRC1
RGN_DIFF builds a part of the two areas
RGN builds a parallel set of two regions
RGN_XOR establishes a part other than two areas
example:
// Create a combined shape of a circular and rectangular intersection
HRGN HRECT = CreateRectrGN (0, 0, 300, 300);
HRGN Helliptic = CreateellLipTicRGN (0, 0, 400, 250);
Combinergn (HRECT, HRECT, HELLIPTIC, RGN_OR);
SetwindowRgn (Handle, HRECT, TRUE);
When you need to restore the window as a standard Windows Rectangular window, just set the SETWINDOWRGN function's HRGN parameter, such as:
SetwindowRgn (Handle, 0, True);
API skills (2)
(2) Number of numbers from the system sound card
When we write a multimedia program (such as a player), sometimes you need to detect whether the sound card is installed in your computer, and if you do not act, you will terminate it.
Here, we should use the API functionWaveoutGetNumDevs to call this function to return to the number of sound cards installed in the system. In C Builder 5.0, it is included inside the header file "mmsystem.h".
example:
1. First add the code containing the header file in the program head:
#include $ # @ 60; mmsystem.h $ # @ 62;
2, add the following code in the oncreate event of the form: int Num;
// Number of sound cards
Num = WaveoutGetNumdevs ();
IF (NUM)
ShowMessage ("You have installed" INTOSTR (NUM) "block sound card");
Else
{
ShowMessage ("You don't have a sound card! / N program termination!");
CLOSE ();
}
3, compile the running program.
API skills (2)
(3) Get, set the interval time of the mouse double-click
During the specified interval, the continuous two mouse click operations are called double-click, and the time double-tanked interval can be changed in the mouse properties in the control panel. To get the interval between or set the mouse double-click in your self-contained application, we only need to use the two API functions of Windows GetDoubleClickTime and SetDoubleClickTime. Calling getDoubleClicktime can return the interval time of the mouse to double-click, and use SetDoubleClickTime, you can set the time of the mouse to double click.
Let's make a simple program for obtaining and setting up a mouse double-tap time:
First, create a new project in Borland C Builder 5.0, add two Button components to form Form1, change their CAPTION attributes to "Get Double-click Interval Time" and "Set Double-click Interval Time", add an edit component. , Change the Text attribute of Edit1 to "200", add a Label component, and change the CAPTION attribute to "milliseconds".
Then, double-click button button1, add the following code in its OnClick:
// Return to the mouse Double click time
ShowMessage ("Double-click Interval Time" INTTOSTR (GETDOUBLECLICKTIME ()) "Mix");
Double-click button button2, and join the code in its OnClick event:
// Set the mouse double click interval
SetDoubleClicktime (STRTOINT (Edit1 - $ # @ 62; text));
Finally, press F9 to compile and run the program. Click the "Get Danger Interval" button in the window to pop up a time to display the current system mouse, if you want to set the mouse double-click interval, just change the value in the text box, such as 300, then click "to double click The interval time button is OK. It should be noted that the unit of the mouse double-hit interval is milliseconds, the smaller the value of the setting, the smaller the interval, the faster the speed of the double click, the system default is 400 milliseconds, but don't set it too little, otherwise "My Computer" will not beaten (your speed is not fast enough, ^ _ ^).
API skills (2)
(4) Start the control panel console application
There are a lot of control panel projects in the control panel, which are console applications, which are all standard DLLs files, and we often need to configure Windows through them. Rundll32.exe is dedicated to calling a DLL file. In C Builder programming, we can use the API function WINEXEC to run external programs Rundll32.exe calling DLL to implement the console application of the launch control panel. Below is a collection of some examples of calling DLL launch console applications:
1, open the control panel
Winexec ("rundll32.exe shell32.dll, control_rundll", sw_shownormal); 2, open mode dialog
Winexec ("Rundll32.exe shell32.dll, openas_rundll" c: //autoexec.bat ", sw_shownormal);
3, add MODEM
Winexec ("Rundll32.exe shell32.dll, control_rundll modem.cpl, add", sw_shownormal);
4, add printers
Winexec ("Rundll32.exe Shell32.dll, Shhelpshortcuts_Rundll Addprinter", SW_SHOWNORMAL);
5, copy disk
Winexec ("Rundll32.exe Diskcopy.dll, DiskcopyRundll", SW_SHOWNORMAL);
6, mouse
Winexec ("rundll32.exe shell32.dll, control_rundll main.cpl", sw_shownormal);
7, network
Winexec ("Rundll32.exe shell32.dll, control_rundll netcpl.cpl", sw_shownormal);
8, password
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Password.cpl", SW_SHOWNORMAL);
9, game controller
Winexec ("rundll32.exe shell32.dll, control_rundll joy.cpl", sw_shownormal);
10, date / time
Winexec ("Rundll32.exe shell32.dll, control_rundll timedate.cpl", sw_shownormal;
11, Internet attribute
Winexec ("rundll32.exe shell32.dll, control_rundll inetcpl.cpl", sw_shownormal);
12, add / delete programs
// Installation / Uninstall
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll AppWiz.cpl, 1", SW_SHOWNORMAL);
// Windows installation
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll AppWiz.cpl, 2", SW_SHOWNORMAL);
//Startup Disk
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll AppWiz.cpl, 3", SW_SHOWNORMAL);
13, regional settings
//regional settings
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll INTL.CPL, 0", SW_SHOWNORMAL);
//digital
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll INTL.CPL, 1", SW_SHOWNORMAL);
//currency
Winexec ("Rundll32.exe shell32.dll, control_rundll intl.cpl, 2", sw_shownormal);
//time
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll INTL.CPL, 3", SW_SHOWNORMAL);
//date
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll INTL.CPL, 4", SW_SHOWNORMAL); 14, auxiliary options
//keyboard
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Access.cpl, 1", SW_SHOWNORMAL);
//sound
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Access.cpl, 2", SW_SHOWNORMAL);
//display
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Access.cpl, 3", SW_SHOWNORMAL);
//mouse
Winexec ("rundll32.exe shell32.dll, control_rundll access.cpl, 4", sw_shownormal);
//conventional
Winexec ("rundll32.exe shell32.dll, control_rundll access.cpl, 5", sw_shownormal);
15, multimedia
// audio
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll MMSys.cpl, 0", SW_SHOWNORMAL);
//video
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll MMSys.cpl, 1", SW_SHOWNORMAL);
// MIDI
Winexec ("rundll32.exe shell32.dll, control_rundll mmsys.cpl, 2", sw_shownormal);
// CD music
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll MMSys.cpl, 3", SW_SHOWNORMAL);
//equipment
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll MMSys.cpl, 4", SW_SHOWNORMAL);
16, system
//conventional
Winexec ("Rundll32.exe shell32.dll, control_rundll sysdm.cpl, 0", sw_shownormal);
// Device Manager
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Sysdm.cpl, 1", SW_SHOWNORMAL);
/ / Hardware configuration file
Winexec ("Rundll32.exe shell32.dll, control_rundll sysdm.cpl, 2", sw_shownormal);
//performance
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Sysdm.cpl, 3", SW_SHOWNORMAL);
17, display
//background
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Desk.cpl, 0", SW_SHOWNORMAL);
//screen protector
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Desk.cpl, 1", SW_SHOWNORMAL);
//Exterior
Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Desk.cpl, 2", SW_SHOWNORMAL);
// Setting
WINEXEC ("Rundll_Rundll Desk.cpl, 3", SW_SHOWNORMAL); these examples can be used in Windows 98 and Windows 2000, provided that the project is installed in the control panel.
API skill collection (3)
(1) The title bar of the scintillation program
In some professional applications, when the program needs to remind the user or to cause the user to pay attention, the title bar of the program is kept. To implement this feature, only one TIMER component and use an API function - FlashWindow.
Using the API function flashWindow can flash to display the specified window, let the window switches between activities and non-active status, it has two parameters: hwnd and binvert, the header file is "Winuser.h". Where the parameter hWnd is a window handle to flash. When the parameter binvert is a BOOL variable, when the program window title bar is switched from the active state, or switch, when set to false, the window title bar is restored. For the initial state. If a time component (TIMER component) is fitted, the statement is executed at a certain time:
FlashWindow (Form1 - $ # @ 62; handle, true);
The title bar of the program window is constantly switching between activities and non-active states. If you specify hWnd to become an application's handle (Application - $ # @ 62; handel), the program bar will be blocked on the taskbar.
Let's take a blinking window title bar and program on the taskbar.
First, add three buttton button1, button2, button3 to Form1, the properties of the "flash window title bar", "Flashing Task Title Bar", "Blinking Task Title Bar", and "Stopping Flash", add two time components Timer1 and Timer2, Setting the Enabled attribute of the two Timer components to false, setting the interval property to 500 (ie, half;), changing the value of this property can modify the frequency of the flicker.
Then, double-click Timer1, join in the ONTIMER event:
FlashWindow (Form1 - $ # @ 62; handle, true);
Double-click Timer2 and join in the ONTIMER event:
FlashWindow (Application - $ # @ 62; handel, true);
Double click on Button1, add: in the Button1 on the onclick event:
Timer1 - $ # @ 62; enabled = true;
Double-click Button2, join in the Button2 onclick event:
Timer2 - $ # @ 62; enabled = true;
Finally, double-click Button3, join in the Button3 onclick event:
Timer1 - $ # @ 62; enabled = false;
Timer2 - $ # @ 62; enabled = false;
Flashwindow (Form1 - $ # @ 62; handle, false);
FlashWindow (Application - $ # @ 62; handel, false);
This way, a simple example is completed. Press F9 to compile the running program, you can flash the window title bar or the flashing task bar? The title bar.
(2) Drag no title form
Now Windows applications, mostly used graphical interfaces, irregular window technology, making the program interface more beautiful. However, using the interface, you should first remove the title bar of the form (in BCB, set the Border attribute of the form to bsnone, you can remove the title bar of the form), so you can't use the original title bar, There is a problem that the window cannot move. How do you use a mouse to drag the form without the title bar? We can use Windows's API function sendMessage to solve this problem. First, create a new project, set the Border attribute of the form to BSnone to remove the form of the form, press the F12 key to switch to the code editing window, add the code containing the header file "Winuser.h" at the head:
#include $ # @ 60; winuser.h $ # @ 62;
Then, add the following code in the onmousedown event of the form:
if (Button == Mbleft) // Judging whether the left mouse button is pressed
{
ReleaseCapture (); // Release mouse operation
SendMessage (handle, wm_nclbuttondown, htcaption, 0);
}
In this way, the window is dragged with the left mouse button, you can implement the window that is not a title. An assembly can also be added to the form, and then add the above code in the onmousedown event of the component, which can also sit in the component drag window. You can also modify the first parameter of the SendMessage function to the handle of this component, such as: Add a Button component to the form, add the above code in its OnMouseDown event, which is changed to:
SendMessage (Button1 - $ # @ 62; handle, wm_nclbuttondown, htcaption, 0);
This allows you to move Button1 with the mouse in the range of the program.
(3) Hidden program in the taskbar icon
Using the API function showWindow can hide an icon in the taskbar, which is included in the header file "Winuser.h".
1. The code of the hidden taskbar icon is:
ShowWindow (Application - $ # @ 62; handle, sw_hide);
2. Use it when you want to re-display:
ShowWindow (Application - $ # @ 62; handle, sw_show);
However, if the program is minimized, the icon in the taskbar will appear again. To minimize the program restore, the program is re-hidden in the taskbar of the taskbar, you can join the hidden program in the form's onpaint event, so that the program is only the taskbar when the program is minimized. An icon will appear, and the icon will be re-hidden when the program restore is minimized.
API skill collection (3)
(4) Restart, turn off Windows
When the user modifies some settings in Windows, Windows often asks if the computer wants to restart the computer, when the user points YES, the computer will automatically restart. This is a typical application of the API function exitwindowsex.
EXITWINDOWSEX, as the name refers to exiting the Windows function, there are two parameters, the first is to exit the Windows option, commonly used: EWX_REBOOT, EWX_SHUTDOWN, EWX_LOGOFF (Logout Current User), Second The parameter system is not used, can be set to 0. In a self-compiled program (such as a registry modification program), when the user modifies a setting to restart the computer, use the EWX_REBOOT option to restart the computer. Such as:
EXITWINDOWSEX (EWX_REBOOT, 0);
Using the wx_shutdown option, shutdown can be implemented. Such as:
EXITWINDOWSEX (EWX_SHUTDOWN, 0);
When you need to log out, use the EWX_LOGOFF option. Such as:
EXITWINDOWSEX (EWX_LOGOFF, 0);
API skill collection (four)
Function name:
SetwindowPOS
head File:
Winuser.h
Function prototype:
Bool setWindowPOS
(
HWND HWND, // window handle
HWND HWNDINSERTAFTER, / / Solder
INT X, / / horizontal coordinates
INT Y, / / vertical coordinates
INT CX, // Wide
INT CY, // High
Uint uflags // window positioning identity
);
Description:
This function can change the size of the window, the location, and setting sub-windows, pop up the order of the window, or the top window.
return value:
BOOL, if the return value is not zero syndrome, returning zero means failed. For error messages, see the getLastError function.
Parameters Table:
Parameter type and instructions
HWND HWND, window handle wants to locate
HWNDINSERTAFTER HWND, placed in the window handle in front of HWnd. This parameter must be one of the handles of the window or one of the following values: hWnd_bottom places the window in the bottom of all other windows
HWND_NOTOPMOST places the window at the top of all of all windows and is located behind any top window. If this window is not top window, this tag does not affect the window.
HWND_TOP places the window at the top of all of it
HWND_TOPMOST places the window at the top of all of all windows and is located in front of any top window. Even if this window is not a moving window, it also maintains the top state
x:
INT, designated window new X coordinate
Y:
INT, designated window new Y coordinate
CX:
INT, designated window new width
CY:
INT, designated window new height
Wflags:
UINT, specify the tag of the window status and location. This parameter uses the combination of the following value: SWP_DRAWFRAME draws a box around the window
SWP_FRAMECHANGED sends a WM_NCCALCSIZE message to enter the window, even if the size of the window does not change. If this parameter is not specified, the message WM_NCCALCSIZE is only sent when the window size changes.
SWP_HIDEWINDOW Hide Window
SWP_NOACTIVATE does not activate the window
SWP_NOCOPYBITS Shielded Customer Area
SWP_NOMOVE keeps the current position (X and Y parameters will be ignored)
SWP_NOOWNERZORDER does not change the location and arrangement of all windows
SWP_NOREDRAW window does not automatically
SWP_NOREPOSITION The same R> SWP_NOSENDCHANGING is the SWP_NOOWNOSENDCHANGING to prevent this window from accepting WM_WINDOWPOSCHANGING message SWP_NOSUE keeps the current size (CX and CY will be ignored)
SWP_NOZORDER keeps the window in the current location of the list (HWndInsertAfter will be ignored)
SWP_SHOWWINDOW display window
Remarks:
If the SWP_SHOWINDOW or SWP_HIDEWINDOW tag is set, this window does not move or change the size. After the window becomes the top window, all of its sub-windings will also enter the top. Once it is set to non-top, all of its sub-windings will also turn to non-top.
related functions:
MoveWindow, SetActiveWindow, SetForegroundWindow, SETFOREGROUNDOW
example:
// Set the top window
Setwindowpos (Handle, Hwnd_topmost, 0, 0, 0, 0, SWP_NOMOVE || SWP_NOSIZE);
/ / Cancel the top window
Setwindowpos (Handle, Hwnd_Notopmost, 0, 0, 0, 0, SWP_NOMOVE || SWP_NOSize);
API skill collection (four)
Super link effect
In many shared software, there are some hyperlinks in the dialog, such as the home URL or E-mail, when the mouse is moved above, the text turns red, when the mouse is left, the text is Change back to the original blue. If you click this link with the mouse, you will pop up the browser window Open the specified URL or write a new message with the default E-mail program, just like the true hyperlink. Do you want to do one in your program? In fact, we only need to call the API function shellexecute and change the color of the text when the mouse moves, you can have this effect in your own program.
First create a new project, add two Label components on the form FORM1, and their Name property uses the default Label1 and Label2.
Then add the code in the oncreate event of Form1:
Label1 - $ # @ 62; cursor = crhandpoint;
Label2 - $ # @ 62; cursor = crhandpoint;
Label1 - $ # @ 62; font - $ # @ 62; color = CLBLUE;
Label2 - $ # @ 62; font - $ # @ 62; color = CLBLUE;
Label1 - $ # @ 62; caption = "Home: Beginner House";
Label2 - $ # @ 62; capen = "e-mail: fdlweb@sina.com";
Add it in Label1's onclick (click) event:
// Blue word please change into your homepage address
Shellexecute (Handle, Null, "http://fdlweb.myrice.com/", null, null, sw_shownormal);
Add: in the OnMouseMove event:
Label1 - $ # @ 62; font - $ # @ 62; color = CLRED;
Join in the Label2's OnClick event:
// Blue word please change into your own email address
Shellexecute (Handle, Null, "Mailto: fdlweb@sina.com", null, null, sw_shownormal;
Add: label2 - $ # @ 62; font - $ # @ 62; color = CLRED;
Finally, in the ONMOMOVE event in Form1:
Label1 - $ # @ 62; font - $ # @ 62; color = CLBLUE;
Label2 - $ # @ 62; font - $ # @ 62; color = CLBLUE;
The code is input, pressing the F9 compiling running program to see the effect.
Copy screen
The Bitblt function can copy a bitmap from a device scenario to another, which is often used in gramphock programs and game programming, and can also be used to do a desktop-based screen saver. Let us use the Bitblt function to make a false desktop program:
First, add an image component to the form, set the Border of the form FORM1 to: bsnone.
Then the oncreate event in the form is added to the program code:
LEFT = 0;
TOP = 0;
Width = Screen - $ # @ 62; width;
HEIGHT = Screen - $ # @ 62;
Image1 - $ # @ 62; left = 0;
Image1 - $ # @ 62; top = 0;
Image1 - $ # @ 62; width = screen - $ # @ 62; width;
Image1 - $ # @ 62; height = screen - $ # @ 62;
// This code is to copy the desktop to the component image1 to store,
// Where GetDC (0) returns the handle of the desktop device (HDC)
Bitblt (image1 - $ # @ 62; canvas - $ # @ 62; handle, 0,0, screen - $ # @ 62; width, screen - $ # @ 62; height, getdc (0), 0, 0, SRCCOPY );
Press F9 to run, a fake desktop is coming out, how to make a react in this "desktop", can be used to catch people! . Some desktop games are also doing, you can add more features on this program, such as adding the Label components and Timer components on the form, using the TIMER component to control the Label components on the form In addition, the code "close ();" close (); finally, the extension of the translated program is changed to SCR, and finally, a text is chaotic on the desktop. The moving screen saver is.
Take the total space and remaining space of the disk
To obtain the total space of the disk and the remaining space, the easiest and straightforward method is to call the API function getDiskFreespace.
The getDiskFreespace function has 5 parameters. The first parameter is to determine the drive name of the available space. The second parameter is a variable that stores the number of sectors per cluster. The third parameter is a variable that stores each segment of the number of fields. The fourth parameter is a variable that stores the number of remaining clusters, and the fifth parameter is a variable that stores total clusters. The total space or remaining space of the specified drive can be obtained with the formula of the corresponding calculation disk space.
The calculation formula of the total space of the disk and the remaining space is:
The remaining space (byte) = number of sectors of the cluster * bytes of the number of sectors * Residual clusters
Total space on disk (byte) = number of sectors of the cluster * The number of bytes of sectors * total cluster number
Here is an example of obtaining the total space of the C disk and the remaining space:
UNSIGNED Long Sectors, Bytes, Free, Total
GetDiskFreespace ("C: //", & Sectors, & Bytes, & Free, & Total; // Available Space (Unit: MB)
INT freekb = bytes * SECTORS * Free / 1024;
// Total space (unit: MB)
INT TOTALKB = BYTES * SECTORS * TOTAL / 1024;
ShowMessage ("The available space of the C disk is:" INTOSTR (FreeKB) "MB, the total space is:" INTALKB) "MB");
API skills (5)
Extraction icon
Calling the API function Extracticon can extract the icon in the program file, and its header file is shellapi.h, the prototype is:
HiCon Extracticon
(
Hinstance hinst, // instance handle
LPCTSTR LPSZEXEFILENAME, / / to extract the file name of the program of the icon
UINT NICONINDEX / / The index of the icon to extract
);
When calling this function, the parameter Hinst is generally set to the current application instance handle, such as form1 - $ # @ 62; handle.
The parameter lpszexefilename is a full path to the program file that needs to extract the icon. This program file can be an Exe file, a DLL file, an ICO file, and the like, as long as it is a file containing the icon resource, you can extract icons.
When the parameter niconIndex specifies an index of an icon to return the handle of the icon, if there is no icon in the specified file, return zero, return zero, when the parameter niconIndex is set to -1, the function returns the total number of icons.
The handle returned by the function can assign a variable that is declared with a TICON class, and then saves the icon to save the icon using the savetofile method of this variable.
example:
Ticon * icon = new ticon ();
Ansistring filename = "c: //windows//system/shell32.dll";
Int Totalicon;
// Get the total number of icons of the file shell32.dll
Totalicon = (int) Extracticon (Form1-> Handle, filename.c_str (), -1);
/ / Extract the first icon, 0 is the first, 1 is the second, the class push ...
Icon-> Handle = Extracticon (Form1-> Handle, FileName.c_STR (), 0);
/ / Save icon
Icon-> Savetofile ("C: //1.ico");
A complete icon extract source code is given below.
This program requires four button controls (Button), four text tab control (Label), two text box controls (Edit), a horizontal scroll bar control (Scrollbar), an open file dialog control (OpenDialog), a save File dialog control (Savedialog) and a picture control (Image), and a Panel control is decorated. The interface is shown in the figure:
Arrange each control, then modify one of the four Label controls, the last input program code, run the program, a program extracted an icon, you will not be worn with no icon resources. Program list (Unit1.cpp):
// ----------------------------------- -------------
#include $ # @ 60; vcl.h $ # @ 62;
#pragma HDRSTOP
#include "unit1.h"
/ / -------------------------------------------------------------------------------------------- ----------------
#pragma package (smart_init)
#pragma resource "* .dfm"
TFORM1 * FORM1;
Ansistring filename;
Ticon * icon = new ticon ();
Int Totalicon;
/ / -------------------------------------------------------------------------------------------- ----------------
__fastcall tform1 :: tform1 (tComponent * Owner)
: TFORM (OWNER)
{
}
/ / -------------------------------------------------------------------------------------------- ----------------
Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)
{
CAPTION = "Icon thief 1.0";
Button1 - $ # @ 62; capen = "Select File";
Button2 - $ # @ 62; caption = "Save Icon";
Button3 - $ # @ 62; caption = "Save all";
Button4 - $ # @ 62; caption = "Exit";
Edit1 - $ # @ 62; Text = 0;
Edit2 - $ # @ 62; Text = 0;
Image1 - $ # @ 62; width = 32;
Image1 - $ # @ 62; height = 32;
OpenDialog1 - $ # @ 62; filter = "executable file (* .exe, *. DLL) | * .exe; *. Dll | icon file (* .ico) | * .ico | All files (*. *) | *. * ";
Savedialog1 - $ # @ 62; filter = "icon file | * .ico";
Scrollbar1 - $ # @ 62; enabled = false;
Button2 - $ # @ 62; enabled = false;
Button3 - $ # @ 62; enabled = false;
}
/ / -------------------------------------------------------------------------------------------- ----------------
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
IF (OpenDialog1 - $ # @ 62; Execute ())
{
Totalicon = (int) Extracticon (Form1 - $ # @ 62; handle, OpenDialog1 - $ # @ 62; filename.c_str (), -1);
IF (Totalicon $ # @ 62; 0)
{
IF (Totalicon $ # @ 60; 2)
Scrollbar1 - $ # @ 62; enabled = false; else
Scrollbar1 - $ # @ 62; max = Totalicon-1;
Button2 - $ # @ 62; enabled = true;
Button3 - $ # @ 62; enabled = true;
Filename = OpenDialog1 - $ # @ 62; filename;
Edit1 - $ # @ 62; Text = Totalicon;
Icon - $ # @ 62; Handle = Extracticon (Form1 - $ # @ 62; handle, filename.c_str (), 0);
Image1 - $ # @ 62; Picture - $ # @ 62; icon = icon;
Edit2 - $ # @ 62; Text = 1;
}
Else
{
ShowMessage ("This file is not icon");
}
}
}
/ / -------------------------------------------------------------------------------------------- ----------------
Void __fastcall tform1 :: button2click (Tobject * Sender)
{
IF (Savedialog1 - $ # @ 62; execute ())
{
/ / Save icon
Icon - $ # @ 62; savetofile (SaveDialog1 - $ # @ 62; filename);
}
}
/ / -------------------------------------------------------------------------------------------- ----------------
Void __fastcall tform1 :: button3click (TOBJECT * Sender)
{
IF (Savedialog1 - $ # @ 62; execute ())
// Abstract all icons
For (int i = 0; i $ # @ 60; totalicon-1; i )
{
Icon - $ # @ 62; Handle = Extracticon (Form1 - $ # @ 62; handle, filename.c_str (), i);
Icon - $ # @ 62; savetofile (Savedialog1 - $ # @ 62; filename (ANSISTRING) i ". ICO");
}
}
/ / -------------------------------------------------------------------------------------------- ----------------
Void __fastcall tform1 :: button4click (TOBJECT * Sender)
{
CLOSE ();
}
/ / -------------------------------------------------------------------------------------------- ----------------
Void __fastcall tform1 :: scrollbar1change (TOBJECT * SENDER)
{
Edit2 - $ # @ 62; Text = Scrollbar1 - $ # @ 62; position 1;
Icon - $ # @ 62; Handle = Extracton (Form1 - $ # @ 62; handle, filename.c_str (), scrollbar1 - $ # @ 62; position);
Image1 - $ # @ 62; Picture - $ # @ 62; icon = icon;
}
/ / -------------------------------------------------------------------------------------------- ----------------
Judging the type of drive
Using the API function getDriveType can determine the type of drive, the function returns an INT type value, when the return value is 2, is a floppy disk; 3, it is a hard disk; 4, it is a network map; 5 o'clock, Is the optical drive; 6, it is a RAM disk; when it is other value, it is an illegal disk. This API function is included in the winbase.h header file, first add statement on the program: include $ # @ 60; winbase.h $ # @ 62;
Contains the header file, then add the following code to the program to determine the type of drive:
INT DRV;
/ / "C: //" is the drive letter to be judged
DRV = GetDriveType ("C: //");
Switch (DRV) / / Decision DRV value
{
Case 2: // drive_removable
ShowMessage ("floppy disk");
Break;
Case 3: // drive_fixed
ShowMessage ("hard disk");
Break;
Case 4: // drive_remote
ShowMessage ("Network Mappings");
Break;
Case 5: // drive_cdrom
ShowMessage ("CD-ROM");
Break;
Case 6: // drive_ramdisk
ShowMessage ("RAM Disk");
Break;
DEFAULT:
ShowMessage ("This disk does not exist!");
Break;
}
Note: The values after the CASE statement can also be replaced with the constant after the comment. Such as 2 can be replaced with constant drive_removable.
API skills set (7)
Window minimization, maximize and restore
By calling the API function showWindow, you can control the status of the specified window, such as minimizing the window, maximizing or restoring the original state, and so on.
Although the window can also minimize, maximize or restore the control buttons on the window title bar, but the showWindow function can achieve more features, and the window minimizes the window to the desktop, these are standard control The button can't do.
Here is an example of a showwindow function. This example demonstrates how to minimize a window to the desktop or minimize the taskbar and maximize, and restore the original state.
First, create a new project in C Builder, in order to facilitate demonstration, the project requires two windows. The method of adding the second window FORM2 is: Select "New Form" under the "File" menu. After adding a new window, select "Include Unit HDR ..." under the "File" menu contains the header file "Unit2.h" of the window FORM2, or add "#include" directly in the head of the code editing window of Form1. "Unit2.h" statement. Then put four buttons on Form1, their CAPTION attributes are "maximized", "minimize desktop", "recovery", and "minimize the taskbar and restore".
Then, double-click the window piece Form1, add: in its oncreate event:
Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)
{
/ / Set Form1 is the top window
Form1 - $ # @ 62; formstyle = fsstayontop;
}
Double-click button button1, join in its OnClick event:
Void __fastcall tform1 :: button1click (TOBJECT * Sender) {
//maximize
ShowWindow (Form2 - $ # @ 62; handle, sw_maximize);
}
Double-click button button2, join in its OnClick event:
Void __fastcall tform1 :: button2click (Tobject * Sender)
{// minimize to the desktop
ShowWindow (Form2 - $ # @ 62; handle, sw_minimize);
}
Double-click button button3, join in its OnClick event:
Void __fastcall tform1 :: button3click (TOBJECT * Sender)
{
// Restore minimization
ShowWindow (Form2 - $ # @ 62; handle, sw_restore;
}
{{秪峈 Most 郔 苤 苤 恄魊 恄魊 恄魊 鞶 鞶 傿蒚鰽 奴 埶朒 锸 ◆ ◆ ◆ 譬 賱 賱 阭Building? 珨跺晊奀 珨跺晊奀 测 ㄩ ㄩ
Void __fastcall tform1 :: button4click (TOBJECT * Sender)
{
/ / Minimize the taskbar
ShowWindow (Application - $ # @ 62; handle, sw_minimize);
// delay 1 second
Sleep (1000);
// Restore minimization
ShowWindow (Application - $ # @ 62; handle, sw_restore;
}
Finally, compile the running program.
Get disk serial numbers, volume labels and file system types
With the API function getVolumeInformation, you can get information about a disk, such as the serial number of the disk, the volume, the file system type. Some software is encrypted using the serial number of the disk.
When you need to get disk information, add the following code:
// Define the band string variable buffer with a length of 255:
Ansistring volume = ansistring :: stringofchar ("", 255);
//serial number
Unsigned long serialnumber;
// Define file system type string buffer with a length of 20
Ansistring systemname = Ansistring :: StringOfchar ("", 20);
// Get disk information
GetVolumeInformation ("C: //", Volumename.c_STR (), 255, & SerialNumber, 0, 0, SystemName.c_STR (), 20);
ShowMessage ("" "Coonsted:" Trim (Volumename));
ShowMessage ("Serial Number of C Dish:" INTTOSTR (SerialNumber);
ShowMessage ("" C-Document System Type: " Trim (SystemName));
Screen magnifying glass
You must have used the screen that comes with Windows98, let you do it, do you want to do it? Others, the key to this program is to use the API function Stretchblt.
Call the API function stretchBLT can put a bitmap specified in a device from copy to another device, during copying, can also zoom bitmap as needed.
Here is its prototype and parameter description:
Bool Stretchblt
(
HDC HDCDEST, / / Target Equipment Handle
INT nxorigindest, // target X coordinate in the upper left corner of the upper left corner
INT NYORIGINDEST, / / The Y coordinate in the upper left corner of the target rectangle
INT NWIDTHDEST, / / Width of the target rectangle
INT NHEIGHTDEST, / / Target Rectangle Height
HDC HDCSRC, // Source Equipment Handle
INT nxoriginsrc, // source X coordinate in the upper left corner of the source
INT NYORIGINSRC, / / Y coordinate in the upper left corner of the source
INT NWIDTHSRC, // Source Rorm Width
INT NHEIGHTSRC, // Source rectangular height
DWORD DWROP / / Raster Operation
);
The header file of the Stretchblt function is "Wingdi.h". Among them, its DWROP parameters have 15 operations, the most commonly used copying operation SRCCOPY. When the rectangular size specified by the source device and the target device is not equal, the function is enlarged or reduced according to the size ratio of the source rectangle and the target rectangle, copies to the target device.
Below is an example of copying the rectangular bitmap on the left upper corner coordinate of the screen to the image control image1 after the rectangular bit map of 100, wide and high is 100:
Stretchblt (image1 - $ # @ 62; canvas - $ # @ 62; handle, 0, 0, 50, 50, getdc (0), 0, 0, 100, 100, srccopy);
The getDC (0) statement of this code is the handle of the desktop device.
If you continue to use the above code, put the screen to image1, which is a "screen shrinkage". Of course, the reduction screen does not have actual use, we can make "screen magnifying glass" as long as we change it.
To do this "screen magnifying glass", first run Borland C Builder, put a picture control image1 and a time control Timer1 on the form form1.
Double-click Form1, add code in its oncreate event:
Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)
{
Image1 - $ # @ 62; width = 200;
Image1 - $ # @ 62; height = 200;
Timer1 - $ # @ 62; interval = 10;
// Set the top window
Setwindowpos (Handle, Hwnd_topmost, 0, 0, 0, 0, SWP_NOMOVE || SWP_NOSIZE);
}
Double-click Time Control Timer1, add the following code in its ONTIMER event:
Void __fastcall tform1 :: Timer1Timer (TOBJECT * SENDER)
{
INT X, Y;
Point curpos;
/ / Get the current coordinates of the mouse
GetCursorpos (& Curpos);
/ / Guarantee the image of the copy is not outside the screen
IF (Curpos.x $ # @ 62; Screen - $ # @ 62; Width - 100)
X = Screen - $ # @ 62; width - 100;
Else IF (Curpos.x $ # @ 60; 50)
X = 0;
Else
X = Curpos.x-50;
IF (curpos.y $ # @ 62; screen - $ # @ 62; height-100)
Y = screen - $ # @ 62; height-100;
Else IF (Curpos.y $ # @ 60; 50)
y = 0;
Else
y = curpos.y-50;
Caption = "coordinate:" INTOSTR (CURPOS.X) "," INTOSTR (Curpos.y);
Image1 - $ # @ 62; Canvas - $ # @ 62; FillRect (RECT (0, 0, 200, 200)); // Copy Enlarge Image
Stretchblt (image1 - $ # @ 62; canvas - $ # @ 62; handle, 0, 0, 200, 200, getdc (0), x, y, 100, 100, srccopy);
}
The code is entered, press F9 or click the Run button to run the program. how about it? It's almost the same as Windows comes!
It should be noted that: C Builder is like VB. The picture control can be non-persistent. In C Builder, it is not possible to directly call the StretchBLT function to get a coherent image, but must first use the picture control FillRect method to populate The picture box (equivalent to the empty picture frame), then call the StretChblt function.
Time delay
Delay is very meaningful in programming! For example, the wait picture at the time of the program, or you need to wait for the completion of a process to run the code below, then use the time delay.
But there is generally no ready-made delay function in many programming languages. In DOS's C era, when the program needs to be delayed, many people use for loops:
For (int i = 0; i $ # @ 60; 10000; i );
There may be someone in now. In the WIN32 era, a function of time delay has been provided in the system's API library, it is the SLEEP function. Of course, in Windows programming, you can also use the Timer control, but use the SLEEP function more convenient and fast. We only need to simply call the SLEEP function, you can realize the delay of time.
The SLEEP function has only one parameter cmilliseconds that specifies the time you need to delay, and its unit is millisecond. Let us look at the example below, please add the following code in a click-OnClick event:
SLEEP (3000); // Delayed three seconds
SHOWMESSAGE ("This dialog has been delayed for three seconds!");
This example demonstrates a delayed dialog box, click the button, the discounted dialog that will be popped up is delayed for three seconds.