معلم دلفی

سه‌شنبه، دی ۰۶، ۱۳۸۴

تغییر رنگهای یک گرید

How to change the string grid cell color?

سورس کد زیر به شما کمک می کند که با استفاده از رویدادهای انتخاب یک سلول یا در زمان ایجاد تصویر سلول تغییرات مورد نظر شما در زمینه رنگ سلولها اعمال شود.



type
TGridCracker = Class( TStringgrid );
// required to access protected method Invalidaterow


procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
With TGridCracker( Sender As TStringgrid ) Do Begin
InvalidateRow( Row );
InvalidateRow( aRow );
End;
end;


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
grid: TStringgrid;
begin
If gdFixed In State Then Exit;


grid := Sender As TStringgrid;
If grid.Row = aRow Then Begin
With Grid.Canvas.Brush Do Begin
Color := $C0FFFF;
Style := bsSolid;
End;
grid.Canvas.FillRect( Rect );
grid.Canvas.Font.Color := clBlack;
grid.Canvas.TextRect( Rect, Rect.Left+2, Rect.Top+2, grid.Cells[acol, arow]);
Grid.Canvas.Brush := grid.Brush;
End;



چهارشنبه، آذر ۰۹، ۱۳۸۴

انتقال یک فایل بزرگ از طریق تقسیم به قطعات کوچک در دلفی

گاهی برای انتقال یک فایل بزرگ محدودیت پهنای باند داریم و نمی توانیم آنرا یکجا منتقل کنیم. در این زمیان با تقسیم فایل به قسمتهای کوچک و انتقال هر قسمت به مقصد و در نهایت ساختن فایل اصلی راه حل خوبی است.
در حال حاضر برنامه های زیادی هستند که انتقال فایل از اینترنت را با این روش انجام می دهند و کاربران ایرانی هم بدلیل سرعت اینترنت در ایران از متقاضیان اینگونه برنامه ها هستند.
نمونه این برنامه ها FlashGet یا ReGet هستند.
حال اگر می خواهید اینکار را در دلفی انجام دهید می توانید با استفاده از TStream و سورس کد نمونه زیر اینکار را انجام دهید.



procedure SplitFile(FileName : TFileName; FilesByteSize : Integer) ;
// FileName == file to split into several smaller files
// FilesByteSize == the size of files in bytes
var
fs, ss: TFileStream;
cnt : integer;
SplitName: String;
begin
fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite) ;
try
for cnt := 1 to Trunc(fs.Size / FilesByteSize) + 1 do
begin
SplitName := ChangeFileExt(FileName, Format('%s%d', ['._',cnt])) ;
ss := TFileStream.Create(SplitName, fmCreate or fmShareExclusive) ;
try
if fs.Size - fs.Position < FilesByteSize then
FilesByteSize := fs.Size - fs.Position;
ss.CopyFrom(fs, FilesByteSize) ;
finally
ss.Free;
end;
end;
finally
fs.Free;
end;
end;






procedure MergeFiles(FirstSplitFileName, OutFileName : TFileName) ;
// FirstSplitFileName == the name of the first piece of the split file
// OutFileName == the name of the resulting merged file
var
fs, ss: TFileStream;
cnt: integer;
begin
cnt := 1;
fs := TFileStream.Create(OutFileName, fmCreate or fmShareExclusive) ;
try
while FileExists(FirstSplitFileName) do
begin
ss := TFileStream.Create(FirstSplitFileName, fmOpenRead or fmShareDenyWrite) ;
try
fs.CopyFrom(ss, 0) ;
finally
ss.Free;
end;
Inc(cnt) ;
FirstSplitFileName := ChangeFileExt(FirstSplitFileName, Format('%s%d', ['._',cnt])) ;
end;
finally
fs.Free;
end;
end;





بصورت زیر می توان این توابع را صدا زد و مورد استفاده قرار داد.




SplitFile('c:\mypicture.bmp', 1024) ; //into 1 KB files
...
MergeFiles('c:\mypicture._1','c:\mymergedpicture.bmp') ;






در این مثال یک فایل تصویری به قطعات یک کیلو بایتی تقسیم شده و سپس این قطعات در مقصد با هم فایل اصلی را خواهند ساخت.