Step by step: New native development function in Visual Studio 2005

xiaoxiao2021-03-13  171

This page

Introduction Experience 1: Creating an ATL ActiveX Control Exercise 2: Creating a MFC ActiveX Host Application Summary

To complete this exercise, you need to have:

• Windows XP Professional. This experience requires Windows XP Professional. • Visual Studio 2005 This experience requires Visual Studio 2005 Standard Edition, Professional Edition, or Team System Edition. This experience will not be possible using any Express Edition. If you don't have the appropriate Visual Studio 2005 version, visit the Visual Studio 2005 Developer Center to learn how to get this version. • ActiveSync 4.0 can be connected to your computer based on Windows Mobile-based devices via ActiveSync 4.0. • Windows Mobile 5.0 SDK. By Windows Mobile 5.0 SDK for Pocket PC and Windows Mobile 5.0 SDK for Smartphone can in Visual Studio 2005 to develop Windows Mobile-based devices: Download and install the Windows Mobile 5.0 SDK for Pocket PC download and install the Windows Mobile 5.0 SDK for Smartphone Note: You use the emulator in the previous hands-on experience, you should first press the emulator before starting this experience. On the emulator, click File, point to RESET, and then click Hard (Hard Reset). If an error is received during the deployment, it means that the process or file is being used, which means that the program is still running on the emulator, and must first exit the program before deploying and running a new copy. This error may occur at any time in the experience of deploying the emulator. For instructions on exiting the running application, see the last task in this experience.

Introduction

Many typical problems should cover "What's New In Visual Studio 2005 for Native Developers" on MSDN and "Migrating Microsoft Embedded Visual C Projects) in two articles.

If you press the Ctrl Space button to use IntelliSense technology to complete a row code, the IDE may freeze. This is a known error; therefore, please do not press CTRL Spacebar.

The goal of this experience is to use Visual Studio 2005 to create an ATL ACTIVEX control for Pocket PC and the MFC host device application corresponding to the ActiveX control. The ActiveX control will be a triangle with a fill color, and when the control is clicked, the fill color changes. The corresponding host application will be a dialog-based application to reside the ActiveX control. In addition, you will also use a test certificate to sign the controls and host applications.

In this experience, you will do the following exercises:

• Create an ATL ActiveX Control • Create an MFC ActiveX host application

Back to top

Exercise 1: Creating an ATL ActiveX Control

In this exercise, you will use Visual Studio 2005 to create an ATL ActiveX control for Pocket PC.

Steps to complete the ATL Intelligent Device Project Wizard

1. In Visual Studio 2005, click File | New | Project (File | New | Project). 2. Expand Visual C , then select Smart Device. 3. Select ATL Smart Device Project (ATL Smart Device Project) and type CTL1 in the Name box. 4. Click OK. 5. In the ATL SMART Device Project Wizard page, select Platforms (Platform). 6. Make sure Windows Mobile 5.0 Pocket PC SDK and Pocket PC 2003 are located in the Selected SDKS area. If they are not in this area, select them in the Installed SDKS (installed SDK) area, then click Add SELECTED Platform SDK To Project (add the selected Platform SDK to the project) button. 7. Select Application Settings. 8. Make sure to select Dynamic-Link Library (Dynamic Library [DLL] under Server Type and select any check box in Additional Options. 9. Click Finish.

Steps to create an ActiveX class

1. In the Solution Explorer (Solution Explorer), right-click the Ctrl1 project, and then click Add | Class (Add | Class). 2. Expand Visual C , then select SmartDevice. 3. Under Visual Studio Installed Templates, select ATL Control (ATL Control), then click Add. 4. On the Names page, type MyCTL in the SHORTNAME (Abbreviation) box. 5. Select STOCKPROPERTIES. 6. In the NOT Supported list, select FillColor (Fill Colors). 7. Click Add Selected Property to The Supported List (Add the selected properties to the supported list) button to add it to the Supported list. 8. Click Finish. Please do not change any other options.

Steps to add attributes to the ActiveX class

1. Click the Class View tab and expand CTL1. 2. Right-click iMyCTL, click Add, and then click AddProperty. 3. In the PropertyType box, select SHORT. 4. In the PropertyName box, type SIDES. 5. Click Finish. Steps to add code to the ActiveX class

1. In the Solution Explorer (Solution Explorer), double click MyCTL.H. 2. Find the following code at the end of the iMyctl interface. PUBLIC:

STDMETHOD (Get_SIDES) (Short * PVAL);

PUBLIC:

STDMETHOD (PUT_SIDES) (SHORT NewVal);

3. Add the following code to the code in step 2. These members are used to draw the edges of the control. Private:

Short m_nsides;

Point M_Arrpoint [100];

INT M_CurrentColor;

4. Add the following code to the CMYCTL () constructor main body. This will initialize the control to have three edges and a fill color. M_CurrentColor is used to track the current color so that the control can be switched to other colors when you click on the control. M_NSIDES = 3;

m_clrfillcolor = RGB (0, 0xFF, 0);

m_currentcolor = 2;

5. Delete the main body of the onDraw (ATL_DRAWINFO & DI) method. 6. Then add a semicolon at the end of the method (because you will perform your own onDRAW). The onDraw method should be as follows. HRESULT OnDRAW (ATL_DRAWINFO & DI);

7. Insert the following message handler in the message mapping segment: Begin_MSG_MAP (CMYCTL). This code allows the ActiveX control to handle your users accordingly and respond. Message_handler (WM_LButtondown, ONLBUTTONDOWN);

8. The full message mapping segment of the CMYCTL class should be as follows. Begin_msg_map (CMYCTL)

Message_handler (WM_LButtondown, ONLBUTTONDOWN);

Chain_msg_map (ccomcontrol )

Default_reflection_handler ()

END_MSG_MAP ()

9. Add the following processing program prototype below the MessageMap section. Lresult Onlbuttondown (Uint, WPARAM, LPARAM, BOOL &)

{

Switch (m_currentcolor)

{

Case 1:

m_currentcolor = 2;

m_clrfillcolor = RGB (0, 0xFF, 0);

Break;

Case 2:

m_currentcolor = 1;

m_clrfillcolor = RGB (0xFF, 0xFF, 0);

Break;

}

PUT_FILLCOLOR (M_CLRFILLCOLOR);

Return 0;

}

10. Open myctl.cpp. 11. Copy and paste the following code to myctl.cpp. This code is used to draw an ActiveX control. #define GSC_Mask 0x80000000l # define GSC_SetBit 0x04000000L

Inline HRESULT WCE_OLETRANSLATECOLOR (OLE_COLOR CLR, HPALETTE,

ColorRef * RETCLR)

{

* RETCLR = (CLR & GSC_MASK)? GetsysColor (CLR & (~ GSC_MASK) |

GSC_SETBIT): (CLR & 0x00FFFFF);

Return S_OK;

}

HRESULT CMYCTL :: OnDraw (ATL_DRAWINFO & DI)

{

Rect & rc = * (Rect *) di.prcbounds;

HDC HDC = DI.HDCDRAW;

Colorref colore;

Hbrush Holdbrush, Hbrush;

HPEN HOLDPEN, HPEN

// Convert M_ColFore to ColorRef Type

WCE_OLETRANSLATECOLOR (M_Clrfillcolor, Null, & Colter);

// Create and select color to draw rounds

HPEN = (HPEN) GetStockObject (Black_pen);

Holdpen = (HPEN) SELECTOBJECT (HDC, HPEN);

Const Double Pi = 3.14159265358979;

Point ptcenter;

Double dblradiusx = (rc.right - rc.left) / 2;

Double dblradiusy = (rc.bottom - rc.top) / 2;

Double dblangle = 3 * pi / 2; // Starting from the top

Double dbldiff = 2 * pi / m_nsides; // Make each side rotation angle

// Make

PTCenter.x = (rc.left rc.right) / 2;

PTCenter.y = (rc.top rc.bottom) / 2;

// calculate the point corresponding to each side

For (INT i = 0; i

{

M_arrpoint [i] .x = (long) (dblradiusx * cos (dblangle) ptcenter.x 0.5);

M_ARrpoint [i] .y = (long) (dbladiusy * sin (dblangle) ptcenter.y 0.5);

DBLANGLE = DBLDIFF;

}

// Create and select the // brush that will be used to fill polygons

Hbrush = Createsolidbrush (ColFore);

Holdbrush = (Hbrush) SelectObject (HDC, Hbrush);

SelectObject (HDC, Hbrush);

Polygon (HDC, & M_ARRPOINT [0], M_NSIDES);

// Select back to the original pen and brush, remove our created // brush

SelectObject (HDC, HoldPen);

SELECTOBJECT (HDC, Holdbrush);

DeleteObject (Hbrush);

Return S_OK;

}

12. Find the following attribute get functions. STDMETHODIMP CMYCTL :: GET_SIDES (SHORT * PVAL) 13. The following code is then added to the function main body (before return S_OK) * pVAL = m_nsides;

14. Find the following attributes PUT functions: stdmethodimp cmyctl :: put_sides (short newval)

15. The following code is then added to the function main body (before return S_OK) Atlassert (NewVal> 2 && NewVal <101);

m_nsides = newval;

Fireviewchange ();

16. Open stdafx.h. 17. Adding the following code below #pragma overce near STDAFX.H. This code will prevent ATL 8.0 for Devices from being thrown into the project when compiling projects for non-DCOM platforms. #define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA

18. Click Build, and then click BuildSolution to generate a project. Make sure there is no compilation or link error.

Steps to sign the ActiveX project

1. In the Solution Explorer (Solution Explorer), right-click the CTL1 project, and then click Properties. 2. Expand Authenticode Signing (Authenticode Signature) and select General (General). 3. Change the Authenticode Signature (Authenticode Signature) property to Yes. 4. In the Certificate list, click the ○ 号. 5. In the SELECT CERTIFICATE dialog box, click ManageCertificates (management certificates). 6. In the ManageCertificates dialog box, click Import. The Certificate Import Wizard will open. 7. Click Next. 8. In File To Import, click Browse. 9. Browse to C: / Program Files / Microsoft Visual Studio 8 / SmartDevices / SDK / SDKTools / TestCertificates. 10. In the FILES OF TYPE box, change the file type to Personal Information Exchange (* .pfx, *. P12), as shown below.

11. Select TestCert_Privileged. 12. Click Open. 13. In File To Import, click Next. 14. Keep your password is empty, then click Next. 15. Make sure the Place All Certificates In The Following Store (place all certificates in the following store) option button, and set the Certificate Store to Personal. 16. Click Next. 17. Click Finish. 18. When the Import WAS SUCCESSFUL appears, click OK. 19. Turn off the Managed Certificate dialog. 20. In the SELECT CERTIFICATE dialog, you should now see the Smartphone 2003 Privileged Test certificate, select the certificate, and then click OK. This certificate should be used to sign each time you generate the project. 21. On the CTL1 Property Pages (page), select the Privileged Certificate Store in the Provision Device list, as shown below. 22. Click OK.

Each time you deploy the project, you will be configured for your device's Privileged Certificate Store.

Steps to deploy and register an ActiveX control

• In the Solution Explorer (Solution Explorer), right-click the CTL1 project and select Deploy. The emulator will open and deploy and register an ActiveX control.

Back to top

Exercise 2: Create an MFC ActiveX host application

In this exercise, you will use Visual Studio 2005 to create an MFC application for Pocket PCs to resign an ActiveX control created in this experience 1.

Steps to complete the MFC Intelligent Device Project Wizard

1. In a solution including a CTL1 project, right-click the solution, click Add, and then click NewProject. 2. Expand Visual C , then select SmartDevice. 3. Select the MFC Smart Device Application (MFC Smart Device Application), and then type type myhostapp in the Name box, as shown below.

4. Click OK. The MFC Intelligent Device Application Wizard will open. 5. Select Platforms. 6. Make sure the Pocket PC 2003 and Windows Mobile 5.0 Pocket PC SDK are located in the SELECTED SDKS area. If they are not in this area, select them in the Installed SDKS (installed SDK) area, then click Add Selected Platform SDK to Project Button (add the selected Platform SDK to the project) button. 7. Select Application Type. 8. Change the ApplicationType to DialogBased. 9. Select AdvancedFeatures. 10. Check the ActiveX Controls check box. 11. Click Finish. Steps to add ActiveX controls to dialog

1. In the Solution Explorer (Solution Explorer), double-click MyCTL.RGS (under the CTL1 Project ResourceFiles Node). 2. Select MyCTL Class CLSID and press Ctrl C (make sure the braces are selected), as shown below. Note that the GUID shown in the project is different from the figure.

3. In the Solution Explorer (Solution Explorer), double-click MyHostAppPPC.RC. 4. Expand myhostapp | myhostapppppc.rc | dialog. 5. Double-click IDD_MYHOSTAPP_DIALOG. 6. In the Dialog Editor (dialog editor), select the default static text control (as shown below), then press the DELETE button.

7. From the Toolbox (Toolbox), drag the CustomControl (Custom Control) to the top left corner of the dialog. 8. Click in the lower right corner of the custom control, and then adjust its size to occupy the entire dialog, as shown below.

9. Right-click the custom control and select Properties. 10. In the Properties window of the custom control, click the Class property field and press Ctrl V. The ActiveX control MyctL class CLSID will be pasted from the clipboard. 11. In the Solution Explorer (Solution Explorer), right-click MyHostAppPPC.RC2 and select ViewCode. 12. In the Add Manually edited resources here, add the following code to the resource from here. The custom control must be displayed correctly with the Dialog Init segment. But do not use the content of the actual Dialog Init section. IDD_MYHOSTAPP_DIALOG DLGINITBEGIN

IDC_CUSTOM1, 0x376, 22, 0

0x0000, 0x0000, 0x0800, 0x0000, 0x094D, 0x0000, 0x043D, 0x0000, 0x0013,

0xcdcd, 0xcdcd,

0

End

Steps to sign the ActiveX project

1. In the Solution Explorer (Solution Explorer), right-click the MyHostApp project, then select Properties. 2. Expand Authenticodesigning (Authenticode Signature) and select General (General). 3. Change the Authenticodesignature (Authenticode Signature) property to Yes (yes). 4. Click Certificate and select the omitted button. 5. In the SelectCertificate dialog box, select the SmartPhone 2003 Privileged Test certificate, and then click OK.

• You should use this certificate to sign it each time you generate this item. 6. Select ProvisionDevice and click Privileged Certificate Store, as shown below.

7. Click OK.

Each time you deploy the project, you will be configured for your device's Privileged Certificate Store.

Steps to deploy solutions

1. In the Solution Explorer (Solution Explorer), right-click the MyHostApp project, and then click Set As Startup. 2. Press F5. 3. After the dialog appears, click the control, pay attention to the change in color.

The following tasks illustrate how to terminate an application running on a device or emulator. This task is used if the application is launched without additional debuses and requires the application to deploy a new application copy. The application will be terminated by using the remote process viewer using the remote process viewer in Visual Studio.

Need to know the name of the executable file to terminate the process. In most cases, this name is the name of the Visual Studio project. If you are not sure the name of the executable file, you can find it in the project properties. Steps to terminate applications running on the device or emulator

1. From Visual Studio, select Project, then select the XXX Properties, where XXX represents the name of the current project. 2. Note the value in the Assembly Name field. This value is the name that will be used when running on the device or emulator. 3. Turn off the Properties dialog. Now you can terminate the process. 4. From the Start menu, click Start> Microsoft Visual Studio 2005> Visual Studio Remote Tools> Remote Process Viewer (Start> Microsoft Visual Studio 2005> Visual Studio Remote Tool> Remote Process Viewer). 5. After receiving the SELECT A Windows CE Device Dialog, select the emulator or device that is running the application (as shown below), then click OK.

6. After connecting to the emulator or device, find the application you want to terminate in the top pane of the Remote Process Viewer, as shown below.

It may be necessary to widow the Process (process) column (the leftmost column) to fully display the process name. 7. Click the process name to select the process. 8. To terminate the process, select File> Terminate Process from the RemoteProcess Viewer menu. Note: Before you click TerminateProcess, make sure you have selected the correct process. The incorrect process may cause the device or emulator to be unavailable, so it must be reset to be used again. 9. By selecting Target> Refresh on the Remote Process Viewer and scroll through the top pane again, the top pane is scrolled, verify that the process has been terminated. If the application name still exists, the process will not be terminated, you need to repeat the above steps. Note: Most processes can be terminated by simply performing an operation; however, according to the application status, occasionally need to perform two operations to terminate.

Back to top

to sum up

In this experience, you have implemented the following exercises.

• Create an ATL ActiveX Control • Create an MFC ActiveX host application

In this experience, you created an ATL ActiveX control by using the Visual Studio 2005 app and class wizard. It also created a dialog-based MFC application to reside the control and use resource editor and custom controls to provide an Design to an ActiveX host. In addition, two items are signed with a test certificate, which is also configured to the device, allowing applications (and controls to run on "privilege" mode on Microsoft Windows Mobile with secure models. . Go to the original English page

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

New Post(0)