DarkByte Utils

DarkByte Utils

Postby DarkByte » 04 Jan 2010, 11:45

DarkByte Utils



Librarie (unit) Delphi


De ceva timp m-am apucat sa-mi creez un unit (librarie) pentru Delphi care sa contina functii/proceduri care le folosesc in mod uzual. Acest unit va poate ajuta sa lucrati cu bitii unui numar, sa modificati starea unei ferestre de prin Windows, sa verificati si sa schimbati starea Lock-urilor (Num Lock, Caps Lock, Scroll Lock), sa manipulati prin Registry in Windows... si altele.

Singura limitare legata de folosirea acestui unit este faptul ca sunteti rugat sa pastrati linia care specifica autorul si acest forum.

Sper sa va fie de ajutor :)

  1. unit db_utils;
  2.  
  3. // by DarkByte - http://www.bitcell.info
  4.  
  5. interface
  6.  
  7. uses Windows, SysUtils, Graphics, ShellAPI, Registry;
  8.  
  9. // Bit manipulation - begin
  10. function GetBit(const Value: DWord; const Bit: Byte): Boolean;
  11. function ClearBit(const Value: DWord; const Bit: Byte): DWord;
  12. function SetBit(const Value: DWord; const Bit: Byte): DWord;
  13. function EnableBit(const Value: DWord; const Bit: Byte; const TurnOn: Boolean): DWord;
  14.  
  15. // Image manipulation
  16. function GetLargeIconFromFile(FName: String; aIndex: Word; var Icon: TIcon): Boolean;
  17. function GetSmallIconFromFile(FName: String; aIndex: Word; var Icon: TIcon): Boolean;
  18. function IcoToBmp(Icon: TIcon; aSize: Integer = 16) : TBitmap;
  19.  
  20. // Window manipulation
  21. procedure SetWindowOnTop(Handle: HWnd; aTop: Boolean);
  22. function MakeWindowTransparent(Handle: HWND; aAlpha: Integer = 10) : Boolean;
  23.  
  24. // Keyboard manipulation
  25. function IsModifierDown(aModifier: Integer): Boolean;   // VK_CTRL, VK_SHIFT, VK_MENU
  26. function IsLockOn(aLock: Integer): Boolean;             // VK_NUMLOCK, VK_CAPITAL, VK_SCROLL
  27. procedure SetLockState(aLock: Integer; aState: Boolean);
  28.  
  29. // Registry manipulation
  30. function GetRegistryData(RootKey: HKEY; Key, Value: String): Variant;
  31. procedure SetRegistryData(RootKey: HKEY; Key, Value: String; RegDataType: TRegDataType; Data: Variant);
  32.  
  33. implementation
  34.  
  35. // Bit manipulation - begin
  36. function GetBit(const Value: DWord; const Bit: Byte): Boolean;
  37. begin
  38.   Result := (Value and (1 shl Bit)) <> 0;
  39. end;
  40.  
  41. function ClearBit(const Value: DWord; const Bit: Byte): DWord;
  42. begin
  43.     Result := Value and not (1 shl Bit);
  44. end;
  45.  
  46. function SetBit(const Value: DWord; const Bit: Byte): DWord;
  47. begin
  48.     Result := Value or (1 shl Bit);
  49. end;
  50.  
  51. function EnableBit(const Value: DWord; const Bit: Byte; const TurnOn: Boolean): DWord;
  52. begin
  53.     Result := (Value or (1 shl Bit)) xor (Integer(not TurnOn) shl Bit);
  54. end;
  55. // Bit manipulation - end
  56.  
  57. // Image manipulation - begin
  58. function GetLargeIconFromFile(FName: String; aIndex: Word; var Icon: TIcon): Boolean;
  59. var LargeIcon, SmallIcon : HIcon;
  60. begin
  61.    ExtractIconEx(PChar(FName), aIndex, LargeIcon, SmallIcon, 1);
  62.    if LargeIcon <= 1
  63.      then Result := False
  64.      else
  65.        begin
  66.          Icon.Handle := LargeIcon;
  67.          Result := True;
  68.        end;
  69. end;
  70.  
  71. function GetSmallIconFromFile(FName: String; aIndex: Word; var Icon: TIcon): Boolean;
  72. var LargeIcon, SmallIcon : HIcon;
  73. begin
  74.    ExtractIconEx(PChar(FName), aIndex, LargeIcon, SmallIcon, 1);
  75.    if SmallIcon <= 1
  76.      then Result := False
  77.      else
  78.        begin
  79.          Icon.Handle := SmallIcon;
  80.          Result := True;
  81.        end;
  82. end;
  83.  
  84. function IcoToBmp(Icon: TIcon; aSize: Integer = 16): TBitmap;
  85. var lBitmap: TBitmap;
  86. begin
  87.   lBitmap := TBitmap.Create;
  88.  
  89.   lBitmap.Width := aSize;
  90.   lBitmap.Height := aSize;
  91.   lBitmap.PixelFormat := pf24bit;
  92.  
  93.   lBitmap.Canvas.Pixels[0, lBitmap.Width - 1];
  94.   lBitmap.Transparent := True;
  95.  
  96.   lBitmap.Canvas.Draw(0, 0, Icon);
  97.   Result := lBitmap;
  98. end;
  99. // Image manipulation - end
  100.  
  101. // Window manipulation - begin
  102. procedure SetWindowOnTop(Handle: HWnd; aTop: Boolean);
  103. begin
  104.   if aTop
  105.     then
  106.       begin
  107.         SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW + SWP_NOMOVE + SWP_NOSIZE);
  108.         SetWindowPos(Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW + SWP_NOMOVE + SWP_NOSIZE);
  109.       End
  110.     else SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW + SWP_NOMOVE + SWP_NOSIZE);
  111. End;
  112.  
  113. function MakeWindowTransparent(Handle: HWND; aAlpha: Integer = 10): Boolean;
  114. type TSetLayeredWindowAttributes = function(HWnd: HWND; crKey: ColorREF; bAlpha: Byte; DwFlags: Longint): Longint; stdcall;
  115. const LWA_ColorKEY  = 1;
  116.       LWA_ALPHA     = 2;
  117.       WS_EX_LAYERED = $80000;
  118. var hUser32: HMODULE;
  119.     SetLayeredWindowAttributes: TSetLayeredWindowAttributes;
  120. begin
  121.   Result := False;
  122.   hUser32 := GetModuleHandle('USER32.DLL');
  123.   if hUser32 <> 0 then
  124.     begin
  125.       @SetLayeredWindowAttributes := GetProcAddress(hUser32, 'SetLayeredWindowAttributes');
  126.       if @SetLayeredWindowAttributes <> nil then
  127.         begin
  128.           SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
  129.           SetLayeredWindowAttributes(Handle, 0, Trunc((255 / 100) * (100 - aAlpha)), LWA_ALPHA);
  130.           Result := True;
  131.         End;
  132.     End;
  133. End;
  134. // Window manipulation - end
  135.  
  136. // Keyboard manipulation - begin
  137. function IsModifierDown(aModifier: Integer): Boolean;
  138. var State : TKeyboardState;
  139. begin
  140.   GetKeyboardState(State) ;
  141.   Result := ((State[aModifier] and 128) <> 0) ;
  142. end;
  143.  
  144. function IsLockOn(aLock: Integer): Boolean;
  145. begin
  146.   Result := 0 <> (GetKeyState(aLock) and $01);
  147. end;
  148.  
  149. procedure SetLockState(aLock: Integer; aState: Boolean);
  150. var KeyState: TKeyboardState;
  151. begin
  152.   GetKeyboardState(KeyState);
  153.   if (KeyState[aLock] = 0) and (aState)
  154.     then
  155.       begin
  156.         Keybd_Event(aLock, 1, KEYEVENTF_EXTENDEDKEY or 0, 0);
  157.         Keybd_Event(aLock, 1, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
  158.       end
  159.     else
  160.       if (KeyState[aLock] <> 0) and not (aState)
  161.         then
  162.           begin
  163.             Keybd_Event(aLock, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);
  164.             Keybd_Event(aLock, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
  165.           end;
  166. end;
  167. // Keyboard manipulation - end
  168.  
  169. // Registry manipulation - begin
  170. function GetRegistryData(RootKey: HKEY; Key, Value: String): Variant;
  171. var Reg: TRegistry;
  172.     RegDataType: TRegDataType;
  173.     DataSize, Len: integer;
  174.     lValue: String;
  175. begin
  176.   Reg := nil;
  177.   try
  178.     Reg := TRegistry.Create(KEY_QUERY_VALUE);
  179.     Reg.RootKey := RootKey;
  180.     if Reg.OpenKeyReadOnly(Key)
  181.       then
  182.         begin
  183.           try
  184.             RegDataType := Reg.GetDataType(Value);
  185.             if (RegDataType in [rdString, rdExpandString])
  186.               then Result := Reg.ReadString(Value)
  187.               else
  188.             if RegDataType = rdInteger
  189.               then Result := Reg.ReadInteger(Value)
  190.               else
  191.             if RegDataType = rdBinary
  192.               then
  193.                 begin
  194.                   DataSize := Reg.GetDataSize(Value);
  195.                   if (DataSize <> -1)
  196.                     then
  197.                       begin
  198.                         SetLength(lValue, DataSize);
  199.                         Len := Reg.ReadBinaryData(Value, PChar(lValue)^, DataSize);
  200.                         if Len = DataSize
  201.                           then Result := lValue;
  202.                       end;
  203.                 end;
  204.           except
  205.             lValue := EmptyStr;
  206.             Reg.CloseKey;
  207.             Raise;
  208.           end;
  209.       Reg.CloseKey;
  210.         end
  211.       else Raise Exception.Create(SysErrorMessage(GetLastError));
  212.   except
  213.     Reg.Free;
  214.     Raise;
  215.   end;
  216.   Reg.Free;
  217. end;
  218.  
  219. procedure SetRegistryData(RootKey: HKEY; Key, Value: String; RegDataType: TRegDataType; Data: Variant);
  220. var Reg: TRegistry;
  221.     lValue: String;
  222. begin
  223.   Reg := TRegistry.Create(KEY_WRITE);
  224.   try
  225.     Reg.RootKey := RootKey;
  226.     if Reg.OpenKey(Key, True)
  227.       then
  228.         begin
  229.           try
  230.             if RegDataType = rdUnknown
  231.               then RegDataType := Reg.GetDataType(Value);
  232.             if RegDataType = rdString
  233.               then Reg.WriteString(Value, Data)
  234.               else
  235.             if RegDataType = rdExpandString
  236.               then Reg.WriteExpandString(Value, Data)
  237.               else
  238.             if RegDataType = rdInteger
  239.               then Reg.WriteInteger(Value, Data)
  240.               else
  241.             if RegDataType = rdBinary
  242.               then
  243.                 begin
  244.                   lValue := Data;
  245.                   Reg.WriteBinaryData(Value, PChar(lValue)^, Length(lValue));
  246.                 end
  247.               else Raise Exception.Create(SysErrorMessage(ERROR_CANTWRITE));
  248.           except
  249.             Reg.CloseKey;
  250.             Raise;
  251.           end;
  252.           Reg.CloseKey;
  253.         end
  254.       else Raise Exception.Create(SysErrorMessage(GetLastError));
  255.   finally
  256.     Reg.Free;
  257.   end;
  258. end;
  259. // Registry manipulation - end
  260.  
  261. end


Daca aveti intrebari legate de cod, puteti posta aici.
0,0p / 0 votes
User avatar
DarkByte
11011011
 
Joined: 29 Dec 2009
Status: 136

Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests

cron