Wednesday, 28 April 2010

Operators ( Example Console Program using Delphi )

This is an example program using operators

program Using_Operators;

{$APPTYPE CONSOLE}
uses
SysUtils;

begin
   // Insert user code here
   writeln('7 + 8 = ', 7 + 8);
   writeln('7 - 8 = ', 7 - 8);
   writeln('7 x 8 = ', 7 * 8);
   writeln('15 : 3 = ', 15/3:0:0);
   { :0:0 is for formatting. You should try to ommit it }
   { and see what happend at the result }
   writeln('17 mod 4 = ', 17 mod 4);
   writeln('17 div 4 = ', 17 div 4);
   writeln('3 + 2 x 4 = ', 3 + 2 * 4); // see the different between
   writeln('(3 + 2) x 4 = ', (3 + 2) * 4); // these two expressions
   writeln(8 shr 1); // logic operator
   writeln(8 shl 1); // logic operator
   writeln(1 = 2); // boolean operator
   writeln(not (1 = 2)); // boolean operator
   writeln(7 >= 2); // relational operator
   writeln('a' < 'b'); // relational operator
   writeln('123delphicodes' = '123
delphicodes'); // string operator
   writeln('123
DelphiCodes' = '123delphicodes'); // string operator
   writeln('123
DelphiCodes' < '123delphicodes'); // string operator
   readln; // wait until enter button is pushed
end.

Delphi Tutorial # 7

Operators

Operator is a symbol to do some operation, such as : sum, product, division, compare etc. Operator involves two or more operands. Operator and operand form an expression. Example :

    12 + 15 = 27

see the table below :

OperatorsOperands
+12
=15

27

arithmetic operators

Operator
*
/
div
mod
+
-

logic operators

Operator
not
and
or
xor
shl
shr

boolean operators

Operator
not
and
or
xor

relational operators 

Operator
>
<
>=
<=
<>
=

string operator

all relational operators also can be use for string :

Operator
>
<
>=
<=
<>
=

In the example, I will show you how to use these operators.

Go to Previous Tutorial or Next Tutorial

Friday, 23 April 2010

Delphi Tutorial # 6

Console Application 

Before we go further with visual programming, it is important to understand the basic element of Delphi. In the first tutorial about, I have mention that a console application is a DOS (Disk Operating System) application. It is a non Windows application. Using console programming, you can focus on the codes only. Meanwhile you can forget about visual programming, and learn about Pascal language.

To make a console application, choose File – Close All (if thee is still a form in IDE). Then choose File – New, select Console Application. Delphi will show Project1.dpr window :

Declare constants and variables after uses SysUtils;

  const

   length : integer = 12;

   width : integer = 8;

  var

   area : integer;

Insert user code between begin …. end.

  area := length*width;

  writeln('Length of rectangle = ',length);

  writeln('Width of rectangle = ',width);

  writeln('The area = ',area);

  readln;

The complete codes of Project1.dpr :

  program Project1;

  {$APPTYPE CONSOLE}

  uses

   SysUtils;

  const

   length : integer = 12;

   width : integer = 8;

  var

   area : integer;

  begin

   // Insert user code here

   area := length*width;

   writeln('Length of rectangle = ',length);

   writeln('Width of rectangle = ',width);

   writeln('The area = ',area);

   readln;

  end.

Run the program, and the result is :

The last code readln; is for delaying program until you press Enter button.

Comment

// Insert user code here is an example of comment. Comment can be written in three ways :

-         Written between { }

-         Written between * *

-         Begun with // like the example above

Assigning a value to a variable

From the program above, we assign a value to area variable using assignment operator :=. The syntax is :

    variable := value;

so the code is :

    area := length*width;

Go to Previous Tutorial or Next Tutorial

Thursday, 22 April 2010

Showing the Hidden Files in Windows XP

Windows hides the important system files. But Some of hidden files are dangerous to our system. And we can call them viruses. Here is the way to make all hidden files shown : Run windows explorer by clicking the mouse right button on Start Menu. Then choose Explore. 

Choose Tools – Folder Option, like the picture above. The Folder Option window will appear :

Choose Show hidden files and folders. Remove the checklist form Hide extensions for known file type and Hide protected operating system files (Recommended). Click Apply, then OK. Now all hidden files will be shown.

Wednesday, 21 April 2010

Enabling Turn Off Autoplay

Windows provides Autoplay feature to examine a removable media that newly discovered by the system. It is closely like AutoRun feature. Removable media can be a flashdisk, a CD, a DVD, a memory card or an external hard disk etc.

But this feature has a disadvantage. If the removable media connected to the computer is infected by some viruses, the virus will be automatically loaded to memory, or even your system will be automatically infected. Virus is usually an executable hidden file. And in a certain way will be automatically executed when a media or a directory / folder  is being connected to the system. Usually by using an autorun.inf file.

To avoid this, we have to enable Turn Off Autoplay feature. From Start Up menu, choose Run. Type gpedit.msc then click OK.

The Group Policy window will appear :

There are two configuration : Computer Configuration and User Configuration. Form Computer Configuration, choose Administrative Templates - System, then choose Turn off Autoplay as the picture shown below :

Then Turn off Autoplay Properties window will appear. Choose Enabled and All drives. Then click Apply – OK :

You also have to do these steps for User Configuration. Choose Administrative Templates - System, then choose Turn off Autoplay  as the picture shown below :

Choose Enabled and All drives. Then click Apply – OK.

Once the Turn Off Autoplay feature has been enabled, just remember do not ever double click a folder or a media that is infected by viruses. It is more save to use Windows Explorer, by clicking the mouse right button on Start Menu, then choose Explorer. In this way, the hidden viruses will not be executed. It will decrease the risk of viral infection to your system. Then all you have to do is deleting suspicious hidden files. But before you can do this, you have to make all the hidden files shown.

Wednesday, 14 April 2010

Delphi Tutorial # 5

Program Structure

A program is composed of three elements :

- Program Title

- Uses Clauses

- Declarations and Statements

Program title is the name of the program. Uses clauses include units that are used in the program. Declarations and statements are codes that will be executed by computer.

You can see an example of Delphi program structure in Delphi Tutorial # 2. See the codes project1.dpr file  below :

    program Project1;

    uses

      Forms,

      Unit1 in 'Unit1.pas' {MyForm};

    {$R *.RES}

    begin

      Application.Initialize;

      Application.CreateForm(TMyForm, MyForm);

      Application.Run;

    end.

The example above does not have declarations. Declaration is where we define constants, types, variables, procedures and functions. The structure of declarations is shown below :

     Const

           {constants list}

     Type

          {types create by user}

     Var

          {variables list}

          {functions and procedures}

Function is a routine that can be called by the program and will return a value. Procedure is a routine that does not return a value.

A program is usually formed by some source code modules that called units. A unit consist some routine (functions and procedures). It is saved as a unit file (.PAS). The structure of a unit is shown below :

       Unit unitname;

       Interface 

       Uses {units}

       {interface}

       Implementation

       Uses {units}

      {implementation}

       Initialization

       {Initialization}

       Finalization

       {Finalization}

       End.

The example of a unit is unit1.pas file :

    unit Unit1;

    interface

    uses

      Windows, Messages, SysUtils, Classes, Graphics, Controls,           Forms, Dialogs, StdCtrls;

    type

      TMyForm = class(TForm)

        Label1: TLabel;

        ExitButton: TButton;

        procedure ExitButtonClick(Sender: TObject);

      private

        { Private declarations }

      public

        { Public declarations }

      end;

    var

      MyForm: TMyForm;

    implementation

    {$R *.DFM}

    procedure TMyForm.ExitButtonClick(Sender: TObject);

    begin

     Application.Terminate;

    end;

    end.

Go to Previous Tutorial or Next Tutorial

Monday, 12 April 2010

Click and Drag Mouse to Draw a Line Using Delphi

To draw a line with click and drag method, we use some events related with mouse : OnMouseDown, OnMouseMove and OnMouseUp. OnMouseDown is an event when we push the left mouse button. OnMouseMove is when we move the cursor. And OnMouseUp is when we release the mouse button. First, prepare a user interface ( a blank form and a button) :

Now, you have to decide what action should be done on each event ( OnMouseDown, OnMouseMove and OnMouseUp ) :

EventAction
OnMouseDownget the x and y start point
OnMouseMove - delete the old line
- draw the new line
OnMouseUp- draw the line
- finish drawing

The complete source code is listed below :

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls;

type

  TForm1 = class(TForm)

    Button1: TButton;

    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;

      Shift: TShiftState; X, Y: Integer);

    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,

      Y: Integer);

    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;

      Shift: TShiftState; X, Y: Integer);

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

  Drawing : boolean;

  StartX, StartY, EndX, EndY : integer;

implementation

{$R *.DFM}

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);

begin

 Drawing := true;

 StartX := x;

 StartY := y;

 EndX := x;

 EndY := y;

end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

  Y: Integer);

begin

 if Drawing then

  begin

   Canvas.Pen.Mode := pmNotXor;

   Canvas.MoveTo(StartX,StartY);

   Canvas.LineTo(EndX,EndY);

   Canvas.MoveTo(StartX,StartY);

   Canvas.LineTo(x,y);

  end;

 EndX := x;

 EndY := y;

 Canvas.Pen.Mode := pmCopy;

end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);

begin

 Canvas.MoveTo(StartX,StartY);

 Canvas.LineTo(x,y);

 Drawing := false;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 Application.Terminate;

end;

end.

Run the program, and now you can click and drag your mouse to draw a line.

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.