Programming for Windows CE

Programming for Windows CE STEFS Fall 2001 Seminar Lecture 4 9/21/01 Introduction Last week, we discussed how to make programs simple for the user...
Author: Roy Curtis
14 downloads 0 Views 108KB Size
Programming for Windows CE STEFS Fall 2001 Seminar

Lecture 4

9/21/01

Introduction Last week, we discussed how to make programs simple for the user, what pitfalls to avoid and some tools to plan out the software design. This week, we will learn the basics of Visual Basic, a simple programming language for developing applications for Windows desktops, labtops and hand-helds.

Review Questions:  What sort of designs should we avoid?      

invisible arbitrary impolite inconsistent dangerous

What is the motto?  “Simple is better”



Other key learnings?  Make programs flow easily  Make them understandable  Standardize functionality

What is programming? Programming is simply the creation of computer code to execute a given task. The computer program is written in a specific language that sets the commands to be used and the structure of the code. The code is interpreted by a compiler that translates it to a set of instructions carried out during program execution.

There are MANY computer languages tailored to specific tasks and whose structure is more suited for particular types of programming. Examples: 







JAVA is a useful programming language for developing graphics to run on ANY platform or embedded within a website (JAVA applet) FORTRAN is a useful programming language for scientific computing tasks that require an efficient compiler. Visual Basic is a useful language for rapid prototype development on Windows computers. HTML is a programming language used to create websites that may include graphics from a simple text format.

Why do we program? For many scientists and engineers programming is an indispensable necessity. For environmental engineers, programming can be a useful tool for many tasks:  



Analyzing time-series data. Creating code to facilitate repetitive computation. Computer modeling of:    



Hydrology/hydraulics Wastewater Chemical reactions Contaminant transport

Developing new software tools or models for environmental applications.

Each new programming language or computer model you learn adds to your value as an engineer. Think about the tools that you already use: 



MATLAB is a C-like scripting language for scientific computations and visualization. C/C++ programming is a good starting point to other languages that are similar in construct (JAVA, Python)

Think of the tools that you can still learn to use. The more you learn, the easier it becomes to learn new ones.

Visual Basic (VB) Visual Basic is a Microsoft product that combines a method for rapid GUI design with the commands of the BASIC language. Types of Visual Basic:  

 



Visual Basic 6.0 (desktop) eMbedded VB 3.0 (Windows CE handheld) Visual Basic.NET Visual Basic for Applications (VBA) VB script

Visual Basic is an extremely popular, easy-to-use product for rapid development of GUI environments in Windows.

Examples of programs created in Visual Basic:  Microsoft Office (Word, Excell, PowerPoints, etc)  ArcView, ArcInfo, ArcPad  Other Windows-based software suites You too can create cool graphics and functional programs for Windows and for the PocketPC with Visual Basic!

Example of an Application ENVIT-Convert  A PocketPC application for Unit Conversions  Custom Tailored for Environmental Engineering  Water Quality  Hydrology  Limited to 1 Form, 1 Frame and about 6 controls  Currently does temperature converts:  C to F  F to C

Time and Effort Summary 



   

Written in VB using eMbedded VB 3.0 for PocketPC Emulation Total Development Time ~ 3 man hours Total Cost ~ 0 US$ Total Headaches ~ 2 Total # of subroutines = 9 # Global variables = 2

Simple, expandable code Some error checking incorporated

Dissecting the Example Code Forms and Controls:  1 Form  1 Frame  3 Combo Boxes  1 Label  2 Text boxes  2 Command Buttons  1 Message Box  1 Menu Bar with 1 Item

Programming elements:  Declare Integers, Doubles  If...Then...Else  Select Case  Utilize the Control Object properties:  Text  ItemData 

Respond to Events  Click  MenuClick  Load

Characteristics of VB Object-oriented:  Visual Basic is based on a set of predefined objects. Examples:  Combo Box object  Frame object



Objects have properties and methods.

Event-driven:  Events handlers determine how an object responds to a user action (an event)  An important part of the coding in VB is responding to events through coding.  Examples:

 Properties (Name,

BackColor, Font, Text)  Methods (Clear, Move, Load, AddItem) 

Think of properties as the descriptors of an object and methods and actions taken.

 Click event  GotFocus event 

Think of events as a programmers reaction to what a user does with the PocketPC. Usability design is foremost when responding to events.

VB Programming 1 When learning a new language, you want to familiarize with: 

   

Data Types and Variable Declarations Arrays of variables Subroutines and Functions Control Flow Statements Loop Statements

In the case of VB, one also wants to get familiar with the VISUAL design process.  



Hides the creation of graphics Utilizes existing objects seamlessly Provides quick access to object properties, methods, code

Unlike other languages, VB is not highly structured   

No Main function No header files No explicit object definition

Instead, VB code is a mostly collection of procedures that respond to events generated by the set of control objects embedded in the form. Extending the provided objects requires either: 





WIN32 API (Applications Programming Interface) OLE (Object Linking and Embedding) ActiveX Controls

VB Programming 2 Data Types and Variable Declarations:  

  

Strings (stores text) Numeric (stores number)  Integers  Doubles Boolean (True or False) Date (stores date) Variant (generic storage)

Declarations:   



Dim x As Int (x = 1) Dim y As Double (y = 2.5) Dim nm As String (nm = “Joe”) Dim x, y, name (Generic variables x = 1 or x = Joe)

1-D, N Arrays:  An array is structure for storing multiple variables.  VB supports multidimensional arrays and dynamic allocation. Declarations:  

Dim Names(10) As String Dim Matrix(x,2*x) As Double

Variables in VB also support Private, Public and Static Keywords that relate to the scope access.

VB Programming 3 Procedures are the small segments of code written in VB to respond to events:  Subroutines  Functions Subroutine Structure: Private Sub Butn_Click() ... End Sub

Subroutines are used as event handlers and DO NOT need to return any value. Other subroutines can be embedded within a particular event-handling subroutine.

Function Structure: Function Sum(x As Double, y As Double) As Double Sum = x + y End Function

Functions are used for coding operations. The type of the return variable (same name as Function) must be specified. Arguments need to be passed to Functions either by Value or by Reference. Function Add(n As Double) Function Add(ByVal n As Double)

VB Programming 4 Control Flow Statements are used to evaluate conditions and respond accordingly. This provides flexibility to the code. If...Then...Else If condition Then statement ElseIf condition2 Then statement2 Else statement3 End If

Select Case Select Case expression Case value1 statement1 Case value2 statement2 End Select

Loop Statements are used to execute code repeatedly For...Next 

For known number of repetitions using counter

Do...Loop 



For repetitions until a conditions is met

Do While...Loop  While condition is TRUE



Do Until...Loop  Until condition is TRUE

While...Wend  While condition is TRUE

Using IDE Tools What is IDE?  Integrated Development Environment simply implies that the compiler and editor work from the same application.  VB IDE provides quick and easy access to the elements of user interface, the object methods, properties and code.  VISUAL Design based on using the Forms and Controls available through the IDE

Elements of User Interface: Forms and Modules 



A Form is the workspace within which you place different graphics and controls A Module is a group of code that you want to apply to various Forms.

Examples of Built-in Controls    

Label Picture Box Combo Box Command Button

Compile and Debug Code Compiling code for other languages and platforms (UNIX) consists of special commands to the compiler and possible use of makefiles In VB, compilation is carried out within the IDE. For example, for eVB you can compile and execute the code on a PocketPC or Emulator. Program executes easily and returns any errors.

Debugging is also simplified within VB by using the integrated debugger. You can step through the various portions of the code to identify errors easily. VB and VC++ also provide a programmer with an enhanced set of tools for identifying errors. Toggling between errors and the code that generated it is much easier.

Help Pages and Manuals The Visual Basic Applications you will be using (VB 6.0 and eMbedded VB 3.0) have very useful help pages. Document object properties, methods and events. Define the various Visual Basic language keywords such as Dim, Public, ByVal etc. Internet Sites for Visual Basic and eVB developers:    

http://www.devbuzz.com http://www.studentdev.org http://www.dev.isinteresting.com http://msdn.microsoft.com/library

Several books are available to you through our collection:  Learn Visual Basic Now  VB 6.0 Programmers Guide  VB Tutorial CD MIT Libraries have many VB books.

Brief Introduction to VC++ C++ is a modern programming language used for many engineering applications. Visual C++ is a development environment for C++ programmers that provide many compiling debugging tools. There are some similarities between VB and VC++ but there are many more differences. Similarities:  

Object-oriented. Can use existing Microsoft objects or classes.

Differences:



VC++ less powerful for graphics and event-handling VC++ more powerful for computations. VC++ more expandable through classes. VC++ strictly object-oriented.



VC++ needs full coding.









VC++ supports class inheritance, polymorphism, etc

C++ gives the programmer more flexibility in creating custom-tailored codes.

Visual C++ Basics Why another language? 



VB is limited in some senses and more useful for GUI development VC++ allows us to develop other components like the Serial Port Communications.

C++ is a stricter language. Usually consists of two files: *.cpp (or *.C) and a *.h. Header files (*.h) used for describing the class definition, its data members and functions. Class files (*.cpp) used for expressing the functions operating on the data members.

When a class is compiled, it creates an object of that type. An object is used to store data and the functions that operate on this data. We can create, access, use and destroy this object in our program’s Main Function. Within the class functions, the execution of the code is much like C. The object-oriented part of the code are particular to C++. This is why C++ is a superset of C.

Visual C++ Basics VC++ contains various application wizards that enable a user to more easily set up the type of application desired. VC++ also contains class wizards that facilitate the creation of new classes and their data members and functions. Both VC++ and eMbedded VC++ are available.

More information on VC++ and C++ is available:  VC++ 6.0 Programmers Guide  MSDN Library: msdn.microsoft.com/library  Barker Library books.