Create an active form favorites in BCB

Source: Internet
Author: User

Create an active form favorites in BCB
C ++ buider has very powerful ActiveX development functions, but there is a small amount of information in this area. After some exploration, I finally figured out the basic method for creating web ActiveX.
(1) Open bcb6 and create an ActiveX project. Select the active form on the ActiveX tab.
(2) In the displayed dialog box, set ActiveX name to actfrm, and the subsequent implementation unit and project name will be changed automatically.
(3) Check make control licensed and include version information. After confirmation, a file similar to the standard form will appear.
(4) Add a button on the automatically created form in C ++ builder to implement some functions, such as showmessage ("click the button ");

Configure package parameters. You certainly want your OCX files to be created not dependent on the development environment of the Local Machine. Therefore, you must make appropriate settings for the compiler.
I project-> Option-> packages remove the check mark before build with runtime packages
Ⅱ project-> Option-> linker removes the check mark before dynamic RTL, so that the ocx file you generate will not depend on the development environment of BCB.
After completing the above steps, a simple ActiveX is created. You can select the register ActiveX Server under the run menu to register it. Then you can see your development results, if you need to publish to the Internet and register through a browser, you need to make the following settings.
Configure web deployment options
Click the Web Deployment Options menu, and the Web deployment option setting window appears. There are three property pages.
Project I is mainly used to set the location information of ActiveX files.
① Target Dir (full path of the deployed OCX) sets the OCX directory, that is, the directory to which the ocx file will be copied after project> webdeploy is selected, we can create a punlished directory under the current project as the release directory.
E. g: the current project directory is F: \ projscts \ firstocx release directory is F: \ projscts \ firstocx \ published (that is, target DIR)
② Target URL (Virtual Path of the deployed OCX)
If the ActiveX control we created is not installed on the client, the browser will automatically search for it at the specified URL.
E. g: MyApp, the virtual directory of port 8080 on the local ip192.168.0.228 server (Tomcat is used, and MyApp is the directory under D: \ Tomcat \ webapps)
Enter http: // 192.168.0.228: 8080/MyApp/
③ HTML Dir (full path of the deployed HTML file)
The location where HTML files matching ActiveX files are stored. You can set them to F: \ projscts \ firstocx \ published to facilitate management.
Select four items under general options.
A. Use cab File compression B. Deploy required packages
C. Include File version number D. Deploy additional files
II packages this page is used to publish additional packages used in the control, because we have removed additional packages in step (5), so this page is blank
III additonal files, this page is used to publish other files. As to whether OCX files use other files, we can use the dependency Walker tool with VC ++ to view the files, according to the display result of dependency Walker, we know that the DLL file used by OCX already exists in the Windows directory, and there is no need to publish it together with our ocx file.
However, because make control licensed is selected when the control is created, a LIC file should be released along with the package. This file is under the project directory, on the additonal files page, click Add to include the LIC file.
This LIC file has led me to make a lot of detours on controls. The developed controls can run well locally (that is, register using register ActiveX Server in the run menu of C ++ builder ), however, once it is moved to another location, it will always fail. At the beginning, we do not know how to use the ActiveX control test container tool with VC ++ to test the generated OCX control.
Well, can I click the webdeploy menu? No. If we copy all the contents in the published directory to the MyApp directory of Tomcat and set the security level of IE to a minimum, we still cannot view the ActiveX control we just generated, A Red Cross is always displayed in the browser window. Truly frustrating
But don't give up. Let's proceed to step 1.
2. Implement IObjectSafety
In IE6 and later browsers, we must implement the IObjectSafety interface to ensure that the self-developed OCX control will not be disabled by IE.
How can this problem be achieved? It's actually very easy. Just add a few words.
① Open the header file of actfrm. We added a button to the form at the beginning. After opening the header file, we can see that a class tacfrm does not care about it, it has nothing to do with it. Next, let's look at it and we can see something similar.
Class atl_no_vtable tactfrmimpl:
For Code such as vclcontrol_impl (tactfrmimpl, actfrm ......), we can add an interface to it after brackets.
This is similar after adding
Class atl_no_vtable tactfrmimpl:
Vclcontrol_impl (tactfrmimpl, actfrm ......), Public iobjectsafetyimpl <tactfrmimpl, interfacesafe_for_untrusted_caller>
Be sure not to lose the comma before public. Multiple inheritance is required.
② Add the void initializecontrol () {} function after completion
Begin_category_map (tactfrmimpl)
Implemented_category (catid_safeforscripting)
Implemented_category (catid_safeforinitializing)
End_category_map ()
③ Begin_com_map (tactfrmimpl)
Vcl_control_com_interface_entries (iactivefrmx)
Com_interface_entry (IObjectSafety) // Add this line and it will be OK.
End_com_map ()
Compilation passed, and there should be no problem
3. Add a custom method (Interface)
The ActiveX control we make should be able to interact with scripts such as JavaScript, so we need to implement our own interface in the control.
Choose menu View> type library to display the TLB file browsing window. There are a lot of things in it, don't worry about it. Let's go straight to the topic and find iactfrm and right-click to create a method, it's called showvalue.
Add a parameter to the method on the parameters page. The parameter name uses longvalue and the parameter type uses variant, click the refresh button C ++ builder to automatically add a method named showvalue to us. The parameter types are all set. In this function, we add showmessage (longvalue. ival );
Then add the corresponding JS Code to the HTM file corresponding to OCX.
The default generated HTM file does not add an ID when loading the ActiveX control. We need to manually add it, assuming id = "myocx", and then add a JS Function
<Script language = "JavaScript">
Function testfun (){
Myocx. showvalue (12345 );
}
</SCRIPT>
Then add a button to the page to trigger the testfun function in The onclick event of the button.
4. Use ActiveX control test container for testing
ActiveX control test container is a good thing. Without it, I really don't know that there is a problem with the ActiveX control I have compiled.
This tool is installed with VC ++ and is easy to use. After opening it, select edit-> Insert new control and select an ActiveX control from the Control List, of course actfrmro is selected.
5. Implement Digital Signature
If the Browser allows you to download ActiveX controls at will, the story ends here. The fact is that to ensure security, the browser does not download the control without a signature. If the control does not have a signature, the browser will not give any prompt, and there is only a red cross on the browser.
IE is cruel.
It doesn't matter. If you DIY a signature, you can ask the browser to provide a download prompt box.
The following tools are required to create a signature: makecert signcode cert2spc and iexpress if you need to pack OCX by yourself. This tool is available in the Windows installation directory.
The first three tools can be obtained from Microsoft's official site
Manually write a BAT file (not used to repeatedly repeat commands) and place the BAT file in the F: \ projscts \ firstocx \ published directory.
Makecert/SV "actfrmproj1.pvk"/N "cn = company name, E = Email, O = Author" actfrmproj1.cer
Cert2spc actfrmproj1.cer actfrmproj1.spc
Signcode
To use these three commands, you must add the paths of these three files to the Windows environment variable path.
The first command generates two files: actfrmproj1.cer and actfrmproj1.pvk.
Step 2 convert actfrmproj1.cer into activeproj1.spc File
In this process, you will be asked to enter the password, and you can enter one by yourself. It doesn't matter if you don't remember it.
Step 3 is to give us the front of the generated cab file, and the graphic interface is easy to operate.
① Select the cab file automatically generated by BCB, which should be in the F: \ projscts \ firstocx \ published directory
② Select Custom signature type
③ Select a certificate from the file. There is no other choice. Only one file is the SPC file under F: \ projscts \ firstocx \ published.
④ Select the private key in CSP for the private key location
⑤ The key container selects actfrmp1_1.pvk. This file is the PVK generated in the first step.
6. You can select MD5 or SHA for the algorithm, and then browse other certificates. Select the generated actfrmproj1.cer.

Then copy all the files in the puulished directory to the MyApp directory and access them through a browser. The browser prompts you to check whether you want to install the ActiveX control, otherwise, isn't it a busy job?

6. ActiveX debugging skills
When we use BCB to develop controls, we will find that F9 cannot run because OCX works in a similar way as DLL files. We can solve this problem by setting runtime parameters, run-> paramters select IE, E. g: C: \ Program Files \ Internet Explorer \ iw.e. EXE
Parameters input: F: \ projscts \ firstocx \ published \ activefrmproj.htm
Press F9 to run it directly.

Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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