Форум: "Основная";
Текущий архив: 2004.07.25;
Скачать: [xml.tar.bz2];
ВнизПомогите перевести код из Си в Делфи. Найти похожие ветки
← →
Tahion2 (2004-07-10 14:20) [0]помогите перевести этот код в Делфийское представление:
typedef struct
{
WORD idReserved;
WORD idType;
WORD idCount;
ICONDIRENTRY idEntries[1];
}ICONDIR, *LPICONDIR;
typedef struct
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
DWORD dwImageOffset;
}ICONDIRENTRY, *LPICONDIRENTRY;
DWORD dwBytesRead;
HANDLE hf = ::CreateFile(Form1 -> OpenDialog1 -> FileName.c_str(),
GENERIC_READ, (DWORD)0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
icdir = (ICONDIR*) malloc (sizeof(ICONDIR));
::ReadFile (hf, icdir, 3 * sizeof(WORD), &dwBytesRead, NULL);
icdir = (ICONDIR*) realloc (icdir, (3 * sizeof(WORD)+sizeof(ICONDIRENTRY)*
icdir->idCount));
::ReadFile (hf,icdir->idEntries, sizeof (ICONDIRENTRY) *icdir->idCount,
&dwBytesRead, NULL);
CloseHandle (hf);
hf = NULL;
Label1 -> Caption = "This file contains " + (String)icdir -> idCount + " icons.";
← →
KilkennyCat © (2004-07-10 14:48) [1]Начинайте, будут ошибки - поправим.
← →
y-soft © (2004-07-10 15:24) [2]Редактор файлов иконок пишем? :)
← →
Fay (2004-07-10 15:46) [3]1) Перевод дословный. В Паскале array[0..0] не принесёт тебе счастья. 8)
2) Код - барахло. Перепишешь по людски.type
ICONDIRENTRY = record
bWidth : Byte;
bHeight : Byte;
bColorCount : Byte;
bReserved : Byte;
wPlanes : WORD;
wBitCount : WORD;
dwBytesInRes : DWORD;
dwImageOffset : DWORD;
end;
LPICONDIRENTRY = ^ICONDIRENTRY;
ICONDIR = record
idReserved : WORD;
idType : WORD;
idCount : WORD;
idEntries : array [0..0] of ICONDIRENTRY;
end;
LPICONDIR = ^ICONDIR;
var
hf, dwBytesRead : DWORD;
icdir : LPICONDIR;
begin
GetMem(icdir, SizeOf(ICONDIR));
hf := CreateFile(PChar(Form1.OpenDialog1.FileName), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
ReadFile(hf, icdir^, 3 * SizeOf(WORD), dwBytesRead, nil);
ReallocMem(icdir, 3 * SizeOf(WORD) + SizeOf(ICONDIRENTRY) * icdir^.idCount);
ReadFile(hf, icdir^.idEntries, SizeOf(ICONDIRENTRY) * icdir^.idCount, dwBytesRead, nil);
Label1.Caption := "This file contains " + IntToStr(icdir^.idCount) + " icons.";
CloseHandle(hf);
hf := 0;
← →
y-soft © (2004-07-10 16:09) [4]>Fay (10.07.04 15:46) [3]
2) Код - барахло. Перепишешь по людски
Вот код примера из MSDN:// We need an ICONDIR to hold the data
pIconDir = malloc( sizeof( ICONDIR ) );
// Read the Reserved word
ReadFile( hFile, &(pIconDir->idReserved), sizeof( WORD ), &dwBytesRead, NULL );
// Read the Type word - make sure it is 1 for icons
ReadFile( hFile, &(pIconDir->idType), sizeof( WORD ), &dwBytesRead, NULL );
// Read the count - how many images in this file?
ReadFile( hFile, &(pIconDir->idCount), sizeof( WORD ), &dwBytesRead, NULL );
// Reallocate IconDir so that idEntries has enough room for idCount elements
pIconDir = realloc( pIconDir, ( sizeof( WORD ) * 3 ) +
( sizeof( ICONDIRENTRY ) * pIconDir->idCount ) );
// Read the ICONDIRENTRY elements
ReadFile( hFile, pIconDir->idEntries, pIconDir->idCount * sizeof(ICONDIRENTRY),
&dwBytesRead, NULL );
// Loop through and read in each image
for(i=0;i<pIconDir->idCount;i++)
{
// Allocate memory to hold the image
pIconImage = malloc( pIconDir->idEntries[i].dwBytesInRes );
// Seek to the location in the file that has the image
SetFilePointer( hFile, pIconDir->idEntries[i].dwImageOffset,
NULL, FILE_BEGIN );
// Read the image data
ReadFile( hFile, pIconImage, pIconDir->idEntries[i].dwBytesInRes,
&dwBytesRead, NULL );
// Here, pIconImage is an ICONIMAGE structure. Party on it :)
// Then, free the associated memory
free( pIconImage );
}
// Clean up the ICONDIR memory
free( pIconDir );
Не правда ли - очень похоже? Тоже никаких проверок и исключений :)
Правда там есть приписка:
The following is an incomplete code fragment for reading an .ICO file :)
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2004.07.25;
Скачать: [xml.tar.bz2];
Память: 0.46 MB
Время: 0.035 c