VCL.Windows 10 SharingContract Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample shows how to use the RAD Studio ShareContract API to share content using the Windows 10 Share charm.

The sample shows the sharing of some strings and images. After a sharing request, Windows shows the sharing compatible applications in the Windows Share charm.

Location

You can find the Windows 10 SharingContract sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples, and then navigate to:
    • Object Pascal\VCL\Windows 10 SharingContract
    • CPP\VCL\Windows 10 SharingContract
  • Subversion Repository:
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

The sample consists of two projects with the same behaviour, ShareContractComponent that uses the TSharingContract component, and ShareContractFramework that uses the framework System.Win.ShareContract.

The demo shows how to transfer content to the Share charm on Windows 10. After starting the sharing process, the Share charm appears with the target applications that support the data being shared.

How to Use the Sample

  1. Navigate to the location given above, and open:
    • For Delphi: ProjectGroupSharingContract.
    • For C++: CPP_SharingContract.
  2. Select one of the projects from the Projects Window: ShareContractComponent or ShareContractFramework.
    Note: The two projects have the same functionality, one using the component, and the other using the framework.
  3. Select 32-bit Windows or 64-bit Windows as the target platform.
    Note: This sample is only compatible with Windows 10.
  4. Press F9 or choose Run > Run.
  5. Click Share.
  6. Choose an application to share the content with.

Implementation

The sample shows how to share content with another application, on Windows 10. The implementation of the sharing operation is done, either by using the TSharingContract component or by using the framework System.Win.ShareContract.

Content Format

This sample covers how to send shared content in a variety of formats, for example:

Note: You can define the properties for the visual component, either by code or using the Object Inspector at design time.

Initializing the Sharing Process

The demo starts sharing after clicking Share. The InitSharing method starts the process.

The InitSharing method is defined in the OnClick event of the Tbutton.

For Delphi:

  • Using the TSharingContract visual component
procedure TFShareComponent.ButtonShareClick(Sender: TObject);
begin
  (...)
  // Launch Sharing process. Shows applications that can receive our shared information.
  ShareContractComponent.InitSharing;
end;
  • Using the framework
procedure TFormSharingContract.ButtonShareClick(Sender: TObject);
begin
  (...)
  // Launch Sharing process. Shows applications that can receive our shared information.
  FShareWrapper.InitSharing;
end;

For C++:

  • Using the TSharingContract visual component
void __fastcall TFormSharingComponent::ButtonShareClick(TObject *Sender)
{
  (...)
  // Launch Sharing process-> Shows applications that can receive our shared information->
  SharingContract->InitSharing();
}
  • Using the framework
void __fastcall TForm1::ButtonShareClick(TObject *Sender)
{
  (...)
  // Launch Sharing process-> Shows applications that can receive our shared information->
  FShareWrapper->InitSharing();
}

Events

The OnAppChosen event triggers after selecting the target application to receive the shared information. In the sample, it adds a line to the TMemo with the name of the application.

For Delphi:

procedure TFShareComponent.ShareContractComponentAppChosen(const Sender: TObject; const AManager: IDataTransferManager;
  const Args: ITargetApplicationChosenEventArgs);
begin
  // With this event we can know which application is going to receive our data.
  Memo1.Lines.Add('Application Chosen: ' + args.ApplicationName.ToString);
end;

For C++:

void __fastcall TFormSharingComponent::SharingContractAppChosen(TObject * const Sender, IDataTransferManager * const AManager,
      ITargetApplicationChosenEventArgs * const Args)
{
  // With this event we can know which application is going to receive our data.
  Memo1->Lines->Add("Application Chosen: " + TWindowsString::HStringToString(Args->ApplicationName));
}

The OnTransferImage triggers when sharing an image.

Uses

See Also