Thursday, July 13, 2006

How to create a non-rectangular form

And how to do this? Here you will find a simple example that just gives some text and sets the region like it. Expand it by your mind!

unit uMainForm;

interface

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

type
TfrmMainForm = class(TForm)
btnDoAction: TButton;
pmnPopup: TPopupMenu;
miDrawText: TMenuItem;
miExit: TMenuItem;
procedure btnDoActionClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure miExitClick(Sender: TObject);
private
{ Private declarations }
HRgn: THandle;
public
{ Public declarations }
end;

var
frmMainForm: TfrmMainForm;

implementation

{$R *.DFM}

procedure TfrmMainForm.btnDoActionClick(Sender: TObject);
var
s: String;
begin
DeleteObject(HRgn);
s := InputBox('Region Text', 'Please enter some text to set to the region', 'CoolRgn');

BeginPath(Canvas.Handle);
with Canvas do
begin
Font.Name := 'Comic Sans MS'; Font.Size := 64; Font.Style := [fsBold];
TextOut(0, 0, s);
end;

EndPath(Canvas.Handle);
HRgn := PathToRegion(Canvas.Handle);
SetWindowRgn(Handle, HRgn, True);

btnDoAction.Visible := False;
Color := clRed;
end;

procedure TfrmMainForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
DeleteObject(HRgn);
end;

procedure TfrmMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
ReleaseCapture;
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
end;
end;

procedure TfrmMainForm.miExitClick(Sender: TObject);
begin
Application.MainForm.Close;
end;

end.

Saturday, June 24, 2006

Using Google Translate from Delphi

This is how to translate a text unsing Google Translate from Delphi:
For sending data to Google will use http GET method.


function translate(ss,lng:string):string;
var s:widestring;
a,b:integer;
http:tidhttp;
begin
http:=tidhttp.Create;
s:=http.Get('http://translate.google.com/translate_t?text='
+httpencode(ss)+'&langpair='+lng);
a:=posex('›',s,pos('‹textarea',s));
b:=posex('‹/textarea›',s,a);
result:=copy(s,a+1,b-a-1);
http.Free;
end;


where ss is the text to be translated and lng is the string that tell
Google from and to what language to translate.
Here is some definiton of lng:


lng:array[0..17]of string=('zh-CN%7Cen',
'en%7Czh-CN',
'en%7Cfr',
'en%7Cde',
'en%7Cit',
'en%7Cja',
'en%7Cko',
'en%7Cpt',
'en%7Ces',
'fr%7Cen',
'fr%7Cde',
'de%7Cen',
'de%7Cfr',
'it%7Cen',
'ja%7Cen',
'ko%7Cen',
'pt%7Cen',
'es%7Cen');

Friday, February 17, 2006

Making life easier with Indy

Let's take a look at the networking in Delphi.
One of the best library for writing network applications in Delphi is with no doubt Indy. It has almost everything someone wished for internt programming.

In the begining because today Google makes the net goes round ;) , let see how can use pop3 for retrieving mails from your Gmail account.

Drop on your form: a button, a listbox, one TIdPOP3, one TIdSSLIOHandlerSocketOpenSSL and one TIdMessage. Make the set the components that .dfm look like this:

object Form1: TForm1
Left = 0
Top = 0
Width = 420
Height = 378
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 16
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ListBox1: TListBox
Left = 32
Top = 72
Width = 345
Height = 265
ItemHeight = 13
TabOrder = 1
end
object pop: TIdPOP3
IOHandler = IdSSLIOHandlerSocketOpenSSL1
AutoLogin = True
Host = 'pop.gmail.com'
Username = 'youruser@gmail.com'
UseTLS = utUseImplicitTLS
Password = 'yourpassword'
Port = 995
SASLMechanisms = <>
Left = 208
Top = 16
end
object msg: TIdMessage
AttachmentEncoding = 'MIME'
BccList = <>
CCList = <>
Encoding = meDefault
FromList = <>
Recipients = <>
ReplyTo = <>
ConvertPreamble = True
Left = 304
Top = 24
end
object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
Destination = 'pop.gmail.com:995'
Host = 'pop.gmail.com'
MaxLineAction = maException
Port = 995
DefaultPort = 0
SSLOptions.Method = sslvSSLv2
SSLOptions.Mode = sslmUnassigned
SSLOptions.VerifyMode = []
SSLOptions.VerifyDepth = 0
Left = 160
Top = 32
end
end

This is a simple application and i only show here how to retrieve the number of messages from the inbox and the subject. For everything eles use your imagination :D

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL,
IdSSLOpenSSL, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdPOP3;

type
TForm1 = class(TForm)
Button1: TButton;
pop: TIdPOP3;
msg: TIdMessage;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var n,i:integer;
begin
pop.Connect;
n:=pop.CheckMessages;
listbox1.Items.Add(format('There are %d messages',[n]));
for i:=0 to n-1 do
begin
pop.Retrieve(i,msg);
listbox1.Items.Add(format('%d - %s',[i,msg.Subject]));
application.ProcessMessages;
end;
pop.Disconnect;
end;

end.

I hope this helps you and if you got any questions feel free to ask.
Good luck !

Thursday, February 16, 2006

First steps

Hello Delphi fans and users!

I'd like to share you my knowledge in Delphi programming and also some piece of code that'll make your life easyer.

Living in the era of internet, make us bump into it every step we make, so any day-by-day applications make use of internet. Maybe many of you tried to make a program that use some network, and maybe you found that is not that easy. I try to post here some tips and techniques to help you.

DISCLAIMER

All you can find here you may freely use into yor programs.
I strongly recommend NOT to use any piece of code for malware developement.
Any abuse you make you make on your own. I didn't assume any responsability of code usage, even it destroys your hardware, software or even your life.