Пятница, 25.09.2026, 21:33
Вы вошли как Гость | Группа "Гости"Приветствую Вас Гость | RSS
Форма входа
Меню сайта
Главная » FAQ [ Добавить вопрос ]

Программирование Delphi [91]
Вопрос-Ответ по программированию

ShellAPI функция ShFormatDrive().
Пример :

 
const SHFMT_DRV_A = 0; 
 const SHFMT_DRV_B = 1; 
 
 const SHFMT_ID_DEFAULT = $FFFF; 
 
 const SHFMT_OPT_QUICKFORMAT = 0; 
 const SHFMT_OPT_FULLFORMAT = 1; 
 const SHFMT_OPT_SYSONLY = 2; 
 
 const SHFMT_ERROR = -1; 
 const SHFMT_CANCEL = -2; 
 const SHFMT_NOFORMAT = -3; 
 
 function SHFormatDrive(hWnd : HWND; 
                        Drive : Word; 
                        fmtID : Word; 
                        Options : Word) : Longint 
    stdcall; external 'Shell32.dll' name 'SHFormatDrive'; 
 
 procedure TForm1.Button1Click(Sender: TObject); 
 var 
   FmtRes : longint; 
 begin 
   try 
     FmtRes:= ShFormatDrive(Handle, 
                            SHFMT_DRV_A, 
                            SHFMT_ID_DEFAULT, 
                            SHFMT_OPT_QUICKFORMAT); 
     case FmtRes  of 
      SHFMT_ERROR : ShowMessage('Error formatting the drive'); 
      SHFMT_CANCEL :  
        ShowMessage('User canceled formatting the drive'); 
      SHFMT_NOFORMAT : ShowMessage('No Format') 
     else 
      ShowMessage('Disk has been formatted'); 
     end; 
   except 
   end; 
 
 end; 

Используйте функцию Windows API CreateFile() чтобы получить дескриптор порта, и стандартные функции ввода-вывода для связи с полученным портом.
Пример :

 
             var 
               hCommFile : THandle; 
 
             procedure TForm1.Button1Click(Sender: TObject); 
             var 
               PhoneNumber : string; 
               CommPort : string; 
               NumberWritten : LongInt; 
             begin 
               PhoneNumber := 'ATDT 1-555-555-1212' + #13 + #10; 
               CommPort := 'COM2'; 
              {Open the comm port} 
               hCommFile := CreateFile(PChar(CommPort), 
                                       GENERIC_WRITE, 
                                       0, 
                                       nil, 
                                       OPEN_EXISTING, 
                                       FILE_ATTRIBUTE_NORMAL, 
                                       0); 
               if hCommFile=INVALID_HANDLE_VALUE then 
               begin 
                 ShowMessage('Unable to open '+ CommPort); 
                 exit; 
               end; 
 
              {Dial the phone} 
               NumberWritten:=0; 
               if WriteFile(hCommFile, 
                            PChar(PhoneNumber)^, 
                            Length(PhoneNumber), 
                            NumberWritten, 
                           nil) = false then begin 
                 ShowMessage('Unable to write to ' + CommPort); 
               end; 
             end; 
 
             procedure TForm1.Button2Click(Sender: TObject); 
             begin 
              {Close the port} 
               CloseHandle(hCommFile); 
             end; 
 

             {tapi Errors} 
              const TAPIERR_CONNECTED          = 0; 
              const TAPIERR_DROPPED            = -1; 
              const TAPIERR_NOREQUESTRECIPIENT = -2; 
              const TAPIERR_REQUESTQUEUEFULL   = -3; 
              const TAPIERR_INVALDESTADDRESS   = -4; 
              const TAPIERR_INVALWINDOWHANDLE  = -5; 
              const TAPIERR_INVALDEVICECLASS   = -6; 
              const TAPIERR_INVALDEVICEID      = -7; 
              const TAPIERR_DEVICECLASSUNAVAIL = -8; 
              const TAPIERR_DEVICEIDUNAVAIL    = -9; 
              const TAPIERR_DEVICEINUSE        = -10; 
              const TAPIERR_DESTBUSY           = -11; 
              const TAPIERR_DESTNOANSWER       = -12; 
              const TAPIERR_DESTUNAVAIL        = -13; 
              const TAPIERR_UNKNOWNWINHANDLE   = -14; 
              const TAPIERR_UNKNOWNREQUESTID   = -15; 
              const TAPIERR_REQUESTFAILED      = -16; 
              const TAPIERR_REQUESTCANCELLED   = -17; 
              const TAPIERR_INVALPOINTER       = -18; 
 
             {tapi size constants} 
              const TAPIMAXDESTADDRESSSIZE      = 80; 
              const TAPIMAXAPPNAMESIZE          = 40; 
              const TAPIMAXCALLEDPARTYSIZE      = 40; 
              const TAPIMAXCOMMENTSIZE          = 80; 
              const TAPIMAXDEVICECLASSSIZE      = 40; 
              const TAPIMAXDEVICEIDSIZE         = 40; 
 
             function tapiRequestMakeCallA(DestAddress : PAnsiChar; 
                                           AppName : PAnsiChar; 
                                           CalledParty : PAnsiChar; 
                                           Comment : PAnsiChar) : LongInt; 
               stdcall; external 'TAPI32.DLL'; 
 
             function tapiRequestMakeCallW(DestAddress : PWideChar; 
                                           AppName : PWideChar; 
                                           CalledParty : PWideChar; 
                                           Comment : PWideChar) : LongInt; 
               stdcall; external 'TAPI32.DLL'; 
 
             function tapiRequestMakeCall(DestAddress : PChar; 
                                          AppName : PChar; 
                                          CalledParty : PChar; 
                                          Comment : PChar) : LongInt; 
               stdcall; external 'TAPI32.DLL'; 
 
             procedure TForm1.Button1Click(Sender: TObject); 
             var 
               DestAddress : string; 
               CalledParty : string; 
               Comment : string; 
             begin 
               DestAddress := '1-555-555-1212'; 
               CalledParty := 'Frank Borland'; 
               Comment := 'Calling Frank'; 
               tapiRequestMakeCall(pChar(DestAddress), 
                                   PChar(Application.Title), 
                                   pChar(CalledParty), 
                                   PChar(Comment)); 
 
             end; 
 
             end.

ShellApi функция ExtractAssociatedIcon()
Пример :

 
            uses ShellApi; 
 
             procedure TForm1.Button1Click(Sender: TObject); 
             var 
               Icon : hIcon; 
               IconIndex : word; 
 
             begin 
               IconIndex := 1; 
               Icon := ExtractAssociatedIcon(HInstance, 
                                            Application.ExeName, 
                                            IconIndex); 
              DrawIcon(Canvas.Handle, 10, 10, Icon); 
             end; 

             program Project1; 
 
             uses 
               Windows, 
               Forms, 
               Unit1 in 'Unit1.pas' {Form1}; 
 
             {$R *.RES} 
 
             begin 
               if GetKeyState(vk_F8) < 1 then 
                MessageBox(0, 'F8 was pressed during startup', 'MyApp', mb_ok); 
               Application.Initialize; 
               Application.CreateForm(TForm1, Form1); 
               Application.Run; 
             end. 

Наш опрос
Сколько вам лет
Всего ответов: 20
Статистика

Онлайн всего: 1
Гостей: 1
Пользователей: 0
Поиск