[Code: 1: D5DC046D55]
1. How to create a DLL in C Builder
2. Save information in Win.ini with C Bulider
3. How to detect hardware in C Builder
4.C Builder How to Respond to Messages and Custom Messages
5. Develop an animation DLL using C Builder
6. Make a screen saver with C Builder 3
7.TCP / IP head format
8.udp
9. Judging the Desktop and other directories of Windows
10 Create a digital signature with C Builder
11 Method for controlling focus switching with ENTER
12. Block Windows Message
13. Using COMMATEXT
14. The program is displayed first when the program starts.
15. How to get the command line parameter of the program?
16. How to monitor clipboard
17. How to use onIdle events
18. Write a serial asynchronous communication program with C Builder
19.c Builder Message Processing Tips for Non-Visual Components
20. Establish a database VCL using C Builder
21. Create an Internet-based point-to-point chat with C Builder
22. Get the application icon with C Builder
23. BiG5 to GB conversion technology
24.c Builder makes your taskbar map tap
25.tform
26. Create shortcuts with BCB on Windows desktop
27. Read magnetic district
28.I / O port reading and writing
29. Detect the mouse position
30. Let Win32 application jump into the system zero layer
31. How to get MEMO's rows and columns
32. Using Sockets
33. How to hide the app under Windows95 / 98 Do not let it appear in the CTRL-ALT-DEL dialog box?
34. How to hide the task bar icon of the app
35. Write your own ping.exe program
36. Prepare a service with C Builder under Winnt
37. How to automatically turn off Windows screensaver in C Builder
38. Show / hide the taskbar icon
39. Mailbox monitoring program
40.c building make a alarm
41. Inspector of dial-up IP address
42. Write the TRAY program with C Builder
43. How to use code to minimize or recover procedures
44. Copyright window before making the main window display
45. Decision has been connected to the Internet
46. Get login username
47. Hide desktop icon
48. Run when the program starts
49. Control panel call
50. Analog Keyboard Button
51. Let the title bar flash
52. Start screen protection
53. Tissue of the year and month
54. Keyboard event
55. Hidden taskbar
56. Disable shutdown
57. How to start the program in a minimized manner
58. After adding a line in MEMO, how to make the last line can display
59. Set wallpaper method
How to create a DLL in C Builder
Since the launch of C Builder, it has attracted a lot of Delphi, VC, VB programmers since the romantic Valentine's Day last year, and a lot of C, C programmers sigh: finally has C visual development tools, the same From BC, Delphi to C Builder.
Dynamic Link Library (DLL) is a programming method that is often encountered by Windows. Here I introduce how to create how to use DLL and some tips in BCB (C Builder).
First, create:
Use BCB File | New to create a new DLL project and save file BCB to generate a DLL program frame.
1. The DLLLENTRYPOINT function is an inlet method. If the user is called when the DLL is initialized or logged out, it is used to write to the DLL initializer and uninstaller; parameter: HinST is used to indicate the base address of the DLL; Reason is used to indicate the DLL The call mode is used to distinguish between multi-thread single thread pairs of DLL, create, uninstall DLL; 2. Add your own DLL procedure, function to the program;
3. Describe the exit with DLLIMPORT;
The routines are as follows:
#Include
#Pragma HDRSTOP
EXTERN 揅? __ declspec (dllexport) int test ();
Int WinAPI DLLENTRYPOINT (Hinstance Hinst, Unsigned Long Reason, Void *)
{
Return 1;
}
Int test ()
{
Return 3;
}
Note: The dynamic link library is called process, and there are different CALL methods __cdecl, __pascal, __fastcall, __pascal, __fastcall, __cdcall, and BCB, if considering compatibility is available, and if the compatibility is available, the __stdcall declaration method is:
EXTERN 揅? __ declspec (dllexport) int __stdcall test ();
For the process, the function is also changed to:
INT __STDCALL TEST ()
Second, use DLL
There are two ways to use DLL in BCB:
1. Static invoice
First, you need to add an input interface library (Import Library), open the project, open the project list, and add the interface library (* .lib) to the project using BCB View | Project Manager.
Second, add an interface declaration in the header file.
The routines are as follows:
// deflude file
EXTERN 揅? __ decspec (dllimport) int __cdecl test ();
// Use function in main program
INT I;
I = test ();
note:
(1) Dynamic Link Library calls, the CALL mode is not written as __cdecl as the mode of creating, and other statements are required.
(2) The DLL created by BCB has a corresponding input interface library (Import library), if there is only DLL without the library, the BCB's IMPLIB tool can be generated: IMPLIB XXX.LIB XXX.DLL; additional: Tlib XXX.LIB, XXX .lst produces a list of internal functions of the DLL, many Windows unless otherwise discovered by this method.
2. Dynamic invoice
Dynamic Call Method To transfer to the DLL library with loadLibrary () and getProcAddress () in the Windows API, pointing to the function location in the library, which is more common.
The routines are as follows:
Hinstance DD;
INT _STDCALL (* DDD) (VOID);
DD = loadingLibrary (搙 xx.dll ?;
DDD = GETPROCADDRESS (DD, 搕 EST ?;
CAPTION = INTSTOSTR (DDD ());
Freelibrary (DD);
Third, pay attention:
When you create a DLL, you should pay attention to the Project Options when compiling.
Packages Tags: Remove Builder With Runtime Packages Check box.
Linker Tags: Remove the USE Dynamic RTL check box. Otherwise, the DLL that is created requires Runtime Packages or Runtime Library.
Save information in Win.ini with C Bulider
Many software now saves the data needed in the program in the registry, so when the software has more and more software, the registry is increasing, and the system is easier to make the system error. Of course, Microsoft also recommends saving data in the registry, but when we need to save data, you can save data in Win.ini for a while, which can be easily maintained, and the implementation is relatively simple. Here are how to do with Borland C Builder as an example.
The principle is actually very simple, just call the API's WriteProfileString and getProfileint functions. The prototype of these two functions is: Bool WriteProfileString (LPCTSTR LPAPPNAME, LPCTSTR LPSTRING);
Uint getprofileint (lpctstr lpappname); int ndefault;
Where lpappname refers to the name in Win.ini, that is, the "à labeled", lpkeyName refers to the name of each item in this segment, lpstring refers to the value of this project, that is, "=", NDEFAULT is When getprofileint does not find the value returned when LPAppName and LPKEYNAME, the default, the former returns to the Boolean (TRUE or FALSE), and the latter returns to the unsigned shaping value. When LPKEYNAME is empty (NULL) in the WriteProfileString function, the content of this section is cleared, the content of this project will be cleared.
The following example will illustrate the usage of these two functions. Create a new application, put two Edit and three button on Form1, where edit's text is empty, and the three Button's CAPTION is "Add", "View", "Clear". Double-click the "Add" button to add the following code:
WriteProfileString ("Example", "Project", Edit1 → Text.c_STR ());
Double-click the "View" button to add the following code:
Unsigned int Temp;
Temp = GetProfileint ("Example", "Project", 100);
Edit2 → Text = INTOSTR (TEMP);
Double-click the "Clear" button to add the following code:
WriteProfileString ("Examples", NULL, NULL;
Then press the F9 key to run the program.
You can check the correctness of the program. Enter a number in Edit1, such as "3265", press the "Add" button, run "Sysedit" to view the last side of the "win.ini" file, you can see the following:
[Example program]
Project = 3265
Where "[]" and "=" are the function of the function. Press the "View" button, "3265" appears in Edit 2, when pressing the Clear button to clear the added part. Viewing the desired purpose has been achieved.
Friends who love programming can apply the above methods into their own programs to achieve the role of saving data information. When you really want to save information to the registry, you can define an object of a Tregistry class in C Builder to perform related operations, or call the Windows API function, how to program you can see the relevant information or contact us. How to detect hardware in C Builder
In the process we have written, it is often dealing with hardware, so how is the device in the system in the program, what is its running status? For beginners, this problem is often unresolved, in fact, just use several API functions, hardware problems are not mysterious. Let's take a look at how to detect hardware in C Builder.
1. Detect the model of the CPU
Let us take a look at your own CPU model first. First, in C Builder, the form shown in Figure 1 is drawn. In the following examples, we will always use this form for demonstration, which includes a METON for activating the test, and a MEMO used to display the results. . We can use the getSystemInfo this API to get the model number of the CPU. Adding the following code to the Button's Click event:
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
// Get the CPU model
System_info systeminfo;
GetSystemInfo (& SystemInfo);
Memo1 → Lines → Add (撃 腃 PU type is: 敚玈 TRING (SystemInfo.dwprocessortype);
}
Run it, click TEST to try, the CPU model is coming out!
2. Detect memory status
The method of obtaining a memory state is similar to the CPU model, but he uses another API: GlobalMemoryStatus.
Among them, members dwtotalphys are used to get the total amount of physical memory, and DWAVAILPHYS is the meaning of effective physical memory. As long as we add the following lines of code to the above programs (do not need to be done, the same):
// Get memory status
MemoryStatus memory;
Memory.dwlength = sizeof (memory); // Initialization
GlobalMemoryStatus (& Memory);
MEMO1 → LINES → Add (撃 锢 砟 诖媸? MB): 敚玈 TRING (int (Memory.dwtotalphys / 1024/1024))))
Memo1 → Lines → Add (fishing zinc-bezer? KB): 敚玈 TRING (INT (Memory. / 1024)))))
How, look at the door? The format of the two programs is almost exactly the same. In fact, GetSystemInfoglobalMemoryStatus can also get many other information about CPU and memory, follow the above format to set up, more detailed information can look at C Builder4 Help.
3. Detect the available hard disk space
Ok, after the warm-up of the two simple questions, let's deal with a slightly complex question: We know that the installer has a process of detecting the hard disk space, then this is achieved? He used the API function GetDiskFreespace This function enters a parameter: the path to the target disk; returns four parameters, in turn, the number of sectors per cluster, the number of bytes per sector, the number of idle clusters, total cluster number. If we need to detect the total capacity and availability of the C disk, then add the following code to the above program: // Get the C disk available space
DWORD SECTOR, BYTE, Cluster, Free;
Long Int FreeSpace, Totalspace;
GetDiskFreespace (揅:? & Sector, & Byte, & Free, & Cluster); // Get return parameters
Totalspace = int (cluster) * int (byte) * INT (SECTOR) / 1024/1024; // Calculation total capacity
Freespace = int (free) * int (byte) * int (sector) / 1024/1024; // Calculate free space
Memo1 → Lines → Add (Total Space (MB): 敚玈 TRING (TOTALSPACE));
Memo1 → Lines → Add (揅 揅 安 用 (MB): 敚玈 TRING (FREESPACE));
How? I can do my own installation now!
How to respond to messages and custom messages in C Builder
Inprise (Borland) C Builder, you can respond as messages as in Delphi, just look slightly more complicated.
For messages defined by the system, you can respond directly:
#define WM_MY_OPEN_CMDLINE_FILE (WM_USER 1) // Custom Messages between Processes
#define WM_MY_Search_node (WM_USER 2) // Find custom message for command
Class Tsomeform: Public TForm
{
// ... other code in the class
protected:
// Message response process
Void __fastcall opencmdlinefile (tMessage Message);
Void __fastcall searchDocumentNode (tMessage message);
Void __fastcall getWindowminMaxInfo (TWMGETMINMAXINFO message);
// The following authentic response of the message is implemented by macro
Begin_MESSAGE_MAP
Message_handler (WM_MY_OPEN_CMDLINE_FILE, TMESSAGE, OpenCMDLINEFILE)
Message_handler (WM_MY_Search_Node, TMESSAGE, SEARCHDocumentNode)
Message_Handler (WM_GETMINMAXINFO, TWMGETMINMAXINFO, GETWINDOWMINMAXIN FO)
END_MESSAGE_MAP (TFORM)
}; // end class
// The following is the implementation of the code
Void __fastcall Tsomeform :: OpenCMDLINEFILE (TMESSAGE Message)
{// pass parameters directly through message structure
LPSTR LPCMDLINE = (LPST) Message.lParam; // Get parameters from Message
This-> handlecmdlinefile (lpcmdline); // Test the parameters of the command line
Return;
}
Void __fastcall tsomeform :: searchdocumentnode (tMessage message) {// Response Find message
The parameters in // Message are not required here.
This-> searchnode ();
Return;
}
Void __fastcall tsomeform :: getWindowminMaxInfo (TwMgetminMaxInfo Messag)
e)
{/ / Set the minimum size of the main window
MinMaxInfo * minMaxinfo = message.minMaxinfo;
MinMaxInfo-> Ptmintracksize.x = 400;
MinMaxInfo-> Ptmintracksize.y = 300;
Return;
}
Where: TMESSAGE and TWMGETMAXINFO type are defined:
C: / program files / borland / cbuilder / inluCDE / VCL / Messages.hpp; other messages
The response method is the same as this.
In addition, a corresponding message structure can be defined for custom messages (such as tsearchnode_mes)
SAGE, as for how to define message structure, you can refer to:
C: / program files / borland / cbuilder / inLuCDE / VCL / Messages.hpp
Develop animation DLL with C Builder
When we perform a copy file, lookup file, or computer is more time-consuming operation, Windows displays a small movie, indicating that the operation is in progress, has a lot of color than the still image of the dead plate. Then we can also show such an animation prompt when we develop software yourself. When I developed a foreign trade application software system, I encountered the amount of data very large. When I was found through a composite condition, because each project of the database table has an index, it is expensive, the system will also show a long pause. Users feel extremely unhappy. After a period of exploration, I developed an animation DLL that can be used under the development environment PowerBuilder used. Due to the multi-threaded programming, the DLL function of PB calls can be resigned in time to PB, and does not affect the operation of the application system. Users can see something in motion, and will not think that the system stopped responding, the feeling time does not seem to have been so long.
Code and compilation options
(1) Select New under the File menu of C Builder, select DLL in the New Item dialog, C Builder creates a blank DLL project.
(2) Select New Form, C Builder under the File menu, create a blank Form, modify its properties as
Borderstyle = BSDIALOG
Bordericons have subasses for False
Formstyle = fsstayontop
Position = poscreencenter
Name = statusform
(3) Add a Win32 animate control animate1 on the Form to modify its properties as
Align = altop
(4) Add a Standard's Button control button_cancel, add the Timer Control Timer1 under System, and set the Timer Interval Time Bit 250 to cancel the user's cancellation request.
Because the PB application system and the animation form code belong to two threads, you cannot use the PB thread to directly close the window of the movie windows. Timed check mark, once the shutdown flag is detected, turn off the window, clear the thread flag, and end the animation thread. The principle of encoding and encoding is given below:
1. DLL DLL body code:
/ *****************************************
* DLL body code
* Define DLL utility variables
* g_commonavi
Animate control animation type index
* GI_CANCELED
Has the button_cancel button is selected
* GI_AVITYPE
The type of animation to display is input by the DLL output function as a parameter.
* GI_REQUESTCLOSE
Request animation thread close sign
* GI_WINDOWACTIVE
The status of the animation window
* LPSWINTILE
The title of the animation form, by the DLL output function as a parameter input
* /
Tcommonavi g_commonavi [] = {
Avinone, AvifindFolder,
AvifindFile, AvifindComputer,
AvicopyFiles, AvicopyFile,
Avirecyclefile, Aviemptyrecycle,
Avideletefile
}
INT GI_CANCELED = 0, GI_AVITYPE = 0;
INT GI_REQUESTCLOSE = 0, GI_WINDOWACTIVE = 0;
Char lpswintitle [256];
HWND HWNDPARENT = NULL;
/ * Define DLL Output Functions * /
Extern "c" __declspec (dllexport)
INT Pascal DllenTryPoint (Hinstance Hinst,
Unsigned long reason, void *);
Extern "C" __DECLSPEC (DLLEXPORT) INT
Pascal ShowStatusWindow (int aviitype,
LPSTR WINTITLE, Long HWND;
Extern "C" __DECLSPEC (DLLEXPORT) INT
Pascal getStatus (intloy_closewin);
Extern "C" __DECLSPEC (DLLEXPORT) INT
Pascal closestatuswindow ();
/ * Define thread TFORMTHREAD: * /
Class TFormThread: Public TTHREAD {
Public: // user declarations
__fastcall tformthread (bool createSuspend);
Void __fastcall execute (void);
}
__fastcall tformthread ::
TFORMTHREAD (Bool CreateSuspend):
TTHREAD (CreateSuspend) {
}
/ * Animation thread execute code,
The timer control of the animation form will turn off,
Clear the operation of the thread after the formation of the form
* /
Void __fastcall tformthread :: execute (void) {
gi_windowactive = 1;
Statusform = New TSTATUSFORM (NULL);
Statusform-> Caption = lpswintitle;
Statusform-> ShowModal ();
Gi_windowactive = 0; delete statusform;
GI_REQUESTCLOSE = 0;
}
/ * Define a thread instance pointer * /
TFORMTHREAD * FORMTHREAD;
/ ******************************************************
* Output Function Code Implementation
* DLLLENTRYPOINT 32-bit DLL entry
* ShowStatusWindow Displays the animation window,
It creates a window by creating a thread to avoid the window
MODAL attributes to return control to the caller
* GetStatus
Get the state of 撊 ∠, ie the user has no choice 撊 麛 button
* CloseStatusWindow Close the animation window,
* /
__Declspec (DLLEXPORT) INT WINAPI DLLENTRYPOINT
Hinstance Hinst, unsigned long reason, void *)
{
Return 1;
}
__Declspec (DLLEXPORT) INT Pascal
ShowStatusWindow (int aviitype, lpstr
Wintitle, long hwnd) {
HWndparent = (hwnd) hwnd;
MEMSET (LPSWINTILE, 0, SIZEOF (LPSWINTILE));
STRNCPY (LPSWINTITE, WINTITLE, SIZEOF (LPSWINTILE) -1);
IF (AvityPe> 0 && Avitype <= 8)
GI_AVITYPE = Avitype;
FormthRead = New TFormThread (TRUE);
FormthRead-> priority = tpnormal
Formthread-> resume ();
}
__Declspec (DLLEXPORT) INT Pascal
GetStatus (int ai_closewin) {
IF (GI_CANCELED)
IF (gi_windowactive) {
GI_REQUESTCLOSE = 1;
While (Gi_RequestClose);
}
Return gi_canceled;
}
__Declspec (DLLEXPORT) INT Pascal
ClosestatusWindow () {
IF (gi_windowactive) {
GI_REQUESTCLOSE = 1;
While (Gi_RequestClose);
}
Return gi_canceled;
}
2. Forms STATUSFORM code:
TSTATUSFORM * STATUSFORM;
// -----------------------------------
Extern Int gi_canceled;
Extern Int Gi_AVITYPE;
Extern Tcommonavi g_commonavi [];
__fastcall tstatusform ::
TSTATUSFORM (HWND PARENTWINDOW)
: Tform (ParentWindow)
{
GI_CANCELED = 0;
}
// -----------------------------------
// The cancel button does not turn off the form directly.
And indicate setting cancellation logo, for the transferr
Void __fastcall tstatusform ::
Button_cancelclick (TOBJECT * SENDER)
{
GI_CANCELED = 1;
// MODALRESULT = MRCANCEL;
}
// ----------------------------------- // Activate the animation, in the FormCreate event
Void __fastcall tstatusform ::
Formcreate (TOBJECT * Sender)
{
Animate1-> Commonavi = g_commonavi [GI_AVITYPE];
Animate1-> Active = true;
}
// -----------------------------------
Extern Int gi_requestclose;
// Timer event detects the end flag to close the form
Void __fastcall tstatusform ::
Timer1Timer (TOBJECT * SENDER)
{
IF (GI_RequestClose) {
ModalResult = MROK;
}
}
// -----------------------------------
(5) Setting the compilation option: Project-> Options Open the Project Options dialog box, clear the Use Dynamic RTL flag in the Linker property page, and clear the Build With Runtime Packages in the package content page. This can be run as long as a single DLL is run without having to install some dynamic connection running time libraries. Use animation DLL
The above compiled DLL can be called by any other development language, and the method of use in the PB is given below.
(1) Definition:
// Declare -> Global External Functions
Function Long ShowStatusWindow (Long
Avitype, String Wintitle, Long Hwnd &
Library "statwin.dll" alias for "showstatusWindow"
Function Long getcancelstatus (long closewindow) &
Library "Statwin.dll" Alias for "getStatus"
Function Long CloseStatusWindow () &
Library "statwin.dll" alias for "closestatuswindow"
(2) call:
Long ll_endtime
// Show Find Folder cartridges
ShowstatusWindow (2)
SetPointer (HOURGLASS!)
LL_ENDTIME = CPU () 10 * 1000
DO
IF getcancelstatus (0) = 1 THEN
exit
END IF
// Do things to do
Loop Until CPU ()> LL_ENDTIME
ClosestatusWindow ()
Making a screen saver with C Builder 3
The screen saver is a standard Windows executable that is SCR's extension. When the "Screen Sword" page of the Display Properties of the Control Panel, the module will automatically find extensions under the Windows Start Directory (Windows Directory and System Directory). The name is SCR's Windows-based executable. Using the screen saver, not only can extend the life of the display, but also protect private information.
The preparation screen saver not only involves the processing of messages, but also involves processing of command line parameters. The strict standards must be complied with the Win32-based screen saver in the Win32SDK documentation. Follow these standards, the screen saver must output two functions: ScreenSaverProc and ScreensaverConfigureDialog, how many of the screen savers in the Windows system do not follow these standards (view you can use using the ImpDef or TDUMP utility). And use the method described in this document to write a screen saver, not only to use the resource editor, but also use the SCRSAVER.LIB file (in the C Builder3 environment when link). Not only should be related to the processing of messages, but also related to the processing of command line parameters. C Builder3 is a quick application development tool that provides many types of application development templates, but does not provide development screen saver templates, and does not mention how to develop such applications in its online help. . After this study, the method of compiling screen savers with C Builder3 is found.
When setting up the "Screen Sword" page of the "Display Properties" item of the control panel, you should have three types of command line parameters, and the results of the screen saver in various situations are different. In general, three types of forms (or two are discussed in subsequent content). The following will specifically explain how to prepare a screen saver.
First, the selection of screen saver
If a protector is selected in the drop-down list box titled "Screen Sword", the system automatically starts the program, which is the screen range of the display graphics on this page, and will Two command line parameters: one is "/ p"; the other is the handle of the display window, passed to this selected program. Therefore, such programs first should be able to handle command line parameters. In C Builder3, the function related to the command line parameter is: paramcount () and paramstr (), the specific declaration method is as follows:
1. EXTERNPACKAGEINT__FASTCALLPARAMCOUNT (VOID);
This function returns the number of command line parameters, but does not include the application itself.
2. EXTERNPACKAGEANSISTRING__FASTCALLPARAMSTR (intIndex);
This function returns the command line parameters specifying the index value. Paramstr (0) returns the application itself.
Therefore, in this step, the statement determined in the steps is as follows:
IF (Uppercase (paramstr (1)) ==
"-p" || Uppercase (paramstr (i)) == "/ p")
{
// Addthecodeinhere
}
After completing the parameter judgment, the processing of the display window should be displayed to enable the program to display the program's parent window and the display area to the screen area of the display graph. This should involve the acquisition of the parent window handle and the setting of the parent window, and the call of the API function. The parent window handle in this environment is the second command line parameter passed; set the parent window, just set the form's ParentWindow property. This procedure is as follows:
Rectrc; // line1
HWNDHWND = (hwnd)
(atol (paramstr (2) .c_str ())); // line2
:: getClientRect (hwnd, & rc); // line3parentWindow = hwnd; // line4
Left = rc.Left; // line5
TOP = rc.top; // line6
Width = rc.right-rc.Left; // line7
HEIGHT = rc.bottom-rc.top; // line8
In the above program clip, the second line of statement is to convert the second parameters passed into a window handle; then, the third line of statement uses this window handle, call the API function to obtain the customer area of the window; The statement sets the parent window of the selected screen saver to the specified window; the remaining statement is the size of the client's window size to the secondary window. The location of this program clip should be in the onCreate event processing of the form.
It should be noted that the form pattern of this type (including the third step) should be:
Formstyle = fsstayontop;
The style of the form boundary should be:
BorderStyle = BSnone;
Of course, there is no need to graphically, so you can set the shape of the mouse to CRNONE:
Cursor = crnone;
Second, the setting of initialization parameters
When you click the "Settings" button in the "Screen Sword" page of the Display Properties module, the system will start the initial value setting dialog of the specified protector. At this time, the command line parameters passing are: "/ c" Or "-C" (parameter processing is the same as the previous introduction). Through this dialog, some initial parameters of the protection program can be set, such as the change in graphics. In this program, it is also related to the read and write of the initialization file or the registry to record the initialization parameters, which is easy to use when the protector is started.
Third, preview and operation
The preview is the display after the screen saver is activated. Click the "Preview" button in the "Screen Sword" page clicking the "Display Properties" page to observe the actual effect of the protector. At this time, the command line parameters passed when the system starts the program is: "/ s" or "-s". The processing of command line parameters is the same as the previous steps, but in this step, several messages are also processed, these messages are: WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN,
WM_KeyDown, WM_Activate. The formation form for WM_MOUSEMOVE and WM_ACTIVATE messages is as follows:
Void__fastcallhandlsomeMessage (TMESSAGE & MSG)
{
Switch (msg.msg)
{// ......
CaseWM_Activate: if (msg.wparamlo == wa_inactive)
CLOSE ();
Break;
CaseWM_MouseMove: IF (OldMousex == - 1 && OldMousey == - 1)
// intheconstructor, Oldmousexand
OldMouseyMustBeinitializedby-1.
{OldMousex = msg.lparamlo;
OldMousey = msg.lparamhi;
}
Elseif (OldMousex! = msg.lparamlo
|| OldMouse! = Msg.lparamhi)
CLOSE ();
Break;
......
}
}
For other messages, just call the close () function to close the application. When using such a message processing, the message mapping must be performed when defined, otherwise, it is necessary to perform processing in the corresponding message response (using a certain Boolean variable, you can use a form with the first step).
Similar to the first step, in this step, the shape of the specific mouse pointer is not required, so the mouse pointer is also set to CRNONE:
Cursor = crnone;
Fourth, modify the project source file
In C Builder3, a form is a class, in other words, classes with certain features are a form, so when preparing screen saver, do not need the main form, and also You don't have to create some forms, then modify the project source file, the program listed below is the project source file used when preparing a screen saver, for the reader's reference.
WinAPIWINMAIN (Hinstance, Hinstance, LPSTR, INT)
{
Createmutex (NULL, TRUE, "Screensaver");
IF (getLastError ()! = error_already_exists
{
Try
{
Application-> Initialize ();
Application-> Title = "Screen Saver Program Test";
IF (Uppercase (paramstr (1)) ==
"/ C" || Uppercase (paramstr (1)) == "- c"
|| Paramcount () == 0)
{TscrsaverConfiguerf * SCRCFG =
NEWTSCRSAVERCONFIGUERF (NULL);
Scrcfg-> showModal ();
DELETESCRCFG;
Return0;
} / / Click the "Settings" button
Elseif (Uppercase (paramstr (1)) ==
"/ P" || Uppercase (paramstr (1)) == "- p")
{Tscrforp * SCRFP = Newtscrforp (NULL);
SCRFP-> ShowModal ();
DELETESCRFP;
Return0;
} // Select a program in the "Screen Sword" drop-down list box
Elseif (Uppercase (paramstr (1)) ==
"/ S" || Uppercase (paramstr (1)) == "- s")
{Tscreensavef * screensave = newtscreensavef (null);
Screensave-> ShowModal ();
deleteScreensave;
Return0;
} / / Click the "Preview" button and run the screen saver
Else
Return1;
}
Catch (Exception & Exception)
{
Application-> Showexception (& Exception);
}
}
Return0;
} // thewinmainfunctionend
The method of preparing a screen saver under C Builder3 is introduced. For C Builder3 RAD tools, developing such programs is also quite convenient, according to the foregoing methods, can be developed at very short time screensaver program. For screen saver, it is not explained herein how to set the password problem, this part will be explored by the reader. TCP / IP head format
First, first is the common IP head format.
IP head format:
Version number (4 digits)
IP head length (4 digits)
Service type (8 digits)
Packet length (16 digits)
Identification section (16 digits)
Sign section (16)
Survival time (8 digits)
Transmission protocol (8 digits)
Head checksum (16 digits)
Send address (16 digits)
Target address (16 digits)
Option
filling
Briefly
============
1. IP header calculation The unit used in 32-bit words, commonly used to calculate the data start offset
2. Packet length is represented by byte, including the length of the head, so the maximum length is 65535 bytes.
3. The time indicates that the data is saved on the network before the data is lost, in seconds.
4. The algorithm for header checksum is the 16-bit and complement of all 16-bit words.
5. The length of the option is variable, the fill area changes with the option length to ensure the length of the entirety.
description
============
Struct iphdr {
Byte Versionihl;
Byte TOS;
Word Tot_Len;
Word ID;
Word Frag_off;
Byte TTL;
BYTE Protocol;
Word check;
DWORD SADDR;
DWORD DADDR;
/ * Put options here. * /
}
Second, TCP head format
TCP head format:
Source port (16 digits)
Destination port (16 digits)
Serial number (32-bit)
Confirmation number (32-bit)
Data offset (4 digits)
Retain (6 digits)
Sign (6 digits)
Window (16 digits)
Checksum (16 digits)
Emergency pointer (16 digits)
Option
filling
Briefly
============
1. Data offset is used to identify the start of data segment
2. The reservation section must be 0
3. Sign includes emergency sign, confirmation flag, set, reset marker, synchronization mark, etc.
4. The verification and calculation method is to add 16-bit binary inverse inverse and in 16-bit binary and in the middle.
5. The length of the option is variable, the fill area changes with the option length to ensure the length of the entirety.
6. For more detailed description, please refer to the relevant information.
description
============
struct tcphdr {
Word Sourport;
Word destport;
DWORD seqno;
DWORD ACKNO;
BYTE HLEN;
BYTE flag;
Word window;
Word chksum;
Word Urgptr;
/ * Put options here. * /
}
UDP
First, explanation
Using UDP, use the API instead of the control.
The first program (ReadBufferUdp) is used to receive the cache.
"Destino" variable is very important, if you receive data from other places to buffer, you must set Destino = 0 and assign your value when you execute you you will send the package (after the execution it will have the address which Send) You the packet.).
If you just want to receive data from a specified address, you must set the variable destino =
. "Gventerrar" is used to abort the process. (Gvencerrar is set to global variables.)Time setting. "INICIO 12" = 12 second.
The third program is used to prepare the Winsock program.
Second, the code
Int ReadBufferudp (unsigned long * destino, void * t, int size)
{
CHAR BUFFER [128];
SockAddr_in SockAddr;
Int lensockaddr = sizeof (sockaddr_in);
FD_SET FDREAD;
Struct TimeVal T_Val;
int R;
Time_t iNicio = Time (null);
Application-> ProcessMess ();
IF (Gvencerrar)
Return False;
FD_ZERO (& FDREAD);
FD_SET (GVSocket, & fdRead);
T_Val.tv_sec = 0;
T_Val.tv_usec = 0;
While ((Ret = SELECT (0, & fdread, null, null, & t_val))! = 1 && (INICIO 12)>
Time (NULL) &&! gvencerrar
{
FD_ZERO (& FDREAD);
FD_SET (GVSocket, & fdRead);
T_Val.tv_sec = 0;
T_Val.tv_usec = 0;
Application-> ProcessMess ();
}
IF (ret! = 1)
Return False;
IF (RECVFROM (GvSocket, Buffer, Size, 0, (LPSOCKADDR) & SockAddr, & LensockAddr)! = size)
Return False;
IF (* destino == 0)
{
* Destino = SockAddr.sin_addr.s_addr;
}
Else
IF (* Destino! = SockAddr.sin_addr.s_addr)
Return False;
Memcpy (t, buffer, size);
Return True;
}
Int WriteBufferudp (unsigned long desino, void * t, int size)
{
SockAddr_in SockAddr;
Int Sent;
Application-> ProcessMess ();
SockAddr.sin_Family = AF_INET;
SockAddr.sin_port = gvportudp;
SockAddr.sin_addr.s_addr = destino;
Sent = sendto (gvsocket, (char
*) T, Size, 0, (LPSOCKADDR) & SockAddr, Sizeof (SockAddr));
IF (Sent! = Size)
Return False;
Else
Return True;
}
Void INicialIzatcpip ()
{
Word wversionRequested;
Wsadata wsadata;
IN_ADDR in;
PServent PServent;
SockAddr_in sockaddrin;
WVersionRequested = MakeWord (1, 1); if (WsaStartup (WVersionRequested, & WSADATA)
{
ShowMessage ("Erro Na Inicializao DO TCP / IP");
Application-> Terminate ();
Return;
}
// Get The Port On Service File
IF ((pservent = getServByname ("Your_Service_name", "UDP")) == NULL)
{
ShowMessage ("Erro Obtendo Port Do Servi TransuRB / UDP");
Application-> Terminate ();
Return;
}
Gvportudp = pservent-> s_port;
Sprintf (Straux, "Servi Transurb / UDP Port:% D", NTOHS (GVPORTUDP));
LOG (Straux);
// Open de Socket
IF ((gvsocket = socket (AF_INET, SOCK_DGRAM, 0) == Invalid_socket
{
ShowMessage ("Erro Na Crtiao Do Socket");
Application-> Terminate ();
Return;
}
"" Socket Criado COM Sucesso ";
// do the bind
SockAddrin.sin_family = af_INET;
SockAddrin.sin_port = gvportudp;
SockAddrin.sin_addr.s_addr = null;
IF (Bind (gvsocket, (lpsockaddr) & sockaddrin, sizeof (sockaddrin) == Socket_ERROR)
{
ShowMessage ("Erro No Bind Do Socket");
Application-> Terminate ();
Return;
}
"Bind Do Socket COM Sucesso);
}
Judging the Desktop and other directories of Windows
Use the API function ShgetSpecialFolder. SHLOBJ.H has the prototype statement of Shgetspecialfolder. This function can help us find the Windows Desktop directory, start the directory, my document directory, etc.
SHGETSPECIALFOLDER requires three parameters. The first parameter is hWnd, which specifies the "Owner Window": The dialog or message box that may appear when calling this function. The second parameter is an integer ID that determines which directory is to look for the directory, and its value may be:
CSIDL_bitBucket Recycle Station
CSIDL_CONTROLS Control Panel
CSIDL_DESKTOP Windows Desktop Desktop
CSIDL_DESKTOPDIRECTORY Desktop directory
CSIDL_DRIVES my computer
CSIDL_FONTS font directory
CSIDL_NETHOOD online neighbor
CSIDL_NETWORK online neighbor Virtual Folder
CSIDL_PERSONAL my documentation
CSIDL_PRINTERS printer
CSIDL_PROGRAMS program group
CSIDL_RECENT Most recently opened document column
CSIDL_SENDTO "Send to" menu item
CSIDL_STARTMENU Task Task Start Menu Item CSIDL_STARTUP Startup Directory
CSIDL_TEMPLATES Temporary Document
The last parameter is the PIDL address. ShgetspecialFolderLocation Write the address to the PIDL.
The following code demonstrates how to use ShgetSpecialFolderLocation:
/ / -------------------------------------------------------------------------------------------- ----------------------
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
LPITEMIDLIST PIDL;
LPMalloc pshellmalloc;
Char Szdir [MAX_PATH];
IF (Succeeded (& pshellmalloc)))
{
IF (SUCCEEDED (SHGETSPECIALFOLDERLOC) (NULL,
CSIDL_DESKTOPDIRECTORY,
& PIDL))))
{
// If you have successfully returned True
IF (SHGETPATHFROMIDLIST (PIDL, SZDIR))
{
Label1-> CAPTION = SZDIR;
}
Pshellmalloc-> free (PIDL);
}
Pshellmalloc-> release ();
}
}
/ / -------------------------------------------------------------------------------------------- ----------------------
Note: Some directories are empty. Some specific directories do not have a corresponding directory on this file system.
Get the name and IP address of the local Internet machine
First, the following example uses the Winsock API to obtain the name and address of the local host
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
Hostent * p;
CHAR S [128];
Char * p2;
// Get the computer name
Gethostname (s, 128);
P = gethostbyname (s);
Memo1-> lines-> add (p-> h_name);
// Get the ipaddress
P2 = INET_NTOA (* (in_addr *) P-> h_addr);
Memo1-> Lines-> Add (p2);
}
Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)
{
Word wversionRequested;
Wsadata wsadata;
// Start Up Winsock
WVersionRequested = MakeWord (1, 1);
WSAStartup (WVersionRequested, & WSADATA);
}
Void __fastcall tform1 :: formdestroy (TOBJECT * Sender)
{
WSACLEANUP ();
}
Create a digital signature with C Builder
If you pass a data on the network, there is a unsafe factor, so that you can reach the destination for the data, and you can add a digital signature to the data. This allows the other party to check if the data you have passed is modified by others by verifying the signature.
First, the principle of program
The working principle of digital signatures is still relatively simple. It is based on the original data you provide, which produces a specific data signature, and the other party also generates a signature through the same process. If the data has been modified, then it is impossible. Get two models of exactly the same signature, so that data has been modified by others. Programmaker uses Windows's CAPI interface to achieve encryption, decryption, and digital signatures of data. Second, the program list
Let's take a look at its specific implementation process by following the statement of C Builder.
First create a digital signature, assume that its data comes from a file.
// Variable declaration:
HcryptProv HPROV;
// CSP handle
Hcrypthash hhash;
// Handle
Const int buffer = 4096;
// Buffer size constant
BYTE PBUFFER [Buffer];
/ / Save buffer of read file content
Byte psignature [256];
// Store the signature buffer
DWORD DSIGNATURELEN = 256;
// Signature length
TFileStream * sourcefile;
// A file stream
IF (! CryptacquiRecontext (& HPROV, NULL, NULL, Prov-RSA-FULL, 0))
/ / Connect the default CSP, accept its handle to put it in HPROV
{
// Error handling
}
IF (! Cryptcreatehash (HPROV, CALG-MD5, 0, 0, & Hhash)
// Create a hash object, get its handle to put it in Hhash
{
// Error handling
}
DO
{
Dreadlen = Sourcefile-> Read (PBuffer, Buffer);
IF (! CrypthashData (Hhash, PBuffer, Dreadlen, 0))
/ / Calculate the quota according to the content of the file
{
// Error handling
}
} While (! (DREADLEN IF (! Cryptsignhash (Hhash, At-Signature, Null, 0, Psignature, & Dsignaturelen) // Use the private key to digitally sign the hash value // Signature data is put in Psignature, plug in DSIGNATURELEN // Error handling } Check the file-based data signature. // Variable declaration: HcryptProv HPROV; // CSP handle Hcrypthash hhash; // Handle HcryptKey HpublicKey; // Handle of the public key Const int buffer = 4096; // Buffer size constant BYTE PBUFFER [Buffer]; / / Save buffer of read file content TFileStream * SourceFile; // A file stream Byte psignature [256]; // Signed buffer from the previous section DWORD DSIGNATURELEN; // The length of the signature obtained by the last paragraph IF (! CryptacquiRecontext (& HPROV, NULL, NULL, Prov-RSA-FULL, 0)) / / Connect the default CSP, accept its handle to put it in HPROV { // Error handling } IF (! CryptgetUserKey (HPROV, AT_SIGNATURE, & HPUBLICKEY); // Get a handle of the public key { // Error handling } IF (! CryptCreatehash (HPROV, CALG-MD5, 0, & Hhash)) // Create a hash object, get its handle to put it in hhash { // Error handling } DO { Dreadlen = Sourcefile-> Read (PBuffer, Buffer); IF (! CrypthashData (Hhash, PBuffer, Dreadlen, 0)) / / Calculate the quota according to the content of the file { // Error handling } } While (! (DREADLEN IF (! Cryptverify, DSignaturelen, HpublicKey, Null, 0)) { IF (GetLastError () == nte-bad-signature) showMessage ("file has been modified"); } Else { ShowMessage ("file is not modified"); } The above is a simple implementation of a digital signature, and the resulting signature data can be saved separately or saved separately. Method for switching with ENTER key control focus In the Windows environment, you can get a control to get a focus, you can use the mouse to click on the control on the control, or press the TAB button to move the focus to the control. This control focus switch is not convincing. As shown in the figure, users will hope with Enter keys, and the control focus is switched from Edit1 to Edit2. To realize this function, you need to help WinAPI functions SendMessage is completed. The method is: first set up the keypreview attribute of Form1 as true, then add the following code in the Form1 onkeyPress event. In this way, the user can move by pressing the ENTER, the key control focus is moved by definite Taborder order! Void __fastcall tform1 :: formkeypress (TOBJECT * Sender, Char & Key) { IF (Key == VK_RETURN) { SendMessage (this-> handle, wm_nextdlgctl, 0, 0); Key = 0; } } Blocking Windows message - - Orland C Builder API back door ---- Pillage ---- C Builder is worthy of Borland's excellent products, using it to develop Windows programs very fast, but you will also find some restrictions in programming, so you can't achieve your own ideas. For example, you can't modify the system menu of the form; for example, when using the tracking bar, you can't find the StartTrack and EndTrack events, and your procedure requires these two events. In Windows API programming, you will not have these troubles, just process the WM_SYSCOMMAND and WM_HSCROLL (or WM_VSCROLL) messages, the above functions can be implemented. The disadvantage of the Windows API is that it is very troublesome, too much time to consume on the details, but its function is the most powerful. The VCL of C Builder is functionally only a subset because the VCL is encapsulated on the API, and some unused functions are prepared. But the programmer's imagination is not encapsulated. They always have a big enthusiasm to achieve other ideas, modify the system menu and add StartTrack and NDTRACK events for the tracking bar. But the VCL does not have these functions, what should I do? ---- Fortunately, Borland did not block roads, but left a back door - allowing programmers to intercept and handle Windows messages, just like API programming. So, the method has ... ---- Method ---- Intercepting Windows Messages requires the following steps: ---- In the table single header file (such as Unit1.h) ---- 1. Create a message mapping table in the class declaration, and give the handling of a message to the custom message processing function. Begin_MESSAGE_MAP Message_handler (Windows Message Name, TMessage, Message Processing Function) Message_handler (...) END_MESSAGE_MAP (TFORM) ---- 2. Declaration Message Processing Functions in the Private Area of Class Declaration. PRIVATE: // user declarations VOID __FASTCALL message processing function name (tMessage & Message); In the form file (such as Unit1.cpp) ---- 3. Write the message handling function, here you implement the features you need. such as Void __fastcall mainform :: OnWMHScroll (TMESSAGE & Message) { ... // Add your own code here TFORM :: Dispatch (& Message); } ---- Explanation ---- 1. About TMESSAGE ---- TMESSAGE is a predefined structure of VCL, defined as follows: Struct tMessage { Unsigned int msg; // message INT wparam; // word parameter INT LPARAM; // long word parameters Int result; // message result } ---- 2. About TFORM :: Dispatch (& Message) ---- Custom message processing function is best added to TFORM :: Dispatch (& Message), this sentence is to let the message continue to pass. If this sentence is not, the message will be completely intercepted, and the VCL class may fail to achieve normal functions due to the message. ---- Example 1: Modify the system menu ---- There are some programs, the main window is small, the menu is not, if you want to join about or setup dialogs, the best way is to take the system menu. In Windows API programming, modify the system menu is the same as implementing other functions, not too easy, and it will not be too difficult. However, in C Builder, the table class (TFORM) does not provide any properties and methods of the system menu, implementing other features, and the modification system menu seems to be difficult to go on the sky. ---- Fortunately, Borland allows programmers to process Window messages, so the opportunity is coming! First, modify the system menu with the Window API function Assume that the form is MAINFORM, set the mainform :: oncreate () function: 1. Get the system menu handle with GetSystemMenu (Mainform-> Handle, False); 2. Modify the system menu with Appendmenu, DELETEMENU, and ModifyMenu function, assign the new ID number to a custom menu item. At this time, you can see that the system menu is also modified, but the custom menu item cannot be responded. Second, intercept the WM_SYSCOMMAND message to respond to custom menu items In the form of a single header (such as Unit1.h) 1. Add a message response table at the end of the form class, obtain the processing of the WM_SYSCOMMAND message. Begin_MESSAGE_MAP Message_handler (WM_SYSCOMMAND, TMESSAGE, ONWMSYSCOMMAND) END_MESSAGE_MAP (TFORM) 2. Add a message processing function declaration within the Private area defined by the form class. PRIVATE: // user declarations Void __fastcall onwmsysCommand (TMESSAGE & Message); In the form file (such as Unit1.h) 3. Write a message response function Void __fastcall tform1 :: onwmsyscommand (tMessage & Message) { IF (message.wparam == id_sysmenu_myitem) { // Your Code Here, Do Something } TFORM :: Dispatch (& Message); } Third, the full program example Example 2: Add OnStartTrack and OneendTrack events to the tracking bar When the tracking column is used for progress control, OnStartTrack and OneendTrack are likely to be the event you need. For example, when controlling multimedia play progress, when the user moves the slider, you will need the onStartTrack event to stop playback, you need the OneendTrack event to locate the new play position. But Borland did not provide these two events, and I have such programming enthusiasts to self-reliance, and the idea of intercepting Windows messages. First, intercept the WM_HSCROLL message, add an onStartTrack and OneendTrack event to the trackball In the table single header file (such as Unit.h) 1. Add a message response table at the end of the form class definition to hand over the WM_HSCROLL message processing to the ONWMHScroll function. Begin_MESSAGE_MAP Message_Handler (WM_HSCROLL, TMESSAGE, ONWMHSCROLL) END_MESSAGE_MAP (TFORM) 2. Add the ONWMHScroll function declaration within the Private area defined by the form class. PRIVATE: // user declarations Void __fastcall onWmhscroll (TMESSAGE & Message); 3. Join the StartTrack and EndTrack function declaration within the Private area defined by the form class. PRIVATE: // user declarations Void __fastcall trackbar1starttrack (TOBJECT * Sender); Void __fastcall trackbar1ndtrack (TOBJECT * Sender); In the form file (such as Unit.cpp) 4. Write the OnWMHScroll function, so that it can call the StartTrack and EndTrack function according to the message parameter, generate an onStartTrack and OneendTrack event in the actual sense. 5. Write the StartTrack and EndTrack function. If it is a vertical tracking bar, change the above WM_HSCROLL to WM_VScroll. Second, the full program example end Borland C Builder programming, intercepting Windows messages is an advanced programming technology that allows you to try to explore Windows's potential, especially let programmers who have programmed with API have been comfortable. Intercepting the Windows message is the stage where the API is funny. When VCL can't do anything for you, please think of the bottom API. Use COMMATEXT Sometimes a convenient way to store a StringList, which is only a simple line. For example, when you want to use an INI file, how to write a line to an Ini file, you can complete this work with COMMATEXT. Here is an example, the function is to create a Blah.ini file and write a value in the following form: [My section] Memo1 = (you are entered in Memo1) 1. There are two buttons on Form1 btnload and btnsave and a MEMO1 2. Also join: #include 3. Define variables: Const string inIfile = "Blah.ini", inisection = "my section", inivalue = "MEMO1"; 4. Save button code: Void __fastcall tform1 :: btnsaveclick (TOBJECT * SENDER) { Tinifile * ini = new inIfile (Application-> Exename) inIfile; Ini-> Writestring (Inisection, Inivalue, Memo1-> Lines-> COMMATEXT); Delete ini; } 5. Load button code: Void __fastcall tform1 :: btnloadclick (TOBJECT * SENDER) { TiniFile * ini = new tinifile (applfilepath (application-> exename) inifile); Memo1-> Lines-> COMMATEXT = INI-> ReadString (Inise, Inivalue, ""); Delete ini; } 6. The following code supports the loading and sorting the content, and the actual storage is unchanged: Void __fastcall tform1 :: btnsortloadclick (TOBJECT * SENDER) { Tstringlist * sl = new tstringlist; Tinifile * ini = new tinifile (Application-> Exename) inifile); SL-> Commatext = Ini-> ReadString (Inisection, Inivalue, "); SL-> sort (); Memo1-> lines = SL; Delete ini; Delete SL; } Show information box when the program starts First, the software enters the main window, first display a message, tell the user some information about the software, such as software name, version number, etc. This information frame automatically disappears after 1 to 2 seconds. 1. Establish a New Application, then the system automatically generates a FORM1, which is the main form. 2.File-> New Form establishes a new form2, this as a message box. 3. Add a component TTIMER (on the System control bar on the Form2 to set the display time of the information box. 4. Ttimer's event ontimer Add: Form2-> Close (); 5. Add: in the WinMain () function: Application-> CREATEFORM (__ classid (tform2), & form2); FORM2-> ShowModal (); // This sentence must be added Application-> Run (); And to put the header file unit2.h of the FORM2 to the Project1.cpp in WinMain (). 6. Operating the program, first display FORM2, the display time is determined by the TTIMER's interval attribute, 1000 is one second. Second, the implementation of the software cover The flow of the current device is to make a picture before the initialization is initialized, and it is often the 1/4 screen size, which shows the name of the software, the author, version of this information. It is very simple to use C Builder to realize this: 1 Customize a window TSPLASHFORM, set it into a "transmissive window", that is, all the options under Bordericons are set to false, borderstyle = bsnone, FormStyle = fsstayontop, position = poscreencenter; 2 Place a TPANEL in the TSPLASHFORM window (in the figure in the figure); 3 Place a TIMAGE control on the TPANEL to transfer the desired graphics; 4 Take a slightly modified WinMain function, add the code as shown below. What you need to point out is that this code is passed through the function FindWindow, and whether there is a window title "DEMO" should be stored in the memory. If you save, you will run out. This function can prevent the re-run operation. This setting is required in some fields. WinApi Winmain (Hinstance, Hinstance, LPSTR, INT) { Try { IF (FindWindow (NULL, "DEMO")! = 0) { Application-> MessageBox ("The program has been run!", "Police Note ", MB_ICONSTOP); Return 0; } Tsplashform * splash = new tsplashform (application); Splash-> show (); Splash-> Update (); Application-> Initialize (); Application-> CREATEFORM (__ classid (tform1), & form1); Splash-> close (); Delete Splash; Application-> Run (); } Catch (Exception & Exception) { Application-> Showexception (& Exception); } Return 0; } How to get the command line parameter of the program? You can solve this problem with both different techniques below. Tips 1: First, perhaps the easiest way is to call the VCL Parastr () function. You can use the paramcount () function to determine how many command line parameters have passed to the application. Paramstr requires an integer parameter and returns an Ansistring object. If the parameter is 0, Paramstr will return the full name path of the executable. If the parameter is 1, the program name and the first command line parameter will be returned. If the parameter is 2, the second parameter will be returned, and so on. As a practice, open a new project, put 5 Label on the main window, add the following code to the window constructor: Label1-> CAPTION = paramstr (0); Label2-> CAPTION = paramstr (1); Label3-> CAPTION = paramstr (2); Label4-> CAPTION = paramstr (3); Label5-> CAPTION = paramstr (4); Run the program again. It is generally possible to see a similar string: E: /cbuilder/projects/project1.exe If you have not passed the parameters to the program, then Label2 to Label5 is an empty string. Close the program, select Run | Parameters from the C Builder menu. Enter a few parameters (-debug -testing -param) run the program again. You will see: E: /cbuilder/projects/project1.exe -debug -Testing -Param Tip: Paramstr's intelligence judgment on the space in the directory. To confirm this, copy the generated EXE file to the Program Files directory and then run it, you will see Paramstr (0) to return the full path and contain spaces. Tips 2: The second method is to call the getcommandline API function. Getcommandline does not require parameters, and returns a C-style char *, which contains all of the command line parameters. You will have to solve the string to get the relevant parameters. Label5-> caption = ansistring (getcommandline ()); Label5 will be: "E: /cbuilder/projects/project1.exe" -debug -testing -param How to monitor clipboard Plus in Form1.h PRIVATE: Void __fastcall clipboardChanged (TMESSAGE & MSG); PUBLIC in Form1. H: Begin_MESSAGE_MAP Message_handler (WM_DRAWCLIPBOARD, TMESSAGE, CLIPBOARDCHANGED) END_MESSAGE_MAP (TFORM) Add: CPP in Form1: Void __fastcall tform1 :: clipboardchanged (tMessage & msg) { Point mousepos; GetCursorpos (& MousePOS); PopupMenu4-> popupComponent = Form1; PopupMenu4-> popup (mousepos.x, mousepos.y); // A change, you can pop up a menu, copy, cut, or clear this function. } There is a ToolButton in Form1. CPP Void __fastcall tform1 :: Toolbutton9Click (TOBJECT * SENDER) { Static hwnd lasthandle; Static Bool Clip = False; IF (Clip == True) { Toolbutton9-> down = false; Changeclipboardchain (Form1-> Handle, LastHandle); // End monitoring } Else { Toolbutton9-> down = true; CLIPBOARD () -> CLEAR (); Application-> minimize (); LastHandle = setClipboardViewer (Form1-> Handle); // Start monitoring } CLIP =! clip; } How to use onIdle events Use the OnIdle event to monitor the clipboard content at any time to change the executables of the pop-up menu. Plus in Form1.h PRIVATE: Void __fastcall onder (TOBJECT * Sender, Bool & Done); Add: CPP in Form1: Void __fastcall tform1 :: onidle (TOBJECT * Sender, Bool & Done) { Bool textSelected = DBRICHEDIT1-> SELLENGTH> 0; N17-> enabled = textselected; // cut, copy, clear N18-> enabled = TextSelected; N20-> enabled = textselected; BOOL CBHASTEXT = Clipboard () -> HASFORMAT (CF_Text); // Need to join #include N19-> enabled = cbhastext; // Paste Bool Hastext = richedit1-> lines-> count> 0; N21-> enabled = Hastext; // Bool Haschanged = richedit1-> modified; Toolbutton2-> enabled = Haschanged; Toolbutton4-> enabled = Haschanged; } Plus in the oncreate of Form1: Application-> onder = onIDLE; Write a serial asynchronous communication program under WIN 95 with C Builder 4.0 · Basic method for serial port manipulation · Under Win32, the operation of the serial port is turned on or off like the file, and reading and writing to serial data can be performed in the user-defined read and write buffer. The specific function is: first use createFile () to open the communication serial port, where the parameter lpFileName points to the serial port logic name, such as "COM1" or "COM2", etc., the parameter dwdesiredAccess defines the read and write permission of the file, generally set to generic-read | generic -Write; parameter DWSHAREMODE defines the resource sharing method, which must be set to 0, for exclusive ways; LPSecurityAttributes define security properties, Win 95 is null; dwcreationDistribution definition file creation method; dwflagsandattributes define file properties and tags, should be set to file- Flag-overlapped indicates asynchronous communication methods; hTEMPLATEFILE points to a handle of a template file and is NULL under Windows 95. The serial communication parameters (such as baud rate, stop bit, data bit, check digits, etc.) are then set with the buildcommdcb () and setcommmmdcb () and setcommmmdcb () functions, such as baud rate, stop bit, data bit, check digits, etc. in which characters in buildcommdcb () are then The string parameter LPDEF defines the parameter format of MODE in the DOS command. About DCB more specific settings require specific definitions of data streams, handshake signals, and communication control requirements, see Windows Technical Information. Use getcommState () to get the current DCB parameter value. If you need to reset the timeout parameters of read-write by setcommtimeouts () and getcommtomeouts (); the settings of the read and write buffer use setupcomm (), parameters dwinqueue, and doutqueue are defined as the size of the input and output buffers, respectively. After the serial port is initialized, it is necessary to establish an event object related to communication. Generally use the createEvent () function, it returns an event handle, where the parameter lpeventattribute points points to the security attribute structure address, which is null in Win 95 (unsafe attribute); Boolean BmanualReset defines event reset, true represents manual reset, False Represents the auto reset (the correlation function is setEvent () and resetEvent ()); parameter binitialState defines the initial state of the event, TRUE represents the signal, otherwise it is not signal; LPNAME is the event name set for multi-process, for single-process definition as NULL. Then use setcommmask () to define the user program to monitor the communication event category. After the above setting is completed, the user program can wait for the generation of the communication event, generally call the function waitcommEvent () to monitor the communication event, where the parameter lpevtmask points to the mask address generated, and the nature of the event is determined, and LPoverlapped points to the overlapping structure address. Simply defined as NULL. For the response to serial port events, there are four ways: query, synchronous I / O, asynchronous I / O and event driver I / O, need to be determined according to different control requirements of users. The query mode takes up longer computer time, synchronized I / O mode to return until the specified byte number or timeout is read, it is easy to cause thread blocking, asynchronous I / O is used for background processing, the event driver is notified by the system The event of the program is incident and serial port operation. Comparison of events drive I / O methods are flexible. When there is a communication event, the function readfile () and writefile () can be used directly to read and write the serial buffer. Where lpbuffer points to the read and write buffer, nNumberofbytes is the number of bytes to read and write, lpnumberofbytes are actually read and written bytes, and LPoverLapped specifies synchronous or asynchronous operations. After the communication is over, the call function closeHandle () is turned off. · Application example With the above API function, the author gives an instance of the simplified serial port initialization. 1 is an interface example of a serial communication basic parameter setting generated using a C Builder component. Handle hcom; // Define the handle DCB DCB; Overlapped e; / / Define overlapping structure Void -fastcall TFORM1 :: Okbtnclick (TobjectSender) {HCOM = CREATEFILE ("COM2", Generic-Read | Generic-Write, 0, Null, Open-EXISTING, File-Attribute-Normal | file-flag-overlapped, null; // Open the communication port Buildcommdcb ("9600, O, 8, 1", & DCB); / / The first string parameter is actually used by Figure 1, after the combination of Figure 1, only simply describes its format Setcommstate (HCOM, & DCB); SetupComm (hcom, 512, 512); // Setting the read / write buffer E.hevent = CreateEvent (NULL, FALSE, FALSE, NULL); // Setting Event Setcommmask (hcom, ev-rxchar | ev-txempty); // Set an event mask Okbtn-> enabled = false; Message processing techniques for C Builder non-visual components A non-visual component must respond to a Windows operating system or user-defined message. However, since a non-visual component has no window, it does not have a window handle, naturally it does not receive the message, in order to solve this problem, our idea is to create a hidden window to make non-visual components to receive news. To create a hidden window for your non-visual components, you need to have the following: 1. A private variable type (private variable type) hWnd to obtain a window handle. 2. A function (A WndProc) used to capture the window sent to the component. 3. Create a window handle and set WNDPROC for the call to AllColatehWnd. In order to clearly explain the above ideas and display creation, the following we will explain with a specific example. First we first create a new component, in C Builder, select File | New ... Double-click Component icon to display a new component dialog to change Ancestor Type to tComponent and Class Name TTEST and is set. Then, switch to the header file of the new component, add the following declarations in the private part of the class: HWND FHANDLE; Void-fastcall WndProc (TMESSAGE & MSG); The first line declares an HWND variable that calls FHANDLE, which will be used to capture the window handle after the window is created. The second line declares a WndProc function for receiving messages. The statement of this function must be identified in order to define it is a WndProc, then construct the following statement in the class declaration public (public) section: VIOD DOIT (); This public function will be used by us to test components, and class declarations should be as follows: Class Package TTEST: PUBLIC Tcomponent { Private: HWND FHANDLE; Void-fastcall WndProc (TMESSAGE & MSG); protected: PUBLIC: -fastcall ttest (TComponent * Owner); Void Doot (); -published: } Now switch to the component's code unit, add the following line to the top of the unit (maybe a good place on the function) #Define my-message.wm_user 1 This line declares that when the DOIT function is called, the component will send it to its own user-defined message. At this point we have to assign a window handle for the component. This handle will provide a hidden window that allows us to capture messages in the component. Find the component constructor, add the following code: -fastcall test :: test (Tcomponent * Owner) : Tcomponent (Owner) { Fhandle = allocatehwnd (WndProc); } Ok, the important step has been completed, the allocatehwnd function creates a hidden window and returns its handle, note that we will send Windows where to send a message, pass the address of WndProc; Now let's create a part of the WndProc. Add: Void-fastcall ttest :: WndProc (TMessage & msg) { IF (msg.msg == my_MESSAGE) MessageBox (0, "Got Here!", "Message", 0); Try { Dispatch (& MSG); } Catch (...) { Application-> HandleException (this); } } Whenever Windows sends a message to the component, Windows calls this function whenever Windows. This part of the code has completed two things. First, it checks if the received message is our user-defined message. If so, a message box will be displayed, you can see the message we have received. Secondly, this code transmits messages during the system (or VCL) process, and the TRY / CATCH block is used to ensure that if an abnormality occurs, it will become a handle under the default style. In summary, the WndProc function is sent to all other messages for the default handle to monitor all customer messages. Now we create a DOIT function, complete our components, join us to create a DOIT function, complete our components, join the code: Void TTEST :: DOIT () { Postmessage (Fhaandle, MY-Message, 0, 0); } This function sends a window handle of a message component (remember that this window handle is previously stored in the FHANDLE data finished product). Now we have completed the creation of component selection, with SELECTFILE | ColSeall to save our work test components. The next step will test the component. If you use BCB3, you must join the component into "PackGE), then use Componet | Install (you can use DCLSTD35 Packe to quickly test). Select the testbb.cpp you just exist. Once you have installed the completion of the component, it will appear on the component board. Double-click the button, create the following code for the button's onclick event: TEST1-> DOIT (); now run the program, when you click the button, you will see a message box to display "Got Here". Listinga and B include header files and source code below. Summary: A non-visual component that can respond to Windows messages has many purposes. The most obvious is to encapsulate some aspects of WindowsAPI. For example: TAPI and WINSOCK send messages to the specified user of the event. If you write the components encapsulated a such API. You will need to capture messages sent by Windows. Join the hidden window in your component will help you do this. The above programs are debugged in C Builder 3.0. Establish Database VCL Use experience with C Builder With the wide application of the database, database programming has become a rapid development of programming. The powerful feature of C Builder in database development is unparalleled, and you can even generate a beautiful database program without writing a program. Let's take a look at the use skills of several database VCLs in C Builder: First, DBGRID control 1. Set the field display width attribute of DBGRID In order to create a smaller column in DBGRID, you must establish a display title, which is equal to or less than field values. For example, you want to build a column with only three characters wide, your column header shows that there must be only three characters or less. 2. Change the display field and date display format of DBGRID (1) Double-click the table1 corresponding to the DBGRID to enter the field editor. (2) Right-click "Add Fields ...", the Add Fields ... ", the Add Field dialog box, select the field you want to add (this field will be displayed by DBGRID when running) and point OK button. (3) Assumption Add the "Date" field, click this field, in the property table: DISPLAYLABEL Fill in the field name you want DBGRID display. If the original field name is in English, here DBGRID will display the Chinese name with the Chinese name. Fill in DISPLAYFORMAT: YYYY-MM-DD, will be displayed in 1999-05-28 format later. Second, TQUERY control The TQuery control is a very important control in database programming. It is responsible for establishing contacts with the database through BDE and the SQL statement is conveniently established. Query must establish the corresponding SQL to take effect. The parameter setting of TQuery is as follows: (1) In the SQL attribute: SELECT * FROM table name Where field name =: variable name Follow the ":" is the variable. After writing, the data type of the variable can be modified in the parameter properties. (2) Assignment of variables: Query1-> Active = false; Query1-> params-> items [0] -> asstring = edit1-> text; Query1-> Active = true; / / Find records that match the variable (3) Display the result with DBGRID DBGRID's DataSource is connected to DataSource1, while DataSource1's DataSet is connected to TQuery1. Third, the application example The query established by the QUERY control is easier and more efficient than Table. Use a simple code to explain how to establish a query program: For example, to create a program name book1 booking book 1 book 1, add DBGRID, DataSource, Query on the form, add the following code: DBGRID1-> DataSource = Datasource1; DataSource1-> dataset = tqery1; query1-> close (); Query1-> SQL-> CLEAR (); Query1-> SQL-> Add ("SELECT * FROM table where (book name = 'BOOK1'"); Query1-> EXECSQL (); Query-> Active = true; You can see all names BOK1 records in the generated table. Create an Internet-based point-to-point chat with C Builder ---- Create an Internet-based application, you might think of complex Winsock programming. However, C Builder3 provides new WebBroker's Internet kits, where the TclientSocket and TServersocket components encapsulate the API of Windows, which greatly simplifies Winsock programming. To transfer data over Internet, at least a pair of Sockets, a socket on the client, another socket on the server. In fact, TClientSocket, TSERVERSOCKET components are not Socket objects, and its properties socket will return their respective socket objects. TclientSocket is used to handle the socket connection between the client to the server side. TSERVERSOCKET is used to process the socket connection from the client. Once the client and the server are connected to the socket, the client, and the server can communicate with each other. . ---- Establish a new project to create an application user interface: ---- 1. Switch the component page to the Internet page, put a TSERVERSOCKET component and a TclientSocket component to the form, so that the application can be both TCP / IP servers or TCP / IP customers. Set the port attribute to the same value (such as 1000), determine the connection type between the socket is Nonblocking. ---- 2. Put two TMEMO components to the form to display both parties' conversation, set the memo2's ReadOnly property to True. ---- 3. Put a panel component at the top of the form, putting three buttons on it: Listening (BTNConnect), disconnected (btndisconnect), is used to initiate the corresponding operation. ---- 4. Place a STATUSBAR component at the bottom of the form, set its SimplePanel property to true, change the status strip information in the corresponding event handler, so that the user knows the connection status at any time. ---- Open the header file, add two private members to the private member: BOOL ISSERVER; STRING Server. When both parties are in communication, ISSERVER is used to determine which CHAT program is in the server side, Server is used to store the host name of the server. The constructor that establishes the form class is as follows: __fastcall tform1 :: tform1 (tComponent * Owner) : TFORM (OWNER) { ISSERVER = FALSE; Server = "LocalHost"; } ---- SERVER is set to localhost by default, so that the program can debug on a single machine that is not connected to the Internet. In the Windows subdirectory you can find the hosts.sam file, you have defined the host name: localhost in this file: Localhost. Void __fastcall tform1 :: formcreate (TOBJECT * Sender) { BTNDISCONNECT-> Enabled = false; } ---- After the program runs, if the user presses the "listening" button, set the program to the server side, then the TSERVERSOCKET's Active property should be set to TRUE, so that the server automatically enters the listening state. Void __fastcall tform1 :: btnlistenclick (TOBJECT * SENDER) { Clientsocket1-> Active = False; Serversocket1-> Active = true; Statusbar1-> SimpleText = "Listening ..."; btnlisten-> enabled = false; BtnConnect-> enabled = false; } ---- When the user presses the "Connection" button, the program pops up a query box, requires the user to enter the host name of the server to connect, and then establish a connection. Void __fastcall tform1 :: btnConnectClick (Tobject * Sender) { IF ("Connect to Server", "Enter Server Address:", Server) { IF (Server.Length ()> 0) { Clientsocket1-> Host = Server; Clientsocket1-> Active = true; btnlisten-> enabled = false; BtnConnect-> enabled = false; BTNDISCONNECT-> enabled = true; } } } ---- After the user proposes the connection request, the client triggers an oncreate event, the program first displays the connection information in the status bar, and then the Memo2 empties to the other party's conversation content is ready to start talking. Void __fastcall tform1 ::clientsocket1connect (TOBJECT * Sender, Tcustomwinsocket * Socket) { STATUSBAR1-> SimpleText = "Connect to:" Server; Memo2-> lines-> clear (); } ---- The onaccept event is triggered after the server accepted the customer's request, and set the variable ISSERVER of the flag server side in this event handler to True and prepare to start talking. Void __fastcall tform1 :: serversocket1accept ( TOBJECT * SENDER, Tcustomwinsocket * Socket) { Memo2-> lines-> clear (); ISSERVER = TRUE; STATUSBAR1-> SimpleText = "Connect to:" Socket-> RemoteAddress; } ---- After establishing the connection, the two sides can enter the conversation content in MEMO1 to start talking. After pressing the Enter key, send the text in the line. The Connections property of the server-side Socket returns an array that consists of a server currently active. Void __fastcall tform1 :: memo1keydown TOBJECT * Sender, Word & Key, TshiftState Shift) { IF (Key == VK_RETURN) { IF (ISServer) ServerSocket1-> Socket-> Connections [0] -> Sendtext Memo1-> lines-> strings [Memo1-> Lines-> count-1]); Else Clientsocket1-> Socket-> Sendtext Memo1-> lines-> strings [Memo1-> Lines-> count-1]); } } ---- In this example we use non-blocking transmission mode, when one of them is written, the other will trigger an OnRead Event (Client) or OnClientRead Event (Server End & ---- In this example we use non-blocking transmission mode, when one of them is written, the other will trigger an OnRead event (client) or onclientRead event (server side), the handler of these two events is just Add the received content to the back of MEMO2. Memo2-> lines-> add (socket-> receivetext ()); ---- If you click the "Dispenser" after the user is established, disconnect the client and server connection, the server side will trigger an OnClientDisconnect event. And the client triggers an onDisconnect event. At this time, the server side should return to the listening state, waiting for the user's connection; and the client will return to the status before the connection, wait for the user to establish a connection again, if there is more than one server, Select Connect to another server. void __fastcall TForm1 :: btndisconnectClick (TObject * Sender) {ClientSocket1-> Close ();} void __fastcall TForm1 :: ServerSocket1ClientDisconnect (TObject * Sender, TCustomWinSocket * Socket) {StatusBar1-> SimpleText = "listening ...";} void __fastcall TForm1 :: ClientSocket1Disconnect (TObject * Sender, TCustomWinSocket * Socket) {btnlisten-> Enabled = true; btnconnect-> Enabled = true; btndisconnect-> Enabled = false; StatusBar1-> SimpleText = "";} ---- In addition, the client should also increase the error capture mechanism, which can feedback information in time when the user enters invalid server name or server terminal is not in the listening state. void __fastcall TForm1 :: ClientSocke t1Error (TObject * Sender, TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode) {StatusBar1-> SimpleText = "Could not connect to:" Socket-> RemoteHost; ErrorCode = 0;} with C Builder Get the application icon now, there is a lot of shared software or free software on the Internet, and there are many very easy to use, and it is convenient. But after all, it is someone else, which is always used, and if you like to program, why not do it yourself! To do it, you will do it, and there is a visualized cutlery - C Builder 4.0, you want to be very simple. First start C Builder, create a new project, place a Button control on the form, an image control, and an OpenDialog control, and their names do not have to be changed. Double-click the button control, write the following code: if (OpenDialog1-> execute ()) {filename = Opendialog1-> filename; hicen hicon; // total = (int) extracticon (form1-> handle, filename.c_str (), -1 ); Icon = new ticon (); hicon = extracton (form1-> handle, filename.c_str (), 0); icon-> handle = hic; icon-> savetofile; image1-> picture-> loadingfromfile TempFile);} where: FileName, TempFile, Icon defines: Ansistring TempFile, FileName; TICON * ICON; This, the first icon of your selected program is displayed in the Image control. The Windows API Extractic is used to get the chart, so it can only get the icon of the executable, if you want to get an icon of any file, you can call the Windows API's SHGETFILEINFO function to complete the task that ShgetfileInfo can complete There are a lot, and the specific usage can be found in the Win32 help file. Because there is too much quantity, it is different from the ASCII code representation with the ASCII code. By calculating these two bytes, we can get the location of the Chinese characters in the Chinese word library. Read several bytes of this location to get dot matrix information indicating this Chinese character. With this information, you can display the Chinese characters in DOS or Windows, respectively. In fact, saved in a text file is two byte encodings corresponding to each Chinese character, and the display problem is automatically resolved by the Chinese operating system. Chinese character encoding is not uniform, we use the GB code, and the Taiwan area is BIG5 code. The BIG5 code file is saved is the corresponding BIG5 encoding of Chinese characters. The GB code file is saved in the GB code file (this is the origin of "garbled phenomena"). Therefore, the key to the conversion work is that there is a code table file that records each BIG5 encoding corresponding GB encoding. The first step to make the code table file BIG5 code encoding rule is this: Each Chinese character consists of two bytes, and the first byte ranges from 0x81-0xfe, a total of 126 species. The range of the second byte is 0x40-0x7e, 0xa1-0xfe, a total of 157 species. That is, 126 * 157 = 19782 Chinese characters can be defined using these two bytes. Part of these Chinese characters is commonly used, such as one, Dan, which is called common words, and its BIG5 code is 0xA440-0XC671, a total of 5401. More unused words, such as abuse, adjust, we call the common words, range from 0xc940-0xf9fe, a total of 7652, and the rest is some special characters. The principle of making code table files is this: first write all BIG5 encodes into a file, then use the software with BIG5 code to GB code conversion function, such as earth village, Oriental express, four-way persons, convert files to GB code file, you get the code table file. The following source program writes all possible BIG5 encoding (0xA100-0XFEFF) into the file "Table.txt". // Turbo C 3.0 #include There are a lot of articles about the taskbar icon programming, but how can we prepare a moving icon? It is more convenient to implement in C Builder. ---- The basic programming idea is: Send a specific message in the specified time interval to make the taskbar icon to form an animation effect by setting the Timer Clock Control to send a specific message within a specified time interval. The implementation method is to load several image controls in the application's form, allowing them to load the corresponding drawings, and several graphs are continuously displayed in sequence, they form an animation. ---- Here, we use a door switch animation to do an example, put a Timer control on the form, two image, load "open" and "closing" two pictures. Start join the code. ---- Application must notify the task bar to add, delete, and modify the icon with the Means to send messages. Send messages must call Shell_Notifyicon. Its original shape is: Winshellapi Boll WinAPI shell_notifyicon; the first parameter dWMessage is a flag that sends a message, you can choose the NIM_ADD / / Task Bar Notification area Add Icon NIM_DELETE // to the taskbar notification area to delete icons NIM_MODIFY // modify notification taskbar notification area icon message preparation function TrayMessage bool __fastcall TForm1 :: TrayMessage (DWORD dwMessage) {NOTIFYICONDATA tnd; PSTR pszTip; pszTip = tipText (); tnd.cbSize = sizeof (NOTIFYICONDATA); // structure Size Tnd.ucallbackMessage = mywm_notify; // Custom callback message, declare Tnd.hwnd = Handle; // accept the window handle of the callback message in the header file, TND.UID = IDC_MYICON; // icon flag number tnd.uflags = nif_message | NIF_ICON | NIF_TIP; // Specify which three parameters which contain valid data IF (dwMessage == nim_modify) {Tnd.hicon = (Hicon) iconhandle (); // acquire the icon handle if (psztip) LSTRCPYN (tnd.sztip, Psztip, sizeof (tnd.sztip); else tnd.sztip [0] = '/ 0';} else {tnd.hicon = NULL; tnd.sztip [0] = '/ 0';} return (shell_notifyicon (dwmessage , & TND);} The function of the acquired icon handle hicon __fastcall tform1 :: iconhandle (void) {if (n == 1) {return (image1-> picture-> icon-> handle); // n is global variable 1 To display image1, 0 is image2} else {return (image2-> picture-> icon-> handle);}} Conversion function void __fastcall tform1 :: ToggleState (void) {if (n == 1) // n is an icon handle lock, is a global variable, 1 is display image1, 0 is image2 {n = n-1;} else {n = N 1;} TRAYMESSAGE (NIM_MODIDIFY); // Send Icon Transform Message} to prepare code to the Timer control, set its INTERVAL attribute to 1000, that is, the timer responds every second. Type code for the ONTIMER event: void __fastcall tform1 :: Timer1Timer (TOBJECT * Sender) {ToggleState ();} ---- Due to the limited space, only the basic part of the code, the implementation of other functions, such as closing the program, Open the window, etc., relatively simple, not described. When the program is running, you will see a movie icon on the top right corner taskbar on the screen. Is it very interesting, let's make a favorite. TFORM 1, let the window always set to the fsstayontop value at the forefront FormStyle property. Second, the dynamic calling window Form In the default, the window generated by the File / New Form is added to the window in the document with "Auto Create" features. That is, only the order is running, the window is stored in memory, and it is not to be called when it is. A window with this characteristic is generally suitable for the condition of the window property than being fixed, and is often called. The advantage is that the speed is fast, the shortcomings are occupied memory. In the actual sequence design, there will be a large amount of a pair of dialogue functionality, which are used to display the status or input information, and it is only necessary to transfer it in the program, and it is completed, there is no need for a resident memory. At this point, you can move the corresponding form in the "Auto - Create Forms" by selecting Project / Options / Forms, such as Form1, with the ">" box, and call the window in the program. In the body, add the following statement: tform1 * myform = new tform1 (this); myform-> showModal (); delete myform; form form1 is only transferred in memory when you need to call, after the call is complete, ie use Delete to clear RAM. This can reduce the occupation of memory resources. Third, the method of traversing the form control to access or modify the control on the form, the method is simple, tedit as an example: edit1-> text = "; edit2-> text =" "; but if there is ten on the form To come like Edit1, you need to perform the same initialization, use the above method one by one, don't trouble! There must be a method of holding a centralized parallel control. Before you introduce the method, let me first solve the component Form's Components and Controls attributes. See Table 1. Table One Property Type Description ComponentCount Int currently Total number of controls on FORM Components Tcompont * Current Form Points to all controls to all controls Point to all controls ControlCount int CONTROLS on a sub-area on a sub-area on the Form TControl * Currently, the array of all controls on a sub-area is illustrated as an example (figure), and the ComponentCount = 6 of Form1, while PANEL1 CONTROLCOUNT = 4., Where: array object Components [0] Panel1 Components [1] label1 components [2] Edit1 Components [3] label2 Components [4] Edit2 Components [5] Button1 array object Controls [0] label1 controls [1] Edit1 Controls [2] label2 controls [3] Edit2 below this code Complete the initialization of all TEDIT controls on Panel1. The reader can be changed slightly, you can travel throughout the calendar. There is a small skill in this, and we put the controls that need to be initialized in a panel1, which is easy to score with the control area that does not require the initialization. Ansistring nameString = "TEDIT"; for (int i = 1; i / / -------------------------------------------------------------------------------------------- ---------------------- Include Here is an improved example: // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------ Void TForm1 :: CreateShortcut (const ANSISTRING & File) {iPersistFile * PPERSISTFILE; LPMALLOC ShellMalloc; LPITEMIDLIST DesktopPidl; char DesktopDir [MAX_PATH]; if (FAILED (SHGetMalloc (& ShellMalloc))) return; if (FAILED (SHGetSpecialFolderLocation (NULL, CSIDL_DESKTOPDIRECTORY, & DesktopPidl))) return; if (! SHGetPathFromIDList (DesktopPidl, DesktopDir)) { ShellMalloc-> Free (DesktopPidl); ShellMalloc-> Release (); return;} ShellMalloc-> Free (DesktopPidl); ShellMalloc-> Release (); if (SUCCEEDED (CoInitialize (NULL))) {if (SUCCEEDED (CoCreateInstance ( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) & pLink))) {pLink-> SetPath (file.c_str ()); pLink-> SetDescription ( "Woo hoo, look at Homer's shortcut"); pLink-> SetShowCmd (SW_SHOW); if (succeeded (plink-> queryinterface))) {WideString strshortcutlocation (desktopdir); strshortcutlocation = "// bcbshortc Ut.Lnk "; pPersistfile-> save (strshortcutlocation.c_bs (), true); pPERSISTFILE-> Release ();} PLINK-> Release ();} Couninitialize ();}} // ------- -------------------------------------------------- ------------ Don't be trapped in the mud in COM, create shortcuts, including some to COM. Don't let you fall into the complexity of COM. COM is just a way to create and use the object. In this example we can consider not using COM but use equivalent C technology. COM code C psuedo-equivalent IShellLink * pLink; TShellLink * Link; IPersistFile * pPersistFile; TPersistFile * PersistFile; CoInitialize (); CoCreateInstance (CLSID_ShellLink, Link = new TShellLink; NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) & pLink) pLink- > Setpath (file.c_str ()); link-> setpath (file.c_str ()); plink-> setshowcmd (sw_show); link-> setshowcmd (sw_show); plink-> queryinterface (IID_IPERSISTFILE PERSISTFILE = (void ** ) & ppersistfile)) Dynamic_cast l # pragma pack (push, 1) struct DIOC_REGISTERS {DWORD reg_EBX; DWORD reg_EDX; DWORD reg_ECX; DWORD reg_EAX; DWORD reg_EDI; DWORD reg_ESI; DWORD reg_Flags;}; #pragma pack (pop) sDiskImageInfo-> hDevice = :: CreateFile ( ".//vwin32", 0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL); if (sDiskImageInfo-> hDevice == INVALID_HANDLE_VALUE) bRunNext = false; // Reset Floppy Disk reg.reg_EBX = 0; reg.reg_EAX = 0x0000 ; // IOCTL for block devices reg.reg_EDX = sDiskImageInfo-> Driver; reg.reg_EDI = 0; reg.reg_ESI = 0; reg.reg_Flags = 0x0001; // assume error (carry flag is set) dwResult = :: DeviceIoControl ( sDiskImageInfo-> hDevice, VWIN32_DIOC_DOS_INT13, ®, sizeof (DIOC_REGISTERS), ®, sizeof (DIOC_REGISTERS), & cb, 0); // Seek Floppy reg.reg_EBX = 0; reg.reg_EAX = 0x0C00; // IOCTL for block devices reg. REG_ECX = (SDISKIMAGEINFO-> NC << 8) | sdiskimageinfo-> ns; reg.reg_edx = (sdiskimageinfo-> nh << 8) | sdiskimageinfo-> driver; reg.reg_edi = 0; reg.reg_esi = 0; REG. REG_FLAGS = 0x0001; // Assume Error (Carry FLA g is set) dwResult = :: DeviceIoControl (sDiskImageInfo-> hDevice, VWIN32_DIOC_DOS_INT13, ®, sizeof (DIOC_REGISTERS), ®, sizeof (DIOC_REGISTERS), & cb, 0); // Read Floppy R_CreateDiskImageFile: reg.reg_EBX = 0; reg. REG_EAX = 0x0200 | 0x01; // ioctl for block def.reg_ecx = (SDISKIMAGEINFO-> NC << 8) | sdiskimageinfo-> ns; reg.reg_edx = (sdiskimageinfo-> nh << 8) | sdiskimageinfo-> driver; Reg.reg_ebx = (dword) & m_buf; reg.reg_edi = 0; reg.reg_esi = 0; reg.reg_flags = 0x0001; // Assume Error (Carry flag is set) dWResult = :: Deviceiocontrol (HDevice, Vwin32_Dioc_dos_INT13, ®, Sizeof (Dioc_registers), ®, Sizeof (Dioc_Registers), & CB, 0); if (! Dwresult || (reg.reg_flags & 0x0001)) {} I / O port read and write Realizing the careful readers will find that C Builder no longer supports such as InportB (), Outportb () I / O port read and write instructions. It is accurately that in the Windows environment, Borland C only supports the port operation of the 16-bit application program, and the port operation of the 32-bit application program is no longer supported, and the program from C Builder is 32-bit. I personally, this is the defeat pen of C Builder. Because of the PC, I / O address space and memory address space are all independent. Look at Delphi, don't you realize an interview with the I / O port through the port array? Can't make it clear? C Builder does not provide a similar mechanism? The following functions are written from the Internet. After passing through, it can be read from the I / O port under the Windows95 environment. Readers can use it for use. Void Outportb (unsigned short int port, unsigned char value) {// mov edx, * (& port); __emit __ (0x8b, 0x95, & port); // Mov al, * (& value); __emit __ (0x8a, 0x85, & value) ; // OUT DX, Al; __EMIT __ (0x66, 0xee);} Void Outportw (UNSIGNED SHORT INT Port, UNSIGNED SHORT INT VALUE) {// Mov EDX, * (& Port); __emit __ (0x8b, 0x95, & port); / / MOV AX, * (& Value); __emit __ (0x66, 0x8b, 0x85, & value); // Out DX, AX; __EMIT __ (0xEF);} Unsigned Char InportB (unsigned short int port) {unsigned char value; // MOV EDX, * (& port); __emit __ (0x8b, 0x95, & port); // in Al, DX; __Emit __ (0x66, 0xec); // Mov * (& Value), Al; __EMIT __ (0x88, 0x85, & value); Return Value;} unsigned short int value; // mov edx, * (& port); __emit __ (0x8b, 0x95, & port); // in Ax, DX __EMIT __ (0xed); / / MOV * (& Value), AX __EMIT __ (0x66, 0x89, 0x85, & value); Return Value;} Detects the mouse position, for example, the trigger event source from a timer Timer1 to detect the mouse position void __fastcall tform1 :: Timer1Timer (TOBJECT * Sender) {TPOINT PT; GetCursorpos (& PT); LA Bel1-> CAPTION = "(" INTSTOSTR (PT.x) ")") ";} Let Win32 application jump into the system zero well known, Win32 in Windows 95/98 The LAN (Ring0, the highest permission level) and the three ring (RING3, the minimum permission level) are utilized in the INTEL X86 system. In general, it is running under RING3, which is subject to strict "protective", which can only be used in torque. If we want to make some system-level operations, such as using such as "MOV EAX, CR0" in embedding, or call some essential system services (such as BIOS, DPMI services) as in DOS, "INT XX ", Will cause" illegal operation ". But this ability is not less than, when you come to this, Microsoft will write a VXD. VXD big home has been smelling, in VXD, not, but can execute the CPU's instructions, and can call the VMM (virtual machine tuner) and other VXDs. The most qualitative due to this ability is in its operation in Ring0, the same level as the system core. But it is the complexity of its system, the uncomfortable development tool is not easy to help the documentation, so that Microsoft has ranked a large number of proceedings and competition. WDM will also be a nightmare for WIN95 programming in Windows2000 (Windows 98, also starting). It is necessary to solve the WINDOWS NT core drive model. ---- Is there a simple manner? We can make a general WIN32 application program to run under Ring0, from the power of VXD? The answer is willing to pay. Next, I will tell this skill, there is a basic knowledge of Intel X86 protection model, please see the book. ---- First of all, this trick is based on the following theory: Depositing the specified address unit) is not a privileged instruction, that is This interrupt service program is called, and this interrupt service program is running under Ring0. This is not often similar in DOS. ---- II, the Windows95 Win32 application runs a segment mapped to all 4G memory, the selected child is 0137h, and the VXD in RING0 runs in another paragraph that maps to all 4G memory, select sub-028h, these two In addition to the access rights decided by the selection, there is no difference, the same offset in each segment corresponds to the same linear address. The interrupt service program in the Win32 application program can be subjected to the VMM address in RING0 in the range of RIN 32 applications. ---- Let me introduce a detailed explanation in the steps of the specific example. ---- This is a Win32 Console Program (control desk application program), although it looks like a real model DOS program in the DOS basket, it is the real price of the goods in the Win32 program under RING3. Create a Win32 Console Program project with Visual C 5.0 AppWizard, add the .cpp file, compile. #Include #include #include #include // If there is no DDK with the overline, these statements demonstrate DWordlong IDTR, SavedGate; Word Ourgate [4] = {0,0x0028,0xee00, 0x0000}; // Interrupt door descriptor format is as follows: dword _eax, _ecx, _cr0; word vmmver; hvm sysvm; void Nothing () {// used to test call in ring0 sysvm = get_sys_vm_handle ();} void __declspec (naked) Ring0Proc (void // Interrupt routine, run in ring0 {_asm {mov _eax, eax // mov _ecx, ECX // MOV EAX, CR0 // test Ring3 privilege mouv _cr0, eax //} vmmcall (GET_VMM_VERSION) ; // Call the VMM service _ASM {MOV VMMVER, AX} Nothing (); // Test in the interrupt routine running in RING0 _ASM IRETD / / interrupt back, with the real mode programming no essential difference} Void Main () // Main Program {_ASM {MOV EAX, OFFSET RING0PROC MOV [OURGATE], AX // Address SHR EAX, 16 // of the interrupt function to fill in the new interrupt door MOV [OURGATE 6], AX // Description SIDT FWORD PTR IDTR // Removes the contents of the interrupt descriptor table register (IDTR) to remove the MOV EBX, DWORD PTR [IDTR 2] // Remove Interrupt Description Table (IDT) Base Add EBX, 8 * 9 // Computing INT 9 Descript of Descriptions The address is placed in an int9 because it does not occupy MOV EDI in Win32 protection mode. Offset SavedGate MOV ESI, EBX MOVSD / / Save the original INT 9 descriptor to Movsd // SavedGate to restore MOV EDI, EBX MOV ESI, Offset Ourgate Movsd // Replace the original interrupt door descriptor MOVSD // to install interrupt service Routines MOV EAX, 0x6200 // Test if the data placed in Eax can be transferred correctly to Ring0 interrupt MOV ECX, 0 // to test whether the data placed in ECX can be correctly transferred to Ring0 interrupt MOV ECX, 0 // Test if the data placed in ECX can be transferred to Ring0 interrupt // Because many VXD services use this two register pass parameters int 9H // artificially trigger interrupt, it will appear to protect errors blue screen or illegal exercises. Dialog box, now the // interrupt service routine is installed, the interrupt service routine is called in RING0 by // vmm - Ring0Proc Mov EDI, EBX MOV ESI, OFFSET SAVEDGATE MOVSD // Restore the original interrupt door descriptor MOVSD} cout << "cr0 = << _ cr0 <} _getch (); if (0 =" = _ getch () "while (_kbhit () = "= 0);" How to get MEMO row and column new application, add two TLabel components named Label1, label2 on the form Form1; add two TButton components Named button1, button2; adds a TMEMO component named MEMO1. Then add the following code in the code editor. void __fastcall TForm1 :: Button1Click (TObject * Sender) {Label1 → Caption = SendMessage (Memo1 → Handle, EM_LINEFROMCHAR, -1,0) 1;} void __fastcall TForm1 :: Button2Click (TObject * Sender) {Label2 → Caption = Memo1 → SELSTART-SENDMESSAGE (Memo1 → Handle, EM_LINEINDEX, -1, 0) 1;} This method is equally applicable to RicheDit. Use sockets to use the Sockets Socket control to create an application that uses TCP / IP and related protocols to communicate with other systems. Using Sockets, you can read and write other machines connected through it without worrying about the details of the actual network software. Sockets provides a connection based on TCP / IP protocol. In addition to this, you can work well, in other related protocols, such as Xerox Network System (XNS), Digital's Dec Net, or Novell's IPX / SPX family. C Builder provides you write a web server or a client application to read and write other systems. A service or client program is usually focused on a single service such as Hypertext Transfer Protocol (HTTP) or File Transfer Protocol (FTP). Using Server Sockets, an application can provide one of these services to connect to a client that wants to use the service. Client Sockets allows an app to use a service application for this service using one of these services. Use Sockets to write applications, you must understand the following knowledge: 1. Services When you need to write network services or client applications, Sockets provides a joining. For many services, like HTTP or FTP, third-party service providers provide these services have been quite valid. Some even bundled with the operating system, so you can write it yourself. However, when you want more control services, if you want your application to communicate with network communications, or when there is no service to provide your special service, you may want to build your own service or customer. application. For example, when working in Distributed Data Sets, you may want to write a layer of communication with other system communications for the database. Want to use Sockets to implement a service, you must understand: 1. Service Agreement Before you write a network service or customer program, you must understand what service will be provided or used. Your web application must support the standard protocol of many services. If you are standard services such as HTTP, FTP write network applications, or Even Finger or Time, you must first understand the protocols used in communication with other system. Special Service Details You must see the documentation provided or used. If your application provides a new service to communicate with other system, the first step is to design a communication protocol for the server and client of this service. What information will be sent? How to organize this information? How do I encode this information? Application communications often, your network server or client application provides a layer of services used between network software and an application. For example, an HTTP service site provides content and answers between the Internet and a Web service application for HTTP request information. Provide an interface between Sockets between your web app (or customer application) and network software. You must provide an interface that is used in your application and intercom. You can copy the standard API provided by third-party service providers, or you can design and publish your own API. 2. Understand the service and port many standard services are associated, specified port numbers. When performing a service, you can consider a port number for your service. If you implement a standard service, Windows Socket Objects provides some ways to let you look for a port number for your service. If you provide a new service, on a Windows 95 or NT machine, you can specify a associated port number for your service in the file service. For more information on Services file, please see Microsoft's Windows Sockets documentation. Second, the Socket connection type Socket connection can be divided into three basic types, which reflects how to start connecting and local Socket connections. These three types are: 1. Client Connection Client Connections is a client Socket for a local system to connect to a server Socket on a remote system. The client connection begins by the client socket. First, the client socket must describe the server socket it wants to connect. Then the client socket looks for the server Socket, which is required to connect when the server is found. The server Socket may not complete the correct connection. Server Sockets maintains a client request queue and completes the connection when they have time. When the server socket accepts the client connection, the server socket will send a complete description to the client Socket it wants to connect, and the client's connection is complete. 2. Listening to the connection server Socket does not locate the client, instead, they form a passive, "semi-connected" state, listen to the request from the client. The server sockets form a queue that stores the connection requests they have heard. This queue records the client connection request as they have been connected. When the server Sockets agrees that the client connection request, it forms a new Socket to connect to the client, so this listening connection can keep the open state to allow other client requests. 3. Server connection When listening to the Socket agrees a client request, the server side Socket forms a server connection. When the server is agreed to connect, send a server Socket description to the client to complete the connection. This connection is confirmed when the client socket receives this description. One but connected to the client's socket, the server connection cannot recognize the connection from one client. The same capabilities have the same capabilities to receive the same event type. Only the listening connection is different, it has only one single end. Third, Sockets Description Sockets makes your web application communicates with other systems over the network. Each Socket can be seen as a terminal point in the network connection. It has a specified address. * This system is running * The interface type it understands * The port used to connect to a complete Socket connection description, you must provide the address on both ends of the connection. Before you start a socket connection, you must complete the connection you want to get. Some information can be obtained from a system platform that is running in your application. For example, you don't need to describe a local IP address of a client Socket - this information can be obtained from the operating system. You must provide information about the type of Socket that you work. Client Socket must describe the servers they want to connect. Listening Server Sockets must describe the ports they provide a reactive server. A full description of a socket connection terminal includes two parts: 1. IP address host is such a system, which runs an application containing socket. You must describe the host to the Socket, complete this description by giving the IP address of the host. The IP address is a string with four numbers (Byte) values, in standard Internet dot paid. For example, 123.197.1.2 A simple system can support more than one IP address. IP addresses are often difficult to memorize and are easy to get wrong. A alive method is to use the host name. The host name is an alias of the IP address, which is the unified resource positioning (URLS) you often see. It is a string, including domain names and services. For example http://www.wsite.com Many internal networks are provided to the host's name, the system IP address is an Internetip address. On the Windows95 and NT machines, if a host name cannot be used, you can create an entry name for your local IP address (this local IP address should refer to the host IP address you want to connect "in the hosts file. . For more information on HOSTS file, please see the documentation of Windows Sockets. Server Sockets does not need to specify hosts. The local IP address can be read from the system. If the local system supports more than one IP address, the server Sockets will listen to the client request on all IP addresses. When a server Socket consents a connection, the client provides a remote IP address. Customer Sockets must specify the remote host by providing hostname or IP address. As a host name and IP address, a number of applications use a host name to specify a system. The host name is easy to remember and easy to check the typesetting error. Further, the server can change the system or the special host name associated with the IP address. Using a host name, you can allow the client to describe the abstract site by the host name, even if the host uses a new IP address. If the host name is unknown, the customer socket must specify the IP address used by the server system. By giving an IP address to specify the server faster. When you provide a host name, Socket must search for IP addresses related to this hostname before locating the server system. 2. Although the port number provides enough information to find enough information in the Socket connection, you usually need to specify the port number of that system. Without a port number, a system can only perform a single connection at the same time. The port number is the unique identifier that allows a standalone system to connect to a host that supports multiple connections at the same time, each connection must specify a port number. In web applications, the port number is a digital code for the server tool. There is a habit to listen to the client connected to their own fixed port numbers so that they can find the client sockets. Server Socket listens to the relevant port numbers for them. When they allow a client Socket connection, they create a separate socket connection that uses different dedicated port numbers. Through this method, you can continue to listen to the port number of the relevant service. Client Socket uses a dedicated local port number without other sockets to find them. They specify the port number of the server-side Socket they want to connect so they can find the server application. Often, this port number is indirectly specified by named service you want to connect. Fourth, use the Socket control C Builder to provide two socket controls, client sockets, and servers sockets. They allow your network application to connect to other machines and allow you to read and write information through this connection. Associated with each Socket control is a Windows Socket object, and their role of the terminal is an actual Socket connection. The Socket control uses the Windows Socket object to encapsulate the Windows Socket API call, so your application does not have to care about the details of the connection or manage Socket information. If you want to use the Windows Socket API call or custom connection details, the Socket control provides convenience, you can use the Properies, Events, and methods of the Windows Socket object. 1. Add a client Socket to your Form or Data Module to make your application a TCP / IP customer. Customer Sockets allows you to specify the server Socket you want to connect to and the services you want the server. One, you describe what you want, you can use the customer's Socket control to complete the connection service. Each customer Socket control uses a separate customer Windows Socket object to deck the client terminal in the connection. Use the customer sockets to go: A. Specify that the you want to get the customer's Socket control has a digital Properties, allowing you to specify the server system and port you want to connect. You can specify the server system through the host name and use Host Property. If you don't know the host name, or if you care about find the speed of the server, you can specify the IP address of the server system, by using Address Property. You must specify one of the IP addresses and host names. If you are all specified, the customer's Socket control will use the host name. In addition to the server system, you must specify your customer socket to connect the port on the server system. You can use Port Property directly to specify the service port number. Or use the name you want to get directly in Service Property. If you specify port numbers and service names, the customer's Socket control will use the service name. B. Establish a connection Once you have completed the setting description description you want to connect to the properties of the server you want to connect, you can connect, by calling the OPEN method. If you want to automatically establish a connection when your application is started, set Active Property to TRUE when designing, and sets with Object Inspector. C. After obtaining information about the connection to the server Socket, you can use the customer Windows socket object related to your customers' Socket control to obtain information about the connection. Use Socket Property to access Client Windows Socket Object. Windows Socket Object has a Properties that allows you to determine the address and port number used by the clients and servers connected to the connected. When using a Windows Socket API called, you can use the SocketHandle Property area to get the Handle used by the Socket connection. You can use the Handle Property to access Windows to receive information from the socket connection. AsyncStyles property determines which information type is Windows Handle to be received. D. Close connection When you complete the communication want to close the socket connection, you can close the connection by calling the Close method. The connection may be turned off by the server. If this is this, you will receive a notification of an OnDisconnect event. 2. Add a server Sockets to your Form or Data Module to make your application a TCP / IP server. Server Sockets allows you to specify the service you want or you want to use to listen to the client request. You can use the server Socket control to listen and allow customer connection requests. Each server Socket control uses a single server Windows Socket Object (TSERVERWInsocket) to answer the connection in the server side. It usually uses a server customer WinODWS Socket Object (TSERVERCLIENTWINSOCKET) to answer each activity of the server and connect to the client Socket that is allowed. Use the server sockets to go: A. Specify port before you can listen to the customer request, you must specify a port to your listening service. You can use Port Property to specify this port. If your server application provides a standard service, this service uses a associated port associated with use. You can specify port numbers directly using Service Property. Using Service Property is a good idea to reduce errors when setting port numbers. If you specify both Port Property, you specify Service Property, and the service name will use the service name. B. Listening Customer Request Once you set your port number on the Server Socket control, you can listen to a connection by calling the Open method at runtime. If you want your application to automatically listen to the connection during startup, set the Active property to True by using the Object Inspector when designing. C. Connect to the client. They will automatically accept this request when the listening service Socket control receives a client connection request. The ONCLIENTCONNETC event will happen when you have received the notice. D. Get information about the connection one but your server socket opens the listening connection, you can use the server Windows socket Object associated with your server Socket control to obtain information about the connection. Use Socket Property to access Server Windows Socket Object.Windows Socket Object There is a property that allows you to find a customer socket connection to all active Customer Socket is allowed to be connected by the server Socket control. Use the Handle property to access the information received through the socket connection through the Socket. Each activity is connected to the customer app, and the customer Windows Socket Bject (TSERVERCLIENTWInsocket) is packaged. You can access all of these through the connection properties of Server Windows Socket Object. These Server Client Windows Socket Object Some properties allow you to decide which addresses and port numbers are used on both ends of the connection - Customer and Server Socket. When you use the Windows Socket API call, you can use the socketHandle property to get the Handle used for the Socket connection. You can use the Handle property to access the information from the Socket connection from the socket connection. The asyncStyles property determines which type of information will be received by Windows Handle. E. Close connection When you decide to turn off the listening connection, call the Close method. This will turn off all open, connect to the client application, and cancel any unconnected connection, then close the listening connection so that your service Socket control is not accepted any new connection. When the client closes their own independent connection to your Server Socket connection, you can get a message in the OnClientDisconnect event. V. Advocates for Socket events When using sockets write an application, Most of the work happens in the Handler event of the socket control. When you start reading or writing through the socket connection, OnRead and OnWrite events happen to notify Sockets in Non-Blocking Client Sockets. Similarly, Server Sockets (Blocking or Non-Blocking) Received ONCLIENTREAD and ONCLIENTWRITE events. When the server ends a connection, the customer ScokeTs receives an OnDisconnect event. When the client ends a connection, the server socket receives an OnClientDisconnect event. In addition, the client sockets and server-side socket are from the connection When you receive an error message, there will be an error event. Error event: Customer Sockets and Server Sockets typically generate an onERROR event when they receive an error message from the connection. You can write an onError event handling To respond to these error messages. This OneRror event handles provides information about this error when the socket is trying to do, and the error code provided by the error message. You can respond to this error in the OneRror event processing, and put the error code Change to 0 to avoid Socket production. When starting and completion occurs, the Socket control usually receives an event number. If your application needs to change the process of the Socket start operation or start through the connection When you read or write, you will write event handlers to answer these client events and server events. A.Client events When a customer socket opens a connection, the following events occur: 1. A ONLOOKUP event first occurs, it tries to position Server Socket. You cannot change Host, Address, Port, Service properties to change the server you want to locate. You can use the socket property to access the Client Windows Socket Object, and use its socketHandle property to call the Windows API to change Socket Customer properties. For example, if you want to set a port number in the customer application, you must do this before the Server Client connection. 2.Windows socket settings and initialization event notifications. 3. When you find a Server Socket, a OnConnecting event In this incident, Windows Socket Object can utilize some information about the service socket on the other end of the connection through the socket property. This is the first opportunity to get the port and IP address of the actual use, which may differ from the monitor Socket The port or IP address is obtained. 4. The server agrees to the request, the client socket complete the connection. 5. When a connection is determined, an OnConnect event occurs. If your socket immediately begins to read or write through the connection, you should write A OnConnect event handler is asking for this matter. B. Server events Server Socket Controls Connects to both: listen to the connection and connect to the customer application. Server Socket receives all events of these two connections. Listening Event When the monitor connection is constituted, the OnListen event happens. At this time you can get Server Windows socket object through the socket attribute. You can use its socketHandle property to change the socket, before the Socket opens the monitor. For example, if you want to limit the listening service Use the IP address, you can do this on this OnListen event handler. Events connected to the client When a server Socket agrees to a client connection request, the next event happens: 1. Server Socket generates an OnGetSocket event, which is transmitted to the other end of the connection via Windows Socket Handle. If you want to provide yourself defined TSerClientWinsocket of Descendant, you can build in the OnGetSocket Event Handler, will be used to replace TSERVERCLIENTWISOCKET. 2 A onaccept event happens, transmit a new TServerClientWinsocket object to the event handle. This is the first point, when you use the TServerClientWinsocket attribute to get the information of the client that is connected to the client. 3. If the service type is stthreadblocking, An ONGETTHREAD event happens. If you want to provide your own TServerClientThread subclass, you can build one in the OnGetThread event handle, which will replace TSERVERCLIENTTHREAD. 4. If the service type is StthreadBlocking, an ONTHREADStart event occurs when this thread begins When executed. If you want to perform any initialization this thread, or call some Windows Socket API, you should use the ONTHREADSTART event handle before which the thread begins to read and write. 5. When the client completes a connection, an OnClientConnect event occurs. If it is a non-blocking service, you may want to start reading or writing at this end via the socket. Six, reading and writing through the socket connection to other machines Through these connections to read and write information What information is what you want to read and write, or when you want to read and write, it relies on which Socket connection related services. Reading and writing via Sockets can be asynchronous, so you don't need to block in your web application. Other code execution. This is called Non-Blocking Connection. You can also pass through the Blocking Connection, at this time your next line of code must wait until the read or write operation is completed. A.non-blocking connection, read and write are asynchronous So do not need to block other code execution in your network application. Create a non-blocking connection: 1. Set the clientType property in the customer socket to ctnonblocking. 2. Set the serverType property in ServerType property in ServerType properties for Stnonb When the connection is non-blocking, the other end of the connection is attempting to read or write the read and write event to inform you of your socket. Read and write operation event Non-blocking sockets wants to read or write, it A read and write operation event will notify you Socket. On the client sockets, you can respond to these events in the OnRead or OnWrite event handle. On the server-side ScokeTs, you can in the onClientRead or OnClientWrite event handle to these events Make a reaction. Windows socket object associated with the socket connection is used as a parameter in the read or write of the event handle. Windows socket object provides a method number (Number of Methods) to allow you to read or write. Via Socket Connection read, use the ReceiveBuf or ReceiveText method. Before using the ReceiveBuf method, use the Recereength method to determine the number of bytes that the connection to the other end Socket is ready to send. Write through the socket, use SendBuf, SendStream, or Sendtext Method. If you send a Socket connection after sending information via Socket, you can use the SendStreamThendrop method. SendStreamThendrop method will close the socket connection after all the information is written, it can read the information from Stream. If you use SendStream or SendStreamThendrop method, do not release Stream Object, Socket will automatically release this stream after the connection is over. Note: SendStreamThendrop will close a separate customer connection service instead of listening. B.Blocking Connections When you use the connection is blocking, your socket must be read or write through the connection, better waiting from the Socket connection. Notice. Use the Blocking Socket when your connection end read and write operation changes. For the client sockets, set the clientType property to ctblocking to make it to form a blocing connection. According to your client application, you may want to build. An execution thread to complete the read or write operation so that your application can continue to perform other threads, when it is waiting to be done by the connection or write operation. For the server sockets, set the serverType property as stthreadblocking to make a blocking connection. Because Blocking Connections hangs the execution of other code while waiting for the connection or write information, the server Socket control typically generates a new execution thread to each client connection, when Servertype is set to stthreadblocking. Many applications using Blocking connection Write using threads (Using Threads. Even if you don't use thread, you may also want to use TWinsocketStream to read and write. 1) Using threads When using a blocking connection to read or write, customer sockets will not automatically generate A new thread. If your client application does not do, until the read or write information is completed, then this is what you want. If your app includes a user interface, it also needs to respond to the user's operation, then You may want to generate a separate thread to read and write. When the server sockets forms a blocking connection, they often generate separate threads to each client, so no customers need to wait until other customers complete through the connection or write. By default, server Sockets uses TServerClientThread objects to perform different threads for each connection. TSERVERCLIENTTHREAD object simulates OnClientRead and OnClientWrite events in the non-blocking connection. However, when listening to Socket, not local Thread-local. If the customer requests frequent, you will want to build your own TSERVERCLIENT Thread subclasses provide a secure thread-safe to complete read and write. When writing a client thread or writing a server thread, you can use TwinsocketStream to do actual read and write operations. A) Write the client thread to customers End Connection Write a thread to define a new thread object, use the new thread object dialog. Your new thread object execute method's handle of the handle of the read / write operation, you can create a TwinsocketStream object, then use it to read Or write. Use your own thread to create it in the OnConnect event handle. For more information on establishing and running the thread, see Executing Thread Objects. Example: This example shows a client thread to the server after the connection is determined. write request void __fastcall TMyClientThread :: Execute () {while // make sure connection is active {try {TWinSocketStream * pStream = new TWinSocketStream (ClientSocket1.Socket, 60000). (Terminated && ClientSocket1-> Active!); try {char buffer [10]; GetNextRequest (buffer); // GetNextRequest Must Be a thread-save method // Write a Request to the Server Pstream-> Write (Buffer, Strlen (Buffer) 1); // continue the communication (eg read a response)} __finally {delete pStream;}} catch (Exception & E) {if (E.ClassNameIs ( "EAbort")!) Synchronize (HandleThreadException ()); // you must write HandleThreadException }}} B) write server-threaded server connection thread is derived from TServerClientThread because of this, you can not use a new thread Object dialog box alternative to manually declare your thread as follows:.. class PACKAGE TMyServerThread: public ScktComp :: TServerClientThread {public void __fastcall ClientExecute (void); Note that you will replace the Execute method with the overloaded clientExcute method. Execute the ClientExecute method to write a similar Execute method thread to the client. However, when you put a customer's Socket control to your application from the control bar When you come to replace this method. Listening Services Socket As a connection, the service customer thread must be built using the TServerClientWinsocket object. This can utilize a common cientsocket property. In addition, you can use the HandleException this protected method, better than you Write your Thread-Safe Exception. WARNING: Server Sockets caches the thread they use. Confident that the ClientExecute method performs some necessary initialization operations so that they do not produce unfavorable results when executing. When you use you When thread, create it in the ONGETTHREAD event handle. When the thread is established, set the createSuspended parameter to false. Example: This example shows a thread for an application service, this application is a read request by the client after the connection is determined. Void __fastcall tmyserverthread :: ClientExecute () {While (! Terminated && ClientSocket-> Connected) // make sureness is active {try { TWinSocketStream * pStream = new TWinSocketStream (ClientSocket, 60000); try {char buffer [10]; memset (buffer, 0, sizeof (buffer)); if (pStream-> WaitForData (60000)) // give the client 60 seconds to Start Writing {IF (Buffer, Sizeof (Buffer) == 0) ClientSocket-> Close (); // if can't read in 60 seconds, close the connection // Now process the request} else ClientSocket -> close ();} __finally} catch (...); {handleexception (); }}} C. When you use TwinsocketStream When you implement a thread as a Blocking connection, you must be sure that the Socket in the other end of the connection is ready to write or read. Blocking connection does not inform Socket When it is ready to write or read. Want to see if the connection is ready, use the TwinsocketStream object .TwinsocketStream provides a method to help adjust the selection of read or write operation time. Call the waitforData method to wait until the other end of the Socket is ready to write. Write actions. If the read or write operation is not completed within the specified time period, Stream will take timeout. This timeout is treated as a result, and the socket application will not be paused, but constantly pass or write through a Dropped connection. Note: Note: Note: Note: Note: Note: Note: Note: Note: You can't use TWinsocketStream Windows95 / 98 how to hide the application in the non-blocking connection not let it appear in the CTRL-ALT-DEL dialog box? A simple way to hide your app from the Ctrl-Alt-Del dialog is to go to the application's title. If the main window of a program is not headline, Windows95 does not put it in the CTRL-Alt-Del dialog. The best place to clear the title attribute is in the Winmain function. WinAPI Winmain (Hinstance, Hinstance, LPSTR, INT) {try {Application-> Title = ""; Application-> Initialize (); Application-> Createform (__ classid (tform1), & form1); application-> run (); Catch (Exception & Exception) {Application-> showexception (& Exception);} Return 0;} Another way is to call the RegisterServiceProcess API function Register the program into a service mode program. RegisterServiceProcess is a function that is related but no official files in kernel32.dll. There is no prototype instructions for this function in the MS SDK header file, but can be found in Borland Import Libraries for C Builder. Obviously, the main purpose of this function is to create a service mode program. The reason why it is obvious because it does not say anything about this function in MSDN. The following example code demonstrates how to hide your program from the Ctrl-ALT-Del dialog box under Windows95 / 98 to hide your program from the Ctrl-Alt-Del dialog box, which uses RegisterServiceProcess. // ------------------------------------------------------------------------------------------------_stdcall * pRegFunction) (DWORD, DWORD); class TForm1: public TForm {__published: TButton * Button1; private: HINSTANCE hKernelLib; pRegFunction RegisterServiceProcess; public: __fastcall TForm1 (TComponent * Owner); __fastcall ~ TForm1 ();}; // - ---------- CPP File ------------------------------ #include "unit1.h" # define RSP_SIMPLE_SERVICE 1 #define RSP_UNREGISTER_SERVICE 0 __fastcall TForm1 :: TForm1 (TComponent * Owner): TForm (Owner) {hKernelLib = LoadLibrary ( "kernel32.dll"); if (hKernelLib) {RegisterServiceProcess = (pRegFunction) GetProcAddress (hKernelLib, "RegisterServiceProcess "); if (RegisterServiceProcess) RegisterServiceProcess (GetCurrentProcessId (), RSP_SIMPLE_SERVICE);}} __fastcall TForm1 :: ~ TForm1 () {if (hKernelLib) {if (RegisterServiceProcess) RegisterServiceProcess (GetCurrentProcessId (), RSP_UNREGISTER_SERVICE); FreeLibrary (hKernelLib); }} // ---------------------------------------------- --- Note: No R under Windows NT EGISTERSERVICEPROCESS function. How to hide the application's task bar icon First, see these terms. The system tray is a small box in the right corner of the task bar, and the application can display a small icon in the pallet. Task bars are toolbars that can be extended on the screen. It is where the program icon is located. To hide the task icon of the program, you can apply the showWindow function and pass it to the Application-> Handle window handle. ShowWindow (Application-> Handle, SW_HIDE); if you want the task bar icon to appear again, simply change sw_hide to sw_show. ShowWindow (Application-> Handle, SW_SHOW); Note: You can set the Visible property of the main window to hide it. Note: The task bar icon for hiding the window via ShowWindow is not lasting. Some movements will make the task bars icon. You can set the hidden application window to Tool Window to remove the program's task bar icon to avoid it again. Tool Windows never has a task bar icon. Make the application window a tool window has a side effect: it will not appear in the list when the user presses alt-tab. You can call the API function getWindowlong and SetWindowl to make the application window a Tool window. WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int) {DWORD dwExStyle = GetWindowLong (Application-> Handle, GWL_EXSTYLE); dwExStyle | = WS_EX_TOOLWINDOW; SetWindowLong (Application-> Handle, GWL_EXSTYLE, dwExStyle); try {Application-> Initialize () Application-> Createform (__ classid (tform1), & form1); Application-> Run ();} catch (exception & exception) {Application-> Showexception (& Exception);} return 0;} Writing your own ping.exe program in Windows In the system, we often use ping.exe to test the connectivity of the network. The implementation process of ping is simple. This command will trigger an IP layer to send a simple IP package, usually 32 bytes. After the goal receives this package, turn the source address and destination address, resend this package, of course, add some timeout mechanisms. In fact, we can also use NMecho controls in C Builder NetMaster to implement network connection detection. First define the following controls: Three Edit controls: A IP address or domain name for receiving the remote host, one for receiving timeout mechanisms for user settings, one for setting the port number. Two RicheDit controls: A information for sending information to the remote host, one for receiving information from the remote host. Two Checkbox controls: Used to set the port number yourself. A Button Control: Used to perform a test. A STATUSBAR control: The status of the application is displayed. The program implements the code as follows: void __fastcall tform1 :: button1click (TOBJECT sSender) {// Set NMECHO control standard TCP / IP attribute nmecho1-> host = edit1-> text; nmecho1-> timeout = start (edit2-> text) ; If (checkbox1-> checked) nmecho1-> port = start (edit3-> text); else nmecho1-> port = 7; // TCP / IP Echo's default port number nmecho1-> reportLevel = status_basic; nmecho1-> Connect (); // Established connection richedit2-> clear (); for (int i = 0; I // richedit1) to send information to remote hosts Richedit2-> text = richedit2-> text nmecho1-> echo (richedit1- > Lines-> strings [i]); NMecho1-> disconnect ();} Note: When calling the NMECHO control, you should ensure that the connection has been established before receiving the data. When the Connect () method is called, if the user enters the domain address instead of the IP address, and the domain server successfully parses this domain name, the control of the onHostResoved event will trigger the control, in the process of this event, we will resolve successful messages Displayed to the user in the status bar. The specific implementation code is as follows: void __fastcall tform1 :: nmecho1HostResolved (Tcomponent Sender) {statusbar1-> panels-> items [0] -> text = "host resolved!";} If the remote host entered by the user is incorrect, the control will be triggered. OnInValidHost events, in this event, the pop-up dialog requires the user to re-enter the IP address or domain name address of the remote host, and then try to reconstruct the server. The specific code is as follows: void __fastcall tform1 :: nmecho1invalidhost (Bool && Handled) {ANSISTRING S; IF (INVAILID HOST: "," Specify A New Host: ", S)) {nmecho1-> host = s; handled = true After the connection is established, the ONCONNECT event of the control will be triggered. In this process, we will display the successful message to the user in the status bar. The specific implementation code is as follows: void __fastcall tform1 :: nmecho1connect (TOBJECT {"{statusbar1-> panels-> items [0] -> text =" echo HAS Connected Host! "If you call the connect () method, Timeout still has no connection with the server, the onconnectfailed event of the control, and the message we fail to the user in the process of this event. The specific implementation code is as follows: void __fastcall tform1 :: nmecho1connectionfailed (TOBJECT {ShowMessage ("Connection Failed!");} In addition to NMECHO controls, NETMASTER's NMDaytime, NMTIME these two controls can be implemented. The method is the same as NMecho controls, and the difference is NMDayTime and NMTIME these two controls do not need to first call the connect () method, and their connection to the server is automatically performed when using DayTimeStr, TIMESTR property. Use C Builder to prepare a service under Winnt - WINDOWS NT with Windows 9x has a very important difference, that is, Windows NT provides a lot of powerful service (service). These services can start from the start of NT, or allow users to start by the control panel, and can be stopped by the Win32 application. Even without the user login system, these services can also be executed. Many ftps, WWW servers and databases are in NT in the form of service, thereby achieving unattended. Even the latest version of the "hacker" program Back OrificE 2000 is also hidden on the service in the form of a service. Because Service is more complicated, many developers want to develop their own service but often. In view of this, then we will construct a new service from the head to the end, readers as long as they add their own code in the program, then you can easily have a self-service. Before writing service, first introduce several important functions: ---- 1. SC_Handle OpenScManager (LPCTSTSTR LPMABASENAME, DWORD DWDESIREDACCESS) --- OpenScManager function Opens the Service Control Manager Database on the specified computer. The parameter lpMachinename specifies the computer name, and if specified as the unit for the air. LPDATABASENAME is empty for the Service Control Manager Database name to be opened. Dwdesired Accept Specifies the permissions of the operation, one of the values below: ---- SC_MANAGER_ALL_ACCESS / / All permissions ---- SC_Manager_connect // Allow Connect to Service Control Manager Database ---- SC_Manager_Create_Service // Allow the creation of service objects and put It joins Database ---- SC_MANAGER_ENUMERATE_SERVICE / / Allow enumeration of service ---- sc_manager_lock // Allow locks Database ---- SC_Manager_Query_lock_status // Allow Query Database's blockade information ---- Function is successful, return A handle pointing to the Service Control Manager Database, returns NULL. Note: Winnt manages all services through a database called Service Control Manager Database, so this database should be opened for any operation of Service. ---- 2. SC_HANDLE CreateService (SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword) - - The CreatService function generates a new service. The parameter hscmanager is the handle of the Service Control Manager Database, returned by OpenScManager. LPServiceName is the name of Service, LPDisplayName is a service display name, DwdesiredAccess is access, this program with service_all_access. WSERVICEPE, specify the service type, in this program with service_win32_oen_process | service_interactive_process. DWStartType is the service launch method, this program uses self-start, ie DWStartType is equal to service_auto_start. DWERRORCONTROL Note What actions take when the Service is in the launch, this program uses service_error_ignore, and readers can be changed to other. LPBINARYPATHNAME indicates the path name of the Service ontology program. The remaining five parameters can be typically set to NULL. If the function call is successful, return this new service handle, and the null is returned. Corresponding to this function is DELETSERVICE (HService), which deletes the specified service. ---- 3. SC_Handle OpenService (sc_handle hscmanager, lpctstr lpserviceename, dword dwdesiredAccess) ---- OpenService function Opens the specified service. The parameter hscmanager is the handle of the Service Control Manager Database, returned by OpenScManager. LPServiceName is the name of Service, dwdesiredAccess is access to access, and its optional value is more, readers can see the SDK Help. The function call successfully returns the open service handle, and returns NULL. ---- 4. Bool StartService (sc_handle hservice, dword dword dword dword dwnumservicegs) ---- StartService function launches the specified service. The parameter hservice is a handle pointing to the service, returned by OpenService. DwnumServiceA is the number of parameters required to start the service. LPSZServiceArgs is the parameters required to start the service. The function is successful, returns true, and false is returned. ---- 5. Bool ControlService (SC_Handle HService DWORD DWCONTROL, LPSERVICE_STATUS LPSERVICESTATUS) ---- Service program does not have a dedicated stop function, but with the controlService function to control the service's pause, continue, stop and other operations. DwControl parameter specifies the control command is issued, it is possible for the following values: SERVICE_CONTROL_STOP // Stop Service SERVICE_CONTROL_PAUSE // Pause Service SERVICE_CONTROL_CONTINUE // continue Service SERVICE_CONTROL_INTERROGATE // Query Service status SERVICE_CONTROL_SHUTDOWN // make ControlService call failure ---- parameters lpServiceStatus Is a pointer to service_status. Service_status is a more important structure that contains various information of the service, such as the current state, which control commands, and more. ---- 6. Bool queryServiceStatus (sc_handle hservice, lpservice_status lpservicestatus) ---- The queryServiceStatus function is relatively simple, it queries and returns the status of the current service. ---- Preparing a service usually requires two programs, one is a service body, one is a control program for controlling Service. Usually the service body is a console program, and the control program is a normal WIN32 application (of course, the user can be enabled by the control panel without the control program, stopping, but not adding, deleting operations. ---- First, let's write the service body. For the service body, it is generally composed of three parts: main (), servicemain (), handler (), the source code of Main (): (Note: Due to the relationship of the space, most of the program is not Error handling, readers can add themselves) int Main (int Argc, char ** argv) {service_table_entry ste [2]; // A service process can have multiple threads, this is the entry table for each // thread Stendum STE [0 ] .lpServiceName = "wzservice"; // thread name Ste [0] .lpserviceproc = serviceMain; // thread portal address Ste [1] .lpServiceName = null; // The last one must be null ste [1] .lpServiceProc = NULL; StartServiceCtrlDispatcher (ste); return 0;} - - main () is the main thread of the service. When Servie Control Manager starts a service process, it is always waiting for this service to call the StartServiceCtrldispatcher () function. Main () as the main thread of this process should call StartServiceCtrlDispatcher as soon as possible after the program. StartServiceCtrlDispatcher () is not immediately returned after being called, connects the host's main thread to Service Control Manager, so that Service Control Manager will start, stop control commands to the main thread through this connection. The main thread played the role of a command transponder, it or calls handle () to process the stop, continue the control requirements, or generate a new thread to execute ServiceMain. StartServiceCtrlDispatcher () returned at the end of the service. ---- ServiceMain () is a true entry point for Service, must be properly defined in main (). The two parameters of ServiceMain () are passed by StartService (). The following is the ServiceMain () of source code: void WINAPI ServiceMain (DWORD dwArgc, LPTSTR * lpszArgv) {ssh = RegisterServiceCtrlHandler ( "WZSERVICE", Handler); ss.dwServiceType = SERVICE_WIN32_OWN _PROCESS | SERVICE_INTERACTIVE_PROCESS; ss.dwCurrentState = SERVICE_START_PENDING; // If the code of the user program is more (execution time is more than 1 second), it is set to service_ start_pending, and then set to service_running after the user program is completed. ss.dwcontrolsaccepted = service_accept_ stop; // indicates that the command currently accepts is a stop command. Ss.dwwin32exitcode = no_error; ss.dwcheckpoint = 0; ss.dwwaithint = 0; setServiceStatus (SSH, & SS); // must update the status of the service in the database at any time. Mycode (); // here the code can be placed in the user's own ss.dwServiceType = SERVICE_WIN32_OWN_ PROCESS | SERVICE_INTERACTIVE_PROCESS; ss.dwCurrentState = SERVICE_RUNNING; ss.dwControlsAccepted = SERVICE_ACCEPT_STOP; ss.dwWin32ExitCode = NO_ERROR; ss.dwCheckPoint = 0; ss.dwWaitHint = 0; setServiceStatus (SSH, & SS); mycode (); // This can also be placed in the user's own code} should immediately call registerServiceCtrlHandler () to register a handler to deal with the controller or control panel to Service. Control requirements. Handler () is called to deal with the forwarder, the following is the source code of Handler (): Void WinApi Handler (DWORD OPCODE) {switch (opcode) {copy service_control_stop: // Stop Service mycode (); // can be put into it here User's own code ss.dwwin32exitcode = 0; s.dwcurrentState = service_stopped; // Put the current state of Service to stop ss.dwcheckpoint = 0; ss.dwaithint = 0; setServiceStatus (ssh, & ss); / must be updated at any time The status of the service in the database; Case Service_Control_INTERROGATE: SETSERVICESTATUS (SSH, & SS); / Must update the status of the service in the database in the database;}} ---- Well, the service body program is basically completed, we will then look at the service Control Program: ---- Control Program is a standard Window program, which mainly has four heads: Create Service, Delete Service, Start, STOP, which is used, delete, start, and stop service. The following is a part of the source thereof: 1. Generate Service void __fastcall TForm1 :: CreateBtnClick (TObject * Sender) {scm = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (scm = NULL!) {Svc = CreateService (scm, " WZSERVICE "," WZSERVICE ", // Service name SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, // an automated way to start SERVICE_ERROR_IGNORE," C: //ntservice.exe ", // Service body program path, and the specific location must consistent NULL, NULL, NULL, NULL, NULL); if (svc = NULL) CloseServiceHandle (svc);! CloseServiceHandle (scm);}} 2. remove Service void __fastcall TForm1 :: DeleteBtnClick (TObject * Sender) {scm = OpenSCManager (NULL, NULL, SC_MANAGER_CONNECT); if (scm = NULL!) {svc = OpenService (scm, "WZSERVICE", SERVICE_ALL_ACCESS); if (! svc = NULL) {QueryServiceStatus (svc, & ServiceStatus); if (ServiceStatus.dwCurrentState == SERVICE_RUNNING) // before deletion, stop this Service ControlService (svc, SERVICE_CONTROL_STOP, & ServiceStatus);. DeleteService (svc); CloseServiceHandle (svc); // delete the Service, the best and then call for immediate CloseServiceHandle} // from This entry is removed in the database. CloseServiceHandle (scm);}} 3. Start Service void __fastcall TForm1 :: StartBtnClick (TObject * Sender) {scm = OpenSCManager (NULL, NULL, SC_MANAGER_CONNECT); if (scm = NULL!) {Svc = OpenService (scm, "WZ Service ", service_start); if (SVC! = Null) {StartService (SVC, 0, NULL); // Start Service CloseServiceHandle (SVC);} ClosServiceHandle (SCM);}} 4. Stop Service Void __fastcall TFORM1 :: StopbTnclick (TObject * Sender) {scm = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS); if (! scm = NULL) {svc = OpenService (scm, "WZSERVICE", SERVICE_STOP | SERVICE_QUERY_STATUS); if (svc = NULL!) {QueryServiceStatus (svc, & ServiceStatus); if (ServiceStatus.dwCurrentState == SERVICE_RUNNING) ControlService (svc, SERVICE_CONTROL_STOP, & ServiceStatus); CloseServiceHandle (svc);} CloseServiceHandle (scm);}} how to automatically turn off the screen in C BUILDER WINDOWS protection --- - In actual programming applications, when procedures need to handle certain calculations with longer time, this time may make Windows start screen protection, so that the process of the program will relatively longer. So how do you automatically turn off the screen protection when running a program? ---- Windows sends a WM_SYSCOMMAND message to the activated programs before starting screen protection, and sets the WPARAM parameter of the message to sc_screensave. We can use the onMessage event of the Tapplication class in C Builder to handle this message from Windows. If you set the Handled parameter to TRUE after the received message, this response message value can prevent the screen protection. ---- The process of C Builder 4.0 is as follows: ---- 1, select File | New Application from the main menu to create a new empty project file. Then add a Label object on the FORN, set its CAPTION to "this program will turn off Windows Screen Protection". ---- 2, declaration of the member function processMessage in the program header file Unit1.h to the definition of TFORM1. class TForm1: public TForm {__published: // IDE-managed Components TLabel * Label1; private: // User declarations void __fastcall ProcessMessage (TMsg & message, bool & handled); public: // User declarations __fastcall TForm1 (TComponent * Owner);} ; ---- 3, unit1.cpp, the increase in the program statement ProcessMessage function: void __fastcall TForm1 :: ProcessMessage (TMsg & message, bool & handled) {if (message.message == WM_SYSCOMMAND && message.wParam == SC_SCREENSAVE) {handled = true;}} ---- 4, in TFORM1 constructor adds the following code: __fastcall tform1 :: tForm1 (tComponent * Owner): TFORM (OWNER) {Application-> OnMessage = processMessage;} display / hidden task Bar icon ---- Standard Windows application runtime generally display task icon on the taskbar, users can click the taskbar icon with the mouse to switch, but some applications do not use the taskbar icon, such as typical Office Tools bars, and some programs can be displayed or hidden by the user custom display mode, such as Winamp. In our program, you can also do it, just call the Windows API function setWindowl, as follows: // Hide the task bar icon: setWindowlong (Application-> Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW); // Display Task Bar Icon: SetWindowlong (Application- > Handle, GWL_EXSTYLE, WS_EX_APPWINDOW; Mailbox Monitor ---- This article will introduce you to how to write your own mailbox monitor, the program will directly call the Winsock function to communicate network communication. In addition to the Winsock programming knowledge, it is necessary to understand the POP3 protocol. The lower surface is a rough introduction to POP3, and readers can see the RFC 1225 more detailedly to solve this agreement. First, the POP3 protocol ---- POP3 server program is often available in TCP port 110. When the customer wants to use the service, it will set up a TCP connection with the server. Once connected to the establishment, the POP3 service will send a welcome message to the customer. Then the passenger will send the commander to the server, and the server gives a negative answer. The command of POP3 is set by a keyword or keyword word. Each command is made as a end bunch of backhand (0xD0XA). For all the commands, the POP3 serviceter will provide an answer. The replies of the server are added to a status marker to add some additional information. The two markers used in front of the purpose are " OK" and "-err", and the demonstration of the table is the order of the customer. All replies are also in the end of the return. ---- The four POP3 commands that are related to the topics discussed in this article are User, Pass, List and Quit. User command format user name ---- NAME is the user ID of the user on the POP3 server. The customer should send this command after the welcome message picks up the servers or after the last user or passenger is lost. Pass command format pass string ---- String is the password of the user. The rear of the customer will send this command after sending the user command and receives a OK answer. If the estate name and password are correct, the server replied OK, and no -ERR. List command format list ---- If the user has an email, the list command will answer OK, list all the identifiers and sizes of all messages (each mail one), the last line contains only one period (0xA0x2e) The whole answer end. If the user does not have a message, some servitators will return to -RR, and some can return to one OK and a row that only contains one of the stations. Of course, the customer must pass the client program to send a list command to the server after passing the pass command. Quit Command ---- Reverse logging from the POP3 server. Second, real-see-related functions ---- Next, the communication rules called Pop3Checkmail in accordance with the communication regulations defined by the POP3 protocol, just call this function, we can detect the mailbox. ---- The code under the lower surface is in Pascal language with Delphi 4, we must include Winsock Units and initialize the Winsock dynamic connection library before calling the following functions. The code that initializes the Winsock dynamic connection library is as follows: ---- IF WSAStartup ($ 002, Wsadata) <> 0 Then Halt; ---- Pop3checkmail's prototypes as follows: ---- Function POSSWORD: STRING; VAR Maillist: tstringlist; var erroormsg: string: Bool; ---- Parameter Description: ---- Email and Password are the user's Email mailbox name and password. ---- Variable parameter MAILLIST is used to return the identity of the message and the size of the Mailist.count indicates the number of mail. ---- Variable parameter erroormsg returns to the wrong message. ---- Under the implementation code of POP3CHECKMAIL and its functions used. Connect_server function ---- function: set up a TCP connection with the specified host, return to an Socket description. The parameter host specifies the name of the host, the port specifies the port number. Function Connect_server (Host: integer): Integer; var i: integer; p: ^ longint; Phe: phostent; sin: sockaddr_in; begin sin.sin_family: = afrin sin_family: = AF_INET; SIN.SIN_PORT: = HTONS (port); // get the ip for host, allowing for dotted decimal Phe: = gethostByname (pchar (host)); if Phe <> nil dam p: = POINTER (PHE ^ .h_addr_list ^); sin.sin_addr.s_addr: = P ^; ELSE BEGIN I: = inet_addr (pchar (host)); if i <> -1 Then sin.sin_addr.s_addr: = I end; // Create a socket result: = socket (pf_inet, sock_stream, 0); IF (Result = INVALID_SOCKET) THEN EXIT; // Connect To Server if Connect (Result, Sin, SizeOf (SIN)) = Socket_ERROR THEN BEGIN {Error Handling} End; End; Write_Socket Function ---- Function: Write to Socket A string. Function Write_Socket (Sockfd: Integer; Integer; Begin Result: = Winsock.send (Sockfd, Pointer (s) ^, Length (s), 0) end; socket_readline function ---- Function: From SOCKET Read a line. Function socket_readline (sockfd: integer): string; // read uncle # 10 var s: string; buf: array [0..1] of char; N: cardinal; begin buf [0]: = # 0; buf [1 ]: = # 0; s: = '; N: = Recv (SOCKFD, BUF, 1, 0); While N> 0 DO Begin BUF [1]: = # 0; s: = S BUF; IF (BUF [0] = # 10) Then Break; N: = Recv (SOCKFD, BUF, 1, 0); End; Result: = Trim (s); end; pop3Response function ---- function: read POP3 server Returns the information, if it is " OK", the function returns Ture if it is "-err", returns FALSE. Function pop3Response (sockfd: integer): Bool; var s: string; begin s: = socket_readline (sockfd); if Copy (s, 1, 3) = ' ok' Ten Result: = true else {if Copy (S, 1 , 4) = '-ERR' TEN} Result: = false; end; pop3checkmail function ---- function: detect mailboxes for email, if there is new mail, return each email size back through variable parameters . Function Pop3checkmail (Email, Password: String; var maillist: tstringlist; var sarormsg: string): Bool; var sockfd, i: integer; s, host, user: string; begin result: = false; errormsg: = '; if Maillist = NIL THEN EXIT; S: = Trim (email); i: = POS ('@', email); user: = trim (COPY (S, 1, I -1)); Host: = Trim (COPY S, I 1, Length (email) -i); maillist.clear; if (user = ') or (host =') THEN BEGIN ERRORMSG: = 'Invalid Email Address.'; EXIT; End; IF Host [1] = '[') AND (Host [Length (Host)] = ']') THEN Begin Host [1]: = '; Host [Length (Host)]: = # 0; End; Host: = Trim (host); sockfd: = connect_server (Host, 110); if not pop3Response (sockfd) THEN BEGIN ERRORMSG: = 'Cannot Connect To Server'; EXIT; End; Write_Socket (Sockfd, 'User' User # 13 # 10); if not pop3Response (SOCKFD) THEN BEGIN ERRORMSG: = 'user failed'; exit; end; write_socket (sockfd, 'pass' password # 13 # 10); if not pop3Response (sockfd) THEN BEGIN ErrorMsg: =' Pass Failed '; EXIT; End; Write_Socket (Sockfd,' List '# 13 # 10); pop3Response (sockfd); while true do begin s: = socket_readline (sockfd); if s = '.' Ten Break; Mailist.Add (s); end; write_socket (sockfd, 'quit' # 13 # 10); ClosSocket (SOCKFD); Result: = true; end; three, email detection --- - Let's take a look at a simple example of using a POP3CHECKMAIL function. var MailList: TstringList; ErrorMsg: String; ... MailList: = TstringList.Create; POP3CheckMail ('simon_liu@263.net ',' mypassword ', MailList, ErrorMsg); If MailList.Count> 0 then MessageBox (0, Pchar ('You Have' INTOSTR (Mailist.count) 'New Messages!' MessageBox (0, Pchar (ErrorMsg), 'Error', 0); Mailist.Free; ---- If you read the implementation code of the Pop3checkmail function, you will find that this function can get the number of emails, You can also get the size of each email. You can get the size of the mail with the Strings archive array of variable parameter maillist in the POP3CHECKMAIL function. ---- Implement the pop3checkmail function, and then write a status of a POP3 mailbox on this basis. You can call Pop3checkmail functions with a timer, which you can monitor an email mailbox. If you want to monitor multiple Email mailboxes, just create a thread for each mailbox and set the POP3CHECKMAIL function in the thread. In your program, there is no control to use Delphi, and a complete mailbox monitoring process can only be only 60K left. C building makes alarm clock ---- Dafai Networms have been caught in the following dilemma: about 7:30 in the late place, but they have been crawling in front of the computer in front of the computer, etc. reflect, Friends have already went down, and they have to apologize for a few days. Why don't you do a simple alarm clock as follows, let you go online, big party. As long as you open this application when you go online, you can set the time (of course, you can also be a default). When the time arrives, the music sounds, goes to date. ---- This alarm program has the following components: Sequence number component type component name function 1 TLabel L_Clock_1 Display "Enter Date" 2 TDATETIMEPICKER DTP_CLOCK_1 Select Date 3 TLabel L_Clock_2 Display "Input Time" 4 TDATETIMEPACKER TDP_CLOCK_2 Select time 5 TMEDIAPLAYER MP_CLOCK Play Music 6 TBUTTON B_CLOCK_OPEN Re-open 7 TTIMER T_CLOCK Timer Detection 8 TButton B_Clock_Close Close Application ---- Screen Component List ---- Description: DTP_Clock_1 The Kind property is set to DTKDATE, DTP_CLOCK_2's Kind property set to DTKTIME, MP_CLOCK's FileName property is set to any MID, WAV, AVI file existing on your host. The T_Clock's INTERVAL property is set to 10. ---- Event descriptions are as follows: 1, T_Clock's Ontimer: {// Trigger the demo Struct Date D at time on time, Struct Time T; Ansistring Thour, Tmin, Tsec; Int Dyear; Int Dintyear; Int DMON, DDAY; ANSISTRING TINTHOUR, TINTMIN, TINTSEC; Ansistring DINTMON, DINTDAY; // Take the day GetDate (& D); DYEAR = D.DA_YEAR; DDAY = D.DA_DAY; DMON = D.DA_MON; DINTYEAR = STRTOINT (DINT.SUBSTRING (1, 2)) DINTMON = DINT.SUBSTRING (4, 2); DINTDAY = DINT.SUBSTRING (7, 2); // Take the time getTime (& T); THOUR = ANSISTRING; TMIN = Ansistring (t.ti_min) ; //tsec=ansistring (TITI_SEC); // Tint = Ansistring (DateTimePicker1-> Time); TintHour = Tint.substring (10, 2); Tintmin = Tint.substring (13, 2); // Tintsec = Tint.substring (16, 2); // Alarm Service Function IF ((STRTOINT (THOUR) == STRTOINT (TINTHOUR) & (STRTOINT (TMIN) == STRTOINT (TINTMIN) & (STRTOINT (Ansistring (Dyear). Substring (3, 2)) == DINTYEAR) && (STRTOINT (DMON) == StrtOINT (DINTMON) && (STRTOINT (DDAY) == STRTOINT (DINTDAY))) {dtimer-> enabled = false; MediaPlayer1-> Open (); MediaPlayer1-> Play ();}} 2, b_clock_open on the onclick: {t_clock-> enabled = true;} 3, b_clock_close's onclick {AP PiCATION-> Terminate ();} ---- Of course, this program can also expand, refine, as I only detect the trigger condition, of course, it can detect seconds, or only detect. Instructions for dial-up IP addresses With the rapid spread of Internet, the number of people on the Internet is growing. Among them, most people are dial online by ordinary telephone lines. We know that every Internet computer, no matter what way to internet, allocated one or more independent IP addresses. For users dial-up Internet access, it is generally allocated by its ISP to dynamically allocate an IP address every time it is dial, this address may not be the same each time (whose reason is mainly to make full use of limited resources). So, can we manage the IP address when you internetrate through some way? The answer is yes. Below we use C Builder to develop a small program to implement this function. (Note: This program can also run in the local area network) ---- First use the New Application under the BCB's File menu to create a new project, named IPCheck.bpr. ---- Then add five tags (Label) and two buttons (button) on the form FORM1, as shown. ---- Next, double-click the oncreate event of the form, plus the following procedure: void __fastcall tform1 :: formcreate (TOBJECT * Sender) {Wsadata Wsadata; if (WsaStartup (MakeWord (1, 1), & WSADATA! = 0) {// Initialization Winsock calls MessageBox (Null, "WRONG WINSOCK VERSION", "ERROR", MB_OK); RETURN;} Refresh1click (sender); // The program is the start of the IP address} and double-click Refresh Button, plus the following program void __fastcall tform1 :: refresh1click (TOBJECT * Sender) // refresh IP address {char hostname [80]; lphostent lphostent; struct in_addr addr [2]; // This program assumes that the host is not more Host, only // one network card and a dynamic IP for (int i = 0; i <2; i ) {memset (& addr [i], 0, sizeof (in_addr)); // Turn 0 to the IN_ADDR structure, Fill in the back} if (getHostName (HostName (Hostname (Hostname) == Socket_ERROR) {// Get this Master name MessageBox (Null, "Can't getting local host name.", "Error", MB_OK; Return } Label3-> caption = hostname; LPHOSTENT = gethostByname (Hostname); // Using the host name to get the host structure IF (! "," YOW! BAD HOST LOOKUP. "," Error ", MB_OK); RETURN;} for (int i = 0; lphostent-> h_addr_list [i]! = 0; i ) // Get IP address from the host address table {Memcpy (& addr [i], lphostent-> h_addr_list [i ], sizeof (in_addr));} label4- > Caption = inet_ntoa (addr [0]); label5-> caption = inet_ntoa (addr [1]);} Double-click the Refresh button, plus the following program void __fastcall tform1 :: button2click (Tobject * sender) {WSACLEANUP ( ); // Release Winsock call close ();} ---- Finally, don't forget to add #include TRAY programming is special, but it is not difficult, mainly including three aspects such as icons, tool tips, and messages, which are part of the shell programming. Shellapi provides a shell-notifyicon function that can be used, delete, or modify the icon in the tray. After placing the icon on the tray, the Windows Shell will be responsible for notifying the app on the icon. The shell-notifyicon function is defined as follows: Winshellapi Bool WinAPI Shell-Notifyicon (PNOTICONDATA PNID); dwMessage denotes the action to be completed: NIM-Add (Add Icon), NIM-DELETE (Remove Icon), NIM-Modify (Modify Icon) Or a prompt text, PNID is a pointer to the Notifyicondata structure. The definition of the structure is as follows: typedef struct -notifyicondata {dword cbsize; // The number of bytes of the structure must be initialized with the size of the structure. HWND HWND; // Accept the window handle of the TRAY icon message; // is used by the application defined icon ID uint uflags; // is used to identify the domain that needs to change its value, NIF_ICON indicates that HICON is valid, can be used to modify the icon, Nif_Message indicates that uCallbackMessage is valid, used to define messages, NIF-TIP indicates that the SZTIP parameter is valid and modifies the tooltip. UINT UCALLBACKMESSAGE; // Application Defined Message Hicon Hicon; // Tray Icon Handle CHAR SZTIP [64]; // Tool Tips Text} Notifyicondata; Let's explain the implementation method through a concrete example, the program is not The main form is displayed, and only an icon is added to the tray, double-click the icon to turn off the program. The program runtime tray area is displayed as follows: New project and place a Timer control to the form. Open the unit1.h file, add the header file Description #include Void - FastCall TFORM1 :: FastCall TFORM1 :: FormDestroy () {transovetrayicon (); // Form When you remove icon} to write Timer1 Timer event code, when the user leaves the mouse on the icon, display prompts text: void --fastcall TForm1 :: Timer1Timer (TObject * Sender) {NOTIFYICONDATA icondata; memset (& icondata, 0, sizeof (icondata)); icondata.cbSize = sizeof (icondata); icondata.hWnd = Handle; String s = " My icon! "; // Define the prompt text strncpy (icondata.sztip, s.c_str (), sizeof (icondata.sztip)); icondata.uflags = nif-tip; shell-notifyicon (nim-modify, & icondata); } The program is not displayed when the program is run, and only the corresponding program icon is placed on the tray. Select View | Project Source from the C Builder Gatherings, add the code after the application → initialize () statement of the Winmain function: showwindow (Application → Handle, sw-hide; Application → ShowMainform = false; press F9 to compile and run the program, the corresponding icon will appear on the tray. The above code is compiled in C Builder3, PWIN98 environments and runs. How to minimize or recover programs to implement it with one of the following three methods. Method 1: Send a Windows message to the Handle property or Application-> Handle of the main window. This message is WM_SYSCOMMAND, set WPARAM to SC_MINIMIZE or SC_RESTORE. You can call the SendMessage API function to send messages. // set to WPARAM SC_MINIMIZE to a minimized window SendMessage (Application-> Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0); // set WPARAM to restore the window to SC_RESTROE SendMessage (Application-> Handle, WM_SYSCOMMAND, SC_RESTORE, 0); Method two: Call the ShowWindow API function. You must send the Application object handle to the showWindow function. If you are transmitted to the slogwindow function is a main window, the main window will minimize Desktop instead of a task bar. @ Minimize: SW_MINIMIZE transferred to ShowWindow ShowWindow (Application-> Handle, SW_MINIMIZE); // Recovery: SW_RESTORE transferred to ShowWindow ShowWindow (Application-> Handle, SW_RESTORE); Method three: Minimize or Restore function call Application object. // Call Minimize Minimize Application Application-> minimize (); // Call RESTORE Recovery Application Application-> restore (); call Application method more easily, but sending a WM_SYSCOMMAND message function is stronger. In addition, the WM_SYSCOMMAND message allows you to maximize the program, change the cursor to help the cursor, scrolling program, move a window, change the window size, and even simulate the ALT-Tab to another window. Keep in keeping, achieving these functions is better with an API function. Despite the call showWindow work, you probably don't want to use it to minimize or recover procedures. ShowWindow causes the minimization animation when the hidden window is minimized. This looks a little stupid because the animation is far from the center of the program main window. The copyright window before making the main window is displayed in the project file. File-> New Form New Window, design the appearance of the window. The window is called AboutBox, and the source file is named aboutbox.cpp to select Project-> Options, and the new window is removed from the automatic creation. Select View-> Project Source, open the source file of the project file, add the sentence. #include "AboutBox.h" WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int) {DWORD lTime; try {Application-> Initialize (); AboutBox = new TAboutBox (AboutBox); AboutBox-> Show (); AboutBox-> Update (); LTIME = gettickcount (); application-> createform (__ classid (tMAINFORM), & mainform); WHILE ((GettickCount () - LTIME) / 1000 <3); AboutBox-> Hide (); Aboutbox-> Free () Application-> Run ();} catch (exception & exception) {Application-> showexception (& Exception);} return 0;} Decision has been connected to the Internet Using the NetMasters PowerSock control to read the local IP address if it is "0.0.0.0 "The description is not connected. Example: void __fastcall tform1 :: button1click (TOBJECT * Sender) {if (PowerSock1-> localip == "0.0.0.0") ShowMessage ("Not Connected"); Else ShowMessage (PowerSock1-> localip);} Get login username Void __fastcall tform1 :: button2click (TOBJECT * Sender) {dword dwsize = 0; // Determine the byte required memory getUserName (null, & dwsize); // Defines buffer char * szbuf = new char [dwsize]; szbuf [0 ] = '/ 0'; // Read User Name GetUsername (SZBUF, & DWSIZE); Label2-> CAPTION = SZBUF; Delete [] SZBUF;} Hide Desktop Icon Void__fastcall TFORM1 :: Button1Click (Tobject * Sender) {HWND HDESKTOP; // Get the desktop handle hDesktop = findwindow ("progman", null); // hide icon showwindow on the desktop ShowWindow;} // ----------------- -------------------------------------------------- -------- Void __fastcall tform1 :: button2click (TOBJECT * Sender) {HWND HDESKTOP; HDESKTOP = FINDWINDOW ("progman", null); // Display icon showWindow on the desktop ShowWindow;} program Run the VOID __FASTCALL TFORM1 :: Button1Click (TOBJECT * Sender) {Tregistry * reg; char AppfileName [256]; if (edit1-> text == ") // determine if the file name is empty {MessageBox (Handle," Application The serial name cannot be empty. "," Wrong ", MB_ok MB_ICONERROR); exit1-> getTextBuf (AppFileName, 256); reg = new Tregistry (); try {reg-> rootkey = hkey_local_machine; if (REG-> OpenKey "Software // Microsoft // Windows // CurrentVersion // Run", false)) {// Add value reg-> WriteString ("startup1", appfilename) in the registry;} else messagebox (Handle, "Opens the registry failure. "," Wrong ", mb_ok | mb_iconerror);} catch (...) {reg-> closekey (); reg-> free ();}} control panel call void __fastcall tform1 :: Button1click (Tobject * sender) {Uint res; // Winexe return result // Display Control Panel RES = WINEXEC ("Rundll32.exe shell32.dll, control_rundll", 9); if (res == 0) MessageBox (Handle, "programs beyond memory." , "Error", MB_OK | MB_ICONERROR); Else If (Res == Error_Bad_Format) MessageBox (Handle, "Command Error.", "Error", MB_OK | MB_ICONERROR); ELSE IF (Res == Error_File_not_found) MessageBox (Handle, " The specified file is not found. " "," Wrong ", MB_ok | MB_ICONERROR);} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- void __fastcall tform1 :: button2click (Tobject * sender) {// Auxiliary Options | Keyboard Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Access.cpl, 1", 9);} //---------------- -------------------------------------------------- ------- void __fastcall tform1 :: butt3click (TOBJECT * Sender) {// Add / Remove Program Properties | Installation / Uninstall Winexec ("Rundll_Rundll AppWiz.cpl, 1", 9 } // --------------------------------------------- ----------------------------- Void __fastcall tform1 :: button4click (TOBJECT * Sender) {// Display Properties | Background Winexec "Rundll32.exe shell32.dll, control_rundll desk.cpl, 0", 9);} // ------------------------- ---------------------------------------------- void __fastcall TFORM1 :: Button5Click (TOBJECT * Sender) {// Internet Properties | General Winexec ("Rundll_Rundll inetcpl.cpl, 0", 9);} // --------- -------------------------------------------------- ---------------- Void __fastcall tform1 :: Button6click (TOBJECT * Sender) {// Region Setting Properties | Area Sets Winexec ("Rundll_Rundll Intl32.dll, Control_Rundll INTL.CPL, 0", 9);} // ------ -------------------------------------------------- ------------------- void __fastcall tform1 :: button7click (TOBJECT * Sender) {// Game Controller | General Winexec ("Rundll32.exe shell32.dll, control_rundll joy .cpl, 0 ", 9);} // ------------------------------------- -------------------------------------- void __fastcall tform1 :: button8click (TOBJECT * Sender) { // Mouse Properties | Button Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Main.cpl", 9); } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: button9click (Tobject * sender) {// Multimedia Properties | Audio Winexec ("Rundll32 . EXE shell32.dll, control_rundll mmsys.cpl, 0 ", 9);} // ----------------------------- --------------------------------------------- void __fastcall tform1: : Button10Click (TOBJECT * Sender) {// Modem Properties Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Modem.cpl", 9);} // ---------------- -------------------------------------------------- --------- void __fastcall tform1 :: button11click (TOBJECT * Sender) {// Network | Configuring Winexec ("Rundll_Rundll Netcpl.cpl", 9);} // -------------------------------------------------- ----------------------- void __fastcall tform1 :: button12click (TOBJECT * Sender) {// Password Properties | Change Password Winexec ("Rundll32.exe shell32. DLL, Control_Rundll Password.cpl, 9);} // ------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- void __fastcall tform1 :: button13click (Tobject * sender) { // Scanner and Digital Camera Properties | Equipment Winexec ("Rundll32.exe Shell32.dll, Control_Rundll Sticpl.cpl", 9);} // -------------------------------------------------------------- -------------------------------------------------- ------- void __fastcall tform1 :: button14click (TOBJECT * Sender) {// System Properties | General Winexec ("Rundll_Rundll Shell32.dll, Control_Rundll Sysdm.cpl, 0", 9);} // -------------------------------------------------- ------------------------- void __fastcall tform1 :: button15click (TOBJECT * Sender) {// Date / Time Properties | Date and Time Winexec (" Rundll32.exe shell32.dll, control_rundll timedate.cpl, 9); } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- void __fastcall tform1 :: button16click (TOBJECT * Sender) {// Power Management Properties | Power Scenario Winexec "rundll32.exe shell32.dll, control_rundll powerpfg.cpl", 9);} // ------------------------------ -------------------------------------------- void __fastcall tform1 :: Button17Click (TObject * Sender) {// dialing properties | my location WinExec ( "rundll32.exe shell32.dll, Control_RunDLL Telephon.cpl", 9);} by simulating keystrokes void __fastcall TForm1 :: Button1Click (TObject * Sender) { // Simulation Press the letter A button in the Edit1 component PostMessage (Edit1-> Handle, WM_KeyDown, 65, 0);} // -------------------- ------------------------------- void __fastcall tform1 :: timer1timal (TOBJECT * Sender) {// Simulation in Form1 Press the Tab button PostMessage (Form1-> Handle, WM_Keydown, VK_TAB, 0);} // --------------------------- --------------- Let the title bar flash void __fastcall tform1 :: button1click (TOBJECT * Sender) {// Activation Timer Timer1-> enabled = true;} // ------ -------------------------------------------------- --------------------- void __fastca LL TFORM1 :: Button2Click (TOBJECT * Sender) {// Disable timer Timer1-> enabled = false; // Restore the original flashwindow (Handle, False);} // ---------- -------------------------------------------------- --------------- void __fastcall tform1 :: timer1timer (TOBJECT * Sender) {// Make the form header bar flash flashwindow (Handle, True); beep (); // Sound prompt } Start screen protection SendMessage (hwnd_broadcast, wm_syscommand, sc_screensave, 0); Year-Month day of the week's Tissue Ansistring Iyear, IMONTH, IDAY, IHOUR, IMINUTE, ISECOND; IYEAR = now (). FormatString ("YYYYY"); // Take Year IMONTH = now (). FormatString ("mm"); // Being iDay = now (). Formatstring ("dd"); // Take the day IHOUR = now (). Formatstring ("hh"); // Take an hour iMinute = now (). FormatString ("nn"); // Take the minute ISECOND = Now (). Formatstring ("ss"); // Take a second dayofweek (now (). CurrentDate () keyboard event void __fastcall tform1 :: formkeydown (Tobject * sender, word & key, tshiftstate shift) {if (Shift.Contains (SSSHIFT)) // If the SHIFT button is pressed, Shift Statusbar1-> Panels-> Items [0] -> Text = "Shift"; if (Shift.Contains (SSALT)) // If the ALT key is pressed, Alt Statusbar1-> panels-> items [1] -> text = "alt"; if (SHIFT.CONTAINS (SSCTRL)) // If the CTRL button is pressed Then display ctrl statusbar1-> panels-> items [2] -> text = "ctrl"; if (SHIFT.CONTAINS (SSCTRL))))) // If you press The Alt Ctrl key is displayed on the second panel, Alt Ctrl statusbar1-> panels-> items [5] -> text = "alt ctrl";} // ---------- -------------------------------------------------- --------------- void __fastcall tform1 :: formyup (Tobject * sender, word & key, tshiftstate shift) {// In Shift, Alt, and Ctrl key bomb starting time clear status bar Content IF on the panel (! (Shift.contains (ssshift))) statusbar1-> panels-> items [0] -> text = ""; if (! (Shift.contains (ssalt)) statusbar1-> panels- > Items [1] -> text = ""; if (! (Shift.contains (ssctrl)))) statusbar1-> panels-> it EMS [2] -> text = ""; if (! Shift.contains (salt) && (! shift.contains (ssctrl))) statusbar1-> panels-> items [5] -> text = ""; / --------------------------------------------------- -------------------------- void __fastcall tform1 :: formousedown (TOBJECT * Sender, TMousebutton Button, TshiftState Shift, Int x, int y) { If (SSLEFT)) // If the left button is pressed, it displays Left statusbar1-> panels-> items [3] -> text = "left"; if (Shift.Contains) on the fourth panel SSMIDDLE)) // If the midkey is pressed, display middle statusbar1-> panels-> items [4] -> text = "on the fifth panel "; If (SSDOUBLE)) // If you are double-clicked, display double statusbar1-> panels-> items [5] -> text =" double ";} // --- -------------------------------------------------- ---------------------- Void __fastcall tform1 :: formouseup (Tobject * Sender, TMousebutton Button, TshiftState Shift, Int x, int y) {// in the mouse Press the key to clear the content IF on the corresponding panel in the status bar when the key bomb is removed (! (Shift.contains (ssleft))) statusbar1-> panels-> items [3] -> text = ""; if (! (Shift.contain) SSMIDDLE)))) STATUSBAR1-> Panels-> items [4] -> text = ""; if (! (shift.contains (ssdouble))) statusbar1-> panels-> items [5] -> text = " } hide the taskbar void __fastcall TForm1 :: Button1Click (TObject * Sender) {HWND WndHandle; // get the window handle of the taskbar WndHandle = FindWindow ( "Shell_TrayWnd", NULL); ShowWindow (WndHandle, SW_SHOW); // display the taskbar } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: button2click (TOBJECT * Sender) {hWnd WndHandle; WNDHANDHANDLE = FINDWINDOW ("shell_traywnd" , Null; showwindow (wndhandle, sw_hide); // Hidden taskbar} // ------------------ -------------------------------------------------- ------- void __fastcall tform1 :: formcreate {setwindowl (Application-> Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW); __published: // IDE-managed Components private: // User declarations void __fastcall WMQueryEndSession (TWMQueryEndSession & msg); public: // User declarations __fastcall TForm1 (TComponent * Owner); BEGIN_MESSAGE_MAP MESSAGE_HANDLER (WM_QUERYENDSESSION, TWMQueryEndSession, WMQueryEndSession) END_MESSAGE_MAP (TForm)} ;