From Form1.caption = "Hello World"
Like many people, my first Windows program starts with Visual Basic, our article title is a simple statement from VB, and the role is to change the program form header. Of course, in different development tools, it can also be shown in the following form:
Visual Basic:
Form1.caption = "Hello World" or me.caption = "Hello World" or direct capen = "....
C builder: form1-> caption = "Hello World";
Delphi: form1.caption: = 'Hello World';
Visual C : SetwindowText ("Hello World");
And the implementation form in the original SDK:
SetwindowText (Hmain, "Hello World"); (assuming Hmain is the handle of the main form)
So listening is not to compare the programming language, our problem is: When we simply detrition a character to CAPTION (this can be other variable name), the compiler has done the form to set the form. Title. The answer is simple: there are many kinds of programming languages, but Windows only one, they all call the API function: setWindowText (), or send a WM_SETTEXT message directly to the specified form.
SetwindowText function original is:
Bool setWindowText
HWND HWND, // Handle of window OR Control
LPCTSTR LPSTRING / / Address of String
);
The two parameters are: form handles, with string addresses, and return according to the success of the operation: True, false; thereby seems to be easier to understand, the MFC class library encapsulates a group of works. Function, and through the C class package, simplify operation, the specific MFC implementation is roughly as follows:
Void CWnd :: setWindowText (LPCTSTR LPSZSTRING)
{
AskERT (:: iswindow (m_hwnd)); // Check if m_hwnd is a valid form handle
// Call the API function, m_hwnd is the internal member variable of the CWND class, representing the handle of the form
:: SetwindowText (m_hwnd, lpszstring);
}
This is the MFC simplifies a performance of Windows programming.
Of course, SETWINDOWTEXT is not just used to set a dialog or a main form title, because we are actually large because of the various butttons, editing boxes (Edit), text tags (static or label), etc. Small and small forms or subforms, so you can also thus set the title of a button. Viore with you to get the form of the form, you can use the getWindowText function, the function original is similar to SetWindowText. Although Set, Get, WindowText is easy to understand but is still not simple:
Set the title: S1 = "Hello World", capen = S1
Get the title with S1 = CAPTION
It looks more intuitive.
Ok, then let's take a look at how C Builder is implemented in Delphi.
When I just used C Builder, I was in addition to surprise C Builder design application's shortcut and convenience, another confusing question is when I wrote form1-> caption = "Hello World"; time C Builder How to implement SETWINDOWTEXT operation, familiar with c friends know: Suppose Form1 is an instance of a class, then we write form1.bok = true; or form1.nsize = 12; we are right when we are A member variable of a class assignment. If FORM1 is a pointer, the above sentence is: form1-> bok = true; form1-> nsize = 12; in addition to the simple assignment operation, in addition to the simple assignment operation, it will not do anything else. thing. If you want to perform a message box, you should call the appropriate member function. At that time, my understanding was that C Builder must be called the member function of the corresponding set, Get started internally. And there is no reason to think that the compiler is to scan the current code row when we start compiling, and then click Caption = "...." To replace it into internal FORM1.SETCAPTION ("....") Of course, things will not be as simple, because C Builder shared the same VCL class library with Delphi, and can even use the component library written in Pascal language, so C Builder is not a pure C in a sense. The compilation environment, obviously Borland has made some small hands and feet to C Builder, making it compatible with Delphi. There are many benefits, both use similar IDE environments, using the same form (* .dfm) format, most important to use the same VCL class library. As long as you have used a development environment, it is easy to master another development tool. The disadvantage is that it is easy to track a large pile of piles to begin, and End will be tracked in the C Builder. It is also the result of the same VCL class library as before. I regretted that I didn't talk about the Pascal language, and I didn't interested it. Therefore, the implementation form in C Builder is still unclear, but the faint approving is that some PASCAL code for C Builder is implemented above.
Later, because "Work Needs", we must maintain a test program written in Delphi, temporarily assaulting a Pascal language. When it sees the implementation of the Pascal class, I finally solved the questions I am in C Builder. Similar to C , similar classes are also supported in Object Pascal. Have your own: private, protected, public, and inheritance of class. A simple Pascal class is approximately the following form:
TMYPOINT = Class (TOBJECT)
Private
FX, fy: integer;
public
Function PtinRect (const r: trect): boolean; {Judging whether the current point is in the specified RECT}
Function getx: integer;
Function game: integer;
/ / Set fx is NX and perform error detection, or execute some operation as needed.
Procedure setX (const nx: integer);
Procedure sety (const ny: integer);
END;
The setx, Getx used to set the current X position, respectively. The advantage of setting FX into classes and setting FX through the SETX function is to give an Assert breakpoint when a suitable value (such as a negative number, or a specific range) is given when the SETX is called. Remind the programmer, or in actual implementation will change the inappropriate value to 0 and throw an exception. Of course, we will think if it can be easily: pt.x: = 12; a1: = pt.y; and it is more convenient to perform error detection when assigning the value; fortunately, Borland's programmer is designing VCL (Visual It is also thinking when component library, thereby introducing a set of new keywords in Delphi: Property, Read, Write; to achieve the above needs, the TMYPOINT class rewritten is as follows:
1: TMYPOINT = Class (TOBJECT)
2: Private
3: fx, fy: integer;
4: Procedure setX (const nx: integer);
5: Procedure sety (const ny: integer);
6: Function getx: integer;
7: Function game: integer;
8: Public
9: {Judging whether the current point is in the specified RECT}
10: Function PtinRect (const r: trect): boolean;
11: Property x: Integer Reade w w;
12: Property Y: integer read get gety;
13: End;
14:
15: Procedure TmyPoint.setX (const nx: integer);
16: Begin
17: IF (NX <0) OR (NX> 1000) THEN
18: fx: = 0
19: Else
20: fx: = Nx;
21: End;
twenty two:
23: Function TMYPOINT.GETX: Integer;
24: Begin
25: RESULT: = fx;
26: End;
27:
This way you execute: pt.x: = 200 compiler knows that you want to perform a Write action, you will automatically jump to procedure tmypoint.setx (const nx: integer); assignment, and then call for read operations Getx function. If GetX is just a simple return of the FX value, 11 lines can also be changed to:
Property X: Integer Read FX Write SetX;
It can be seen: When we write form1.caption = "...." In Delphi, or C Builder, it is really executing a specific function, not an assignment, and there is no magic after this. Existence, everything is so clear. Like the operator of C , Delphi also overloads the class "." Operator, which does not only look more intuitively, reduce the code writing, from a sense, it also reflects the idea of object-oriented programming.
Ok, I will talk about VB. In fact, VB is not a language of a full-scale object, lacking many must-have language features, although the implementation of the class is not allowed, and the inheritance of the class is not allowed, in addition to type check, error It is not strong enough to handle it. The worst that Microsoft does not provide similar MFC source code like VC6.0. When you want to track InputBox, MsgBox, SQR, Trim function in VB, you can't see the specific implementation. This prevents us from understanding the principles of Caption = "Hello World" in VB, of course, friends who are good at Debug can discover themselves. The good news is that the Basic language has been extended to a fully object-oriented language in VB.NET and supports exception handling. I have turned a few VB.NET and C # books, I feel that Basic in VB.NET is more like a C language, such as handling this piece in an exception, and C # is very like the current VB, add a lot. " "operating. (By the way, everyone is in the stage of learning .NET, everyone is in the stage of blind, so the bookstore .Net series book is very bad, please take care.) Summary:
This paper analyzes the implementation principle of the Caption = "..." operation in VB, VC, C Builder, Delphi. The so-called "existing is reasonable" I don't mean that we see it is justified. When the program is designed, ask several few why help improve your program design level.
Ok, it's nothing to say, the only problem is that this article is suitable for someone, the master doesn't look, it is too simple. Novice may not understand what I am talking about: p, if you like this article or any problem, please contact me (fpefans@sina.com).
BY Ji Yang 2-5-2002
To reprint, keep the integrity of the article, other casual.