哪位朋友帮我把C++程序改成Delphi程序,该程序的作用是“通过托盘图标得到该所属进程”(50分)

  • 主题发起人 主题发起人 ahui
  • 开始时间 开始时间
A

ahui

Unregistered / Unconfirmed
GUEST, unregistred user!
哪位朋友帮我把C++程序改成Delphi程序,该程序的作用是“通过托盘图标得到该所属进程”<br>C++程序如下:<br>void CTrayDlg::OnButton1() <br>{<br> // TODO: Add your control notification handler code here<br> HWND wd = ::FindWindow(&quot;Shell_TrayWnd&quot;, NULL);<br> if (wd == NULL)<br> {<br> MessageBox(&quot;Error1&quot;);<br> return;<br> }<br> HWND wtd = FindWindowEx(wd, NULL, &quot;TrayNotifyWnd&quot;, NULL);<br> if (wtd == NULL)<br> {<br> MessageBox(&quot;Error2&quot;);<br> return;<br> }<br> HWND wd1 = FindWindowEx(wtd, NULL, &quot;SysPager&quot;, NULL);<br> HWND wd2;<br> if (wd1 == NULL)<br> {<br> wd2 = FindWindowEx(wtd, NULL, &quot;ToolbarWindow32&quot;, NULL);<br> }<br> else<br> {<br> wd2 = FindWindowEx(wd1, NULL, &quot;ToolbarWindow32&quot;, NULL);<br> }<br> if (wd2 == NULL)<br> {<br> MessageBox(&quot;Error3&quot;);<br> return;<br> }<br> DWORD pid;<br> pid = 0;<br> GetWindowThreadProcessId(wd2, &pid);<br> if (pid == NULL)<br> {<br> MessageBox(&quot;Error4&quot;);<br> return;<br> }<br> HANDLE hd = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_ALL_ACCESS,<br> true, pid);<br> if (hd == NULL)<br> {<br> MessageBox(&quot;Error5&quot;);<br> return;<br> }<br> int num = ::SendMessage(wd2, TB_BUTTONCOUNT, NULL, NULL);<br> int i;<br> unsigned long n;<br> TBBUTTON p, * pp;<br> CString x;<br> wchar_t name[256];<br> unsigned long whd,proid;<br> CString temp;<br> TBBUTTON* sp;<br> sp = (TBBUTTON *) 0x20f00;<br> for (i = 0; i &lt; num; i++)<br> {<br> ::SendMessage(wd2, TB_GETBUTTON, i, (LPARAM) sp);<br> pp = &p;<br> ReadProcessMemory(hd, sp, pp, sizeof(p), &n);<br> // &nbsp; &nbsp;x.Format(&quot;%x %x %x %x &nbsp;%x %x&quot;,p.iBitmap,p.idCommand,p.fsState,p.fsStyle, &nbsp;p.dwData, p.iString);<br> name[0] = 0;<br> if (p.iString != 0xffffffff)<br> {<br> try<br> {<br> ReadProcessMemory(hd, (void *) p.iString, name, 255, &n);<br> name[n] = 0;<br> }<br> catch (...)<br> {<br> }<br> // &nbsp; &nbsp;x+=&quot; &quot;;<br> // &nbsp; &nbsp;x+=name;<br> temp = name;<br> try<br> {<br> whd = 0;<br> ReadProcessMemory(hd, (void *) p.dwData, &whd, 4, &n);<br> }<br> catch (...)<br> {<br> }<br> proid = 0;<br> GetWindowThreadProcessId((HWND) whd, &proid);<br> x.Format(&quot;位置=%d 名称=%s 窗口句柄=%08x 进程ID=%08x&quot;, i,<br> (LPCTSTR) temp, whd, proid);<br> MessageBox(x);<br> }<br> }<br>}
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> v_hShellTray: &nbsp;THandle;<br> v_hTrayNotify: THandle;<br> v_hToolBar: &nbsp; &nbsp;THandle;<br> i,j,k:integer;<br> &nbsp; Info: &nbsp; _TBBUTTON;<br> buffer:pchar;<br> PID: &nbsp; THandle;<br> PRC: &nbsp; THandle;<br> R: &nbsp; Cardinal;<br> S:array[0..1024] of char;<br> MaxWidth: integer;<br> whd,proid:thandle;<br> name: array[0..254] of char;<br>begin<br> &nbsp;k:=0;<br> &nbsp;CheckListBox1.Items.Clear;<br> &nbsp;v_hShellTray := FindWindow('Shell_TrayWnd', nil);<br> &nbsp;v_hTrayNotify := FindWindowEx(v_hShellTray, 0, 'TrayNotifyWnd', nil);<br> &nbsp;v_hToolBar := FindWindowEx(v_hTrayNotify, 0, 'ToolbarWindow32', nil);<br><br> &nbsp;GetWindowThreadProcessId(v_hToolBar, &nbsp; @PID);<br> &nbsp;PRC &nbsp; := &nbsp; OpenProcess(PROCESS_VM_OPERATION &nbsp; or &nbsp; PROCESS_VM_READ &nbsp; or &nbsp; PROCESS_VM_WRITE, &nbsp; False, &nbsp; PID);<br> &nbsp;Buffer &nbsp; := &nbsp; VirtualAllocEx(PRC, &nbsp; nil, &nbsp; 4096, &nbsp; MEM_RESERVE &nbsp; or &nbsp; MEM_COMMIT, &nbsp; PAGE_READWRITE);<br><br> &nbsp;j:=SendMessage(v_hToolBar,TB_BUTTONCOUNT,0,0);<br> &nbsp;for i:=0 to j-1 do<br> &nbsp;begin<br> &nbsp; &nbsp; FillChar(Info, &nbsp; SizeOf(Info), &nbsp; 0);<br> &nbsp; &nbsp; WriteProcessMemory(PRC, &nbsp; Buffer, &nbsp; @Info, &nbsp; SizeOf(Info), &nbsp; R);<br><br> &nbsp; &nbsp; SendMessage(v_hToolBar, &nbsp; TB_GETBUTTON, &nbsp; i, &nbsp; integer(Buffer));<br> &nbsp; &nbsp; ReadProcessMemory(PRC, &nbsp; Buffer, &nbsp; @Info, &nbsp; SizeOf(Info), &nbsp; R);<br> &nbsp; &nbsp; if &nbsp;Info.iString&lt;&gt;-1 then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp;k:=k+1;<br><br> &nbsp; &nbsp; &nbsp;//得到图标所属的窗口和窗口所属的进程ID<br> &nbsp; &nbsp; &nbsp;ReadProcessMemory(PRC,Pointer(Info.dwData), @whd, 4, R);<br> &nbsp; &nbsp; &nbsp;proid := 0;<br> &nbsp; &nbsp; &nbsp;GetWindowThreadProcessId(whd,@proid);<br><br> &nbsp; &nbsp; &nbsp; //得到名称<br> &nbsp; &nbsp; &nbsp; &nbsp;//ReadProcessMemory(PRC,Pointer(Info.iString), @name[0],255, R); &nbsp; &nbsp;没用得出来全是乱嘛<br><br><br> &nbsp; &nbsp; &nbsp;if &nbsp;not Boolean(SendMessage(v_hToolBar, &nbsp; TB_ISBUTTONHIDDEN, &nbsp; Info.idCommand, &nbsp; 0)) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(v_hToolBar, &nbsp; TB_GETBUTTONTEXT, &nbsp; Info.idCommand, &nbsp; integer(integer(@Buffer[0]) &nbsp; + &nbsp; SizeOf(Info)));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReadProcessMemory(PRC, &nbsp; Pointer(integer(@Buffer[0]) &nbsp; + &nbsp; SizeOf(Info)), &nbsp; @S[0], &nbsp; SizeOf(S), &nbsp; R);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;id[k-1]:= Info.idCommand;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CheckListBox1.Items.Add(S+'--进程ID:'+inttostr(proid));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CheckListBox1.Checked[k-1]:=false;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(v_hToolBar, &nbsp; TB_GETBUTTONTEXT, &nbsp; Info.idCommand, &nbsp; integer(integer(@Buffer[0]) &nbsp; + &nbsp; SizeOf(Info)));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReadProcessMemory(PRC, &nbsp; Pointer(integer(@Buffer[0]) &nbsp; + &nbsp; SizeOf(Info)), &nbsp; @S[0], &nbsp; SizeOf(S), &nbsp; R);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;id[k-1]:= Info.idCommand;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CheckListBox1.Items.Add(S);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CheckListBox1.Checked[k-1]:=true;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;VirtualFreeEx(PRC, &nbsp; Buffer, &nbsp; 0, &nbsp; MEM_RELEASE);<br> &nbsp;CloseHandle(PRC);<br><br> &nbsp;MaxWidth := 0;<br> &nbsp;for i := 0 to CheckListBox1.Items.Count - 1 do<br> &nbsp;if MaxWidth &lt; CheckListBox1.Canvas.TextWidth(CheckListBox1.Items.Strings) then<br> &nbsp; &nbsp;MaxWidth := CheckListBox1.Canvas.TextWidth(CheckListBox1.Items.Strings);<br> &nbsp;SendMessage(CheckListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth+40, 0);<br>end;
 
非常谢谢大唐电信,不过有一个问题,要注意在Win2003下ToolbarWindow32是在SysPager下面,所以代码要想在2003下运行,则应做如下改动<br>v_hToolBar := FindWindowEx(FindWindowEx(v_hTrayNotify, 0, 'SysPager', nil), 0, 'ToolbarWindow32', nil);<br> &nbsp;if v_hToolBar = 0 then<br> &nbsp; &nbsp;v_hToolBar := FindWindowEx(v_hTrayNotify, 0, 'ToolbarWindow32', nil);
 
后退
顶部