Advertisement
Guest User

Tutorial #5

a guest
Nov 21st, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.71 KB | None | 0 0
  1. unit uMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7.   System.Classes, Vcl.Graphics,
  8.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Menus;
  9.  
  10. type
  11.   TPalWinInfo = Record
  12.     sTitle: String;
  13.     hMainWin: Hwnd;
  14.     hOutgoing: Hwnd;
  15.     hIncoming: Hwnd;
  16.     hNickList: Hwnd;
  17.     bIsRoom: Boolean;
  18.   End;
  19.  
  20. type
  21.   TForm1 = class(TForm)
  22.     lvWinInfo: TListView;
  23.     btnRefresh: TButton;
  24.     reIncoming: TRichEdit;
  25.     btnGetAllText: TButton;
  26.     PopupMenu1: TPopupMenu;
  27.     GetTHISroomsText1: TMenuItem;
  28.     procedure FormDestroy(Sender: TObject);
  29.     procedure btnRefreshClick(Sender: TObject);
  30.     procedure GetTHISroomsText1Click(Sender: TObject);
  31.     procedure btnGetAllTextClick(Sender: TObject);
  32.   private
  33.     { Private declarations }
  34.     Procedure AddToListView(pWinInfo: TPalWinInfo);
  35.     Procedure CleanUp;
  36.     Function GetPalText(const HwndIncommingText: Hwnd): String;
  37.   public
  38.     { Public declarations }
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.   aPalWinInfo: Array of TPalWinInfo;
  44.  
  45. implementation
  46.  
  47. {$R *.dfm}
  48.  
  49. procedure TForm1.AddToListView(pWinInfo: TPalWinInfo);
  50. var
  51.   sRoomType: String;
  52. begin
  53.  
  54.   case pWinInfo.bIsRoom of
  55.     True:
  56.       sRoomType := 'Room';
  57.     False:
  58.       sRoomType := 'Pm';
  59.   end;
  60.  
  61.   with lvWinInfo.Items.Add do
  62.   begin
  63.     Caption := pWinInfo.sTitle;
  64.     subItems.Add(IntToStr(pWinInfo.hMainWin));
  65.     subItems.Add(IntToStr(pWinInfo.hIncoming));
  66.     subItems.Add(IntToStr(pWinInfo.hOutgoing));
  67.     subItems.Add(IntToStr(pWinInfo.hNickList));
  68.     subItems.Add(sRoomType);
  69.   end;
  70. end;
  71.  
  72. function GetTitle(aHwnd: Hwnd): String;
  73. begin
  74.   SetLength(Result, 255);
  75.   SetLength(Result, GetWindowText(aHwnd, Pchar(Result), 255));
  76. end;
  77.  
  78. function GetClass(aHwnd: Hwnd): String;
  79. begin
  80.   SetLength(Result, 255);
  81.   SetLength(Result, GetClassName(aHwnd, Pchar(Result), 255));
  82. end;
  83.  
  84. Function EnumChildProc(aHwnd: Hwnd; Param: LParam): BOOL; stdcall;
  85. begin
  86.   Result := True;
  87.   if IsWindowVisible(aHwnd) = True then
  88.     case GetDlgCtrlId(aHwnd) of
  89.       202:
  90.         begin
  91.           aPalWinInfo[Pred(Length(aPalWinInfo))].hIncoming := aHwnd;
  92.           Result := False;
  93.         end;
  94.       203:
  95.         begin
  96.           aPalWinInfo[Pred(Length(aPalWinInfo))].hOutgoing := aHwnd;
  97.         end;
  98.       1789:
  99.         begin
  100.           aPalWinInfo[Pred(Length(aPalWinInfo))].hNickList := aHwnd;
  101.           aPalWinInfo[Pred(Length(aPalWinInfo))].bIsRoom := True;
  102.         end;
  103.     end;
  104. end;
  105.  
  106. function EnumWindowsProc(aHwnd: Hwnd; Param: LParam): BOOL; stdcall;
  107. begin
  108.   Result := True;
  109.   if GetClass(aHwnd) = 'DlgGroupChat Window Class' then
  110.   begin
  111.     if aPalWinInfo <> nil then
  112.       SetLength(aPalWinInfo, Length(aPalWinInfo) + 1)
  113.     else
  114.       SetLength(aPalWinInfo, 1);
  115.  
  116.     aPalWinInfo[Pred(Length(aPalWinInfo))].sTitle := GetTitle(aHwnd);
  117.     aPalWinInfo[Pred(Length(aPalWinInfo))].hMainWin := aHwnd;
  118.  
  119.     EnumChildWindows(aHwnd, @EnumChildProc, 0);
  120.  
  121.   end;
  122. end;
  123.  
  124. procedure TForm1.btnRefreshClick(Sender: TObject);
  125. var
  126.   I: Integer;
  127. begin
  128.   CleanUp;
  129.   EnumWindows(@EnumWindowsProc, 0);
  130.  
  131.   for I := Low(aPalWinInfo) to High(aPalWinInfo) do
  132.     AddToListView(aPalWinInfo[I]);
  133.  
  134. end;
  135.  
  136. procedure TForm1.CleanUp;
  137. begin
  138.   if aPalWinInfo <> nil then
  139.     aPalWinInfo := Nil;
  140.  
  141.   lvWinInfo.Clear;
  142. end;
  143.  
  144. procedure TForm1.FormDestroy(Sender: TObject);
  145. begin
  146.   CleanUp;
  147. end;
  148.  
  149. Function TForm1.GetPalText(const HwndIncommingText: Hwnd): String;
  150. var
  151.   iLineCount, iLineLength, iLineIndex: Integer;
  152.   szBuffer: array of WideChar;
  153. begin
  154.  
  155.   if isWindow(HwndIncommingText) = True then
  156.   begin
  157.     iLineCount := Sendmessage(HwndIncommingText, EM_GETLINECOUNT, 0, 0) - 2;
  158.     iLineIndex := Sendmessage(HwndIncommingText, EM_LINEINDEX, iLineCount, 0);
  159.     iLineLength := Sendmessage(HwndIncommingText, EM_LINELENGTH, iLineIndex, 0);
  160.  
  161.     if iLineLength = 0 then
  162.     begin
  163.       Result := 'Fail Line Length';
  164.       Exit;
  165.     end;
  166.  
  167.     SetLength(szBuffer, iLineLength);
  168.     Word(szBuffer[0]) := iLineLength;
  169.     Sendmessage(HwndIncommingText, EM_GETLINE, iLineCount, LParam(szBuffer));
  170.     szBuffer[iLineLength] := #0;
  171.     Result := PWidechar(szBuffer);
  172.  
  173.     szBuffer := Nil;
  174.   end
  175.   else
  176.     Result := 'Fail Hwnd';
  177. end;
  178.  
  179. procedure TForm1.GetTHISroomsText1Click(Sender: TObject);
  180. begin
  181.   reIncoming.Lines.Add(aPalWinInfo[lvWinInfo.Selected.Index].sTitle + ': ' +
  182.     GetPalText(aPalWinInfo[lvWinInfo.Selected.Index].hIncoming));
  183. end;
  184.  
  185. procedure TForm1.btnGetAllTextClick(Sender: TObject);
  186. var
  187.   I: Integer;
  188. begin
  189.   reIncoming.Clear;
  190.   for I := 0 to Pred(lvWinInfo.Items.Count) do
  191.     reIncoming.Lines.Add(aPalWinInfo[I].sTitle + ': ' +
  192.       GetPalText(aPalWinInfo[I].hIncoming));
  193. end;
  194.  
  195. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement