Tips-A2Z home page


Sub-categories

(None)

All  |   Discussion (0)Delphi Code snippets

Converting strings to and from Pchars:
Use"StrPCopy" to convert a string into a pchar.
function StrPCopy(Dest: PChar; 

const Source: string): PChar;

Use "StrPas" to convert a pchar into a string.
function StrPas(Str: PChar): string;
   Kevin Solway (173)

– –– ——— –– –

Some one-liners:
  • Display last system error message:
    ShowMessage(SysErrorMessage(GetLastError()));

  • Convert a number to currency:
    FloatToStrF(Value, ffCurrency, 15, CurrencyDecimals); // "15" is the precision, where "Value" is a "Double"




  • File name of application:
    ExtractFileName(Application.ExeName);

  • Path of application:
    ExtractFilePath(Application.ExeName);

  • Let other applications use the CPU when you're in a loop:
    Application.ProcessMessages;
       Kevin Solway (173)
  • – –– ——— –– –

    Running on Vista?
    function IsWindowsVista: Boolean;   

    var VerInfo: TOSVersioninfo;

    begin

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);

    GetVersionEx(VerInfo);

    Result := VerInfo.dwMajorVersion >= 6;

    end;
       Kevin Solway (173)

    – –– ——— –– –

    2 ways to do 'if and, then':
    if (SentenceAge >= '18') then

    if (SentenceAge <= '20') then

    Result := IntToStr(TrQi4) + ' - ' + IntToStr(TrB);


    if (SentenceAge >= '18') and (SentenceAge <= '20') then

    Result := IntToStr(TrQi4) + ' - ' + IntToStr(TrB);
       kellyjones00 (593)

    – –– ——— –– –

    Horizontal scrollbar for listbox
    listbox.Perform(LB_SETHORIZONTALEXTENT, 900, 0);
       thesource (378)

    – –– ——— –– –

    Array of byte to string, and string to array of byte:
    // string to array

    StrPLCopy(pansichar(@arr), str, sizeof(arr));



    // array to str

    procedure AnsiStringFromBuf(var S: AnsiString; Buffer: PAnsiChar; Len: Integer);

    // Usage: ansistringfrombuf(str, pansichar(@arr), sizeof(arr));

    begin

    setstring(s, buffer, len);

    setlength(s, strlen(buffer));

    end;
       thesource (378)

    – –– ——— –– –

    Detect when system is shutting down:
      private

    procedure EndSessionMsg(var Msg : TMessage); message WM_QUERYENDSESSION;





    procedure TForm1.EndSessionMsg(var Msg : TMessage);

    begin

    if backingupinprocess then Msg.Result := 0; // prevent system from shutting down or logging off

    Msg.result := 0;

    if Msg.lparam = 0 then

    begin

    ShowMessage('System is shutting down');

    end

    else if (DWORD(Msg.lparam) and ENDSESSION_LOGOFF) = ENDSESSION_LOGOFF then

    begin

    ShowMessage('User is logging off');

    end;

    end; // EndSessionMsg
       thesource (378)

    – –– ——— –– –

    Focus a combobox:

    Normally, you can focus a component by using its "setfocus" method. However, a combobox requires the use of a different method.
    procedure TForm1.focuscombobox(combobox : tcombobox);

    var editHWnd : HWND;

    begin

    editHWnd := FindWindowEx(Combobox.handle, 0, nil, nil); // get the handle of the edit component

    if editHWnd <> 0 then windows.setfocus(editHWnd);

    end;
       thesource (378)

    – –– ——— –– –

    Reading old versions of unpacked records:

    Different versions of delphi use a different number of bytes to align record fields. Using the following compiler directive may help in reading records that were used with older compilers.

    {$A4} // Set alignment to 4 bytes, for reading old format (Delphi 4) unpacked records

    As a rule, when creating the declarations for records, you should specify the use of packed records. For example:

    TPackedRecord = Packed Record
    . . .
    end;

    This future-proofs the record in the case that future versions of Delphi use a different number of bytes for field alignment.

    There may be some cost in performance, depending on the number and size of records, since unpacked records can be accessed faster.   thesource (378)

    – –– ——— –– –


     


     

    To post a new tip, sign up for a free account.
    (Unfortunately this is a necessary spam prevention measure)

    Who is online
    In total there are 43 users online :: 0 registered and 43 guests (based on users active over the past 5 minutes)