منو
 کاربر Online
927 کاربر online
 : کامپیوتر
برای پاسخ دادن به این ارسال باید از صفحه قبلی اقدام کنید.   کاربر offline دبیر گروه کامپیوتر 3 ستاره ها ارسال ها: 1679   در :  پنج شنبه 31 شهریور 1390 [17:00 ]
  آموزش دلفی - بخش سوم
 

نمایش میزان کپی شدن فایل با ProgressBar در دلفی
چطور میتوان زمان کپی شدن فایل را با استفاده از ProgressBar نمایش داد؟
برای انجام این کار ابتدا بر روی یک فرم یک ProgressBar اضافه کنید سپس تابع زیر را تایپ کنید:





procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string) ;
var
FromF, ToF: file of byte ;
Buffer: array0..4096 of char ;
NumRead: integer ;
FileLength: longint ;
begin
AssignFile(FromF, Source) ;
reset(FromF) ;
AssignFile(ToF, Destination) ;
rewrite(ToF) ;
FileLength := FileSize(FromF) ;
with Progressbar1 do
begin
Min := 0 ;
Max := FileLength ;
while FileLength > 0 do
begin
BlockRead(FromF, Buffer0, SizeOf(Buffer), NumRead) ;
FileLength := FileLength - NumRead ;
BlockWrite(ToF, Buffer0, NumRead) ;
Position := Position + NumRead ;
end ;
CloseFile(FromF) ;
CloseFile(ToF) ;
end ;
end ;

در این تابع شما در واقع فایل مبدا را خوانده و در مقصد مینویسید. حالا یک دکمه اضافه کرده کد زیر رو اضافه نمایید:

procedure TForm1.Button1Click(Sender: TObject) ;
begin
CopyFileWithProgressBar1(`c:Welcome.exe`, `c:tempWelcome.exe’) ;
end ;

چطور می توان زمان کپی شدن فایل را محاسبه و نمایش داد؟
برای این کار نیز میتوانید از تابع زیر استفاده کنید:

procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string) ;
var
FromF, ToF: file of byte ;
Buffer: array0..4096 of char ;
NumRead: integer ;
FileLength: longint ;
t1, t2: DWORD ;
maxi: integer ;
begin
AssignFile(FromF, Source) ;
reset(FromF) ;
AssignFile(ToF, Destination) ;
rewrite(ToF) ;
FileLength := FileSize(FromF) ;
with Progressbar1 do
begin
Min := 0 ;
Max := FileLength ;
t1 := TimeGetTime ;
maxi := Max div 4096 ;
while FileLength > 0 do
begin
BlockRead(FromF, Buffer0, SizeOf(Buffer), NumRead) ;
FileLength := FileLength - NumRead ;
BlockWrite(ToF, Buffer0, NumRead) ;
t2 := TimeGetTime ;
Min := Min + 1 ;
// Show the time in Label1
label1.Caption := FormatFloat(`0.00`, ((t2 - t1) / min * maxi - t2 + t1) / 100) ;
Application.ProcessMessages ;
Position := Position + NumRead ;
end ;
CloseFile(FromF) ;
CloseFile(ToF) ;
end ;
end ;

در این تابع ابتدا زمان اولیه در متغیر t1 ذخیره شده و سپس پس از کپی شدن هر قسمت از فایل، زمان در متغیر t2 ذخیره میشود و توسط فرمول زیر مقدار زمان باقی مانده تا کپی کامل فایل بدست می آید.

((t2 - t1) / min * maxi - t2 + t1) / 100

نصب ProgressBar روی StatusBar در دلفی
انجام این کار بسیار ساده است. برای این کار کافی است بر روی فرم خود یک StatusBar اضافه نمایید حالا در قسمت تعاریف متغیر های عمومی کد زیر را بنویسید:

ProgressBar1: TprogressBar ;

در ادامه دستورات زیر را در خاصیت OnCreate فرم خود بنویسید:

var
ProgressBarStyle: LongInt ;
begin
{create a run progress bar in the status bar}
ProgressBar1 := TProgressBar.Create(StatusBar1) ;
ProgressBar1.Parent := StatusBar1 ;
{remove progress bar border}
ProgressBarStyle := GetWindowLong(ProgressBar1.Handle, GWL_EXSTYLE) ;
ProgressBarStyle := ProgressBarStyle - WS_EX_STATICEDGE ;
SetWindowLong(ProgressBar1.Handle, GWL_EXSTYLE, ProgressBarStyle) ;
{set progress bar position and size - put in Panel2}
ProgressBar1.Left := StatusBar1.Panels.Items0.Width +
StatusBar1.Panels.Items1.Width + 4 ;
ProgressBar1.Top := 4 ;
ProgressBar1.Height := StatusBar1.Height - 6 ;
ProgressBar1.Width := StatusBar1.Panels.Items2.Width - 6 ;
{set range and initial state}
ProgressBar1.Min := 0 ;
ProgressBar1.Max := 100 ;
ProgressBar1.Step := 1 ;
ProgressBar1.Position := 0 ;
end ;

حالا برای آنکه پس از خارج شدن از فرم حافظه اشغال شده آزاد گردد، در قسمت OnDestroy در Event فرمتان دستور زیر را اضافه نمایید:

ProgressBar1.free ;

استفاده از DLLها در دلفی
ایجاد یک DLL
با استفاده از منو فایل گزینه New Items را انتخاب کنید و آیتم DLL Wizard را انتخاب نمایید. حال به فایل ایجاد شده، یک فرم با استفاده از روش بالا اضافه نمایید. دقت نمایید که Application را بجای فرم انتخاب ننمایید. حال اگر فرض کنیم که نام فرم شما Demo باشد و بانام UDemo.pas آنرا ذخیره کرده باشید. باید در فایل DLL بصورت زیر کد نویسی نمایید:

library demodll ;

{Important note about DLL memory management: ShareMem must be the
first unit in your library`s USES clause AND your project`s (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters.}

uses
SysUtils ,
Classes ,
UDemo in `UDemo.pas` {Demo} ;

{ $R *.res}
procedure ShowdemoForm;stdcall ;
begin
Demo :=Tdemo.Create(nil) ;
demo.Show ;
end ;

function ShowdemoFormModal:integer;stdcall ;
begin
demo :=Tdemo.Create(nil) ;
Result := demo.ShowModal ;
end ;

Exports
ShowDemoForm ,
ShowdemoFormModal ;
begin
end .

دقت کنید که نام DLL فوق DemoDll می باشد و با نام DemoDll.dpr ذخیره گردیده است.
حال بر روی فرم موجود تمام دکمه ها و آبجکت های مورد نظرتان را اضافه و کد نویسی کنید (اختیاری). در پایان در منو Project گذینه Build DemoDll را انتخاب کرده و اجرا نمایید. فایلی با نام DemoDll.dll ایجاد می گردد که برای استفاده آماده است.

استفاده از یک DLL بصورت دینامیکی
برای استفاده از یک DLL بصورت دینامیکی، ابتدا نام توابعی را که در فایل DLL شما موجود است بصورت زیر تعریف نمایید:

unit UMain ;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ,
Dialogs, StdCtrls, ExtCtrls ;
type
TShowdemoFormModal= Function :integer ;
.
.
.

دقت کنید که نام برنامه انتخابی پیش فرض Main و با نام UMain.pas ذخیره گشته است. حال برای لود کردن DLL یادشده، یک دکمه بر روی فرم قرارداده آنرا بصورت زیر کد نویسی کنید:

var
hndDLLHandle:THandle ;
ShowdemoFormModal:TShowdemoFormModal ;
procedure TFMain.Button1Click(Sender: TObject) ;
begin
try
hndDLLHandle:=LoadLibrary(`Demodll.dll’) ;

if hndDLLHandle 0 then begin
@ShowdemoFormModal:=getProcAddress(hndDLLHandle,`ShowdemoFormModal’) ;

if addr(ShowdemoFormModal) nil then begin
ShowdemoFormModal ;
end
else
showmessage (`function not exists …’) ;
end
else
showMessage(`Dll Not Found!’) ;
finally
freelibrary(hndDLLHandle) ;
end ;
end ;

محدودکردن تغییر اندازه فرم در دلفی
گاهی اوقات نیاز است فرم ما از نظر اندازه پیرو یک الگو باشد و کاربر نتواند خارج از محدوده این فرم را تغییر اندازه دهد، راهی که پیشنهاد می شود، استفاده از Windows Messages ، تابع WM_GetMinMaxInfo میباشد.

unit MinMax ;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls ,
Forms, Dialogs ;
type
TForm1 = class(TForm)
private
{Private declarations}
procedure WMGetMinMaxInfo(var MSG: Tmessage); message WM_GetMinMaxInfo ;
public
{Public declarations}
end ;

var
Form1: TForm1 ;
implementation
{ $R *.DFM}
procedure TForm1.WMGetMinMaxInfo(var MSG: Tmessage) ;
Begin
inherited ;
with PMinMaxInfo(MSG.lparam)^ do
begin
with ptMinTrackSize do
begin
X := 300 ;
Y := 150 ;
end ;
with ptMaxTrackSize do
begin
X := 350 ;
Y := 250 ;
end ;
end ;
end ;

  امتیاز: 0.00