Creating CF Applications and Integrating a Smart Device Emulator with Delphi - Creating a CF application from within the Delphi IDE
(Page 4 of 4 )
The entire process I laid out above is a bit complicated and even unnecessary if you are using Delphi 2006. In this version all you need to do is change the search path in the environment option to point to the directory which contains the CF assemblies. Do that and you can use familiar Delphi features like code completion and error insight while coding.
Now create a new WinForm application and drop a button on the form. Go to project menu and click compile; it will fail and a bunch of errors will be thrown. But that's nothing to panic about; it was expected since we were trying to compiling a full .NET application with CF libraries. Remember, not all .NET classes and methods are supported in Compact Framework.
Switch to the code editor and comment out or delete the codes that cause errors during compilation. Some of the properties of the Label component like Name, TabIndex, FlatStyle, and so forth are not supported in Compact Framework, hence they need to be removed. (Note: it would be pointless to list all properties of a component here; you can find that out yourself or refer the help file from Microsoft). After you have modified the main unit file, commenting out the unsupported methods/properties, there is yet another line, "STAThread," which you need to comment out in the project file.
The modified code looks like this (code sections in gray are unsupported methods/properties; therefore they are commented out):
procedure TWinForm.InitializeComponent;
begin
Self.Label1 := System.Windows.Forms.Label.Create;
Self.Button1 := System.Windows.Forms.Button.Create;
//Self.SuspendLayout;
//
// Label1
//
// Self.Label1.FlatStyle := System.Windows.Forms.FlatStyle.Popup;
Self.Label1.Location := System.Drawing.Point.Create(64, 104);
// Self.Label1.Name := 'Label1';
Self.Label1.Size := System.Drawing.Size.Create(160, 24);
// Self.Label1.TabIndex := 1;
Self.Label1.Text := 'Click On the Button ';
//
// Button1
//
Self.Button1.Location := System.Drawing.Point.Create(88, 152);
// Self.Button1.Name := 'Button1';
// Self.Button1.TabIndex := 0;
Self.Button1.Text := 'Button1';
Include(Self.Button1.Click, Self.Button1_Click);
//
// TWinForm
//
// Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
Self.ClientSize := System.Drawing.Size.Create(292, 266);
Self.Controls.Add(Self.Button1);
Self.Controls.Add(Self.Label1);
// Self.Name := 'TWinForm';
Self.Text := 'WinForm';
// Self.ResumeLayout(False);
end;
{$ENDREGION}
procedure TWinForm.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;
constructor TWinForm.Create;
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
self.MinimizeBox:=False; // exit application instead of minimizing
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
Label1.Text:='Hello from Pocket PC';
MessageBox.Show('Hello from Pocket PC !');
end;
Now compile the application, copy it to the shared folder and switch to emulator. The executable should now be accessible from the storage device of the emulator. Click on it to execute it and the application launches successfully, displaying the label caption and a button. Click on the button and a message box pops up displaying the text "Hello World from Pocket PC!"

You have finished creating your first .NET CF application in Delphi (even if it is as simple as a "hello world" application) but if you go back to modify it using the visual IDE and drop another component, the codes that we removed to compile successfully are generated once again and you have to remove them manually. Unfortunately I haven't yet found a way to stop these lines from being generated every time the code is modified. So, knowing which classes and members are available in CF can help you avoid using the designer while coding.
You can download the Class Library Comparison Tool which provides a quick reference to identify which classes and members are available in the .NET Compact Framework. Further, there are some additional classes and members in the Compact Framework which are not supported by the .NET Framework. The list of these classes can be accessed here: Classes Exclusive to the .NET Compact Framework .
Support for the Compact Framework in Delphi is a major enhancement but I really hope that Delphi extends its biggest strength, its VCL support to CF application development, in the near future.
Danish Ahmed
Mindfire Solutions
http://www.mindfiresolutions.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |