然后
//button的onclick事件
procedure TTabShell.AppName_BtnClickHandle(Sender:TObject);
var wnd:HWND;
begin
with Sender as TBitBtn do
begin
wnd:=Windows.FindWindow(nil,PChar(Hint));
Image1.Picture.Icon.Handle := MyGetWindowIcon(wnd);
ShowWindow(wnd,SW_SHOW); // agui: 原来是SW_NORMAL
SetForegroundWindow(wnd);
end;
end;
问题已经差不多解决,首先感谢NowCan,他的WndToProc(hwnd As Long) As String使Handle和WindowName联系起来;
再次感谢CoolSlob(),是他不厌其烦帮我想办法,并且教了我一些技巧。
最后感谢给我回复或关注的朋友^_*
http://www.csdn.net/Expert/TopicView1.asp?id=884142 是我最后的代码,
大家感兴趣的话可以去看看,并且接分^_^
//响应热键,进行抓图
procedure Tcashform.WMHOTKEY(var Message:TMessage);
begin
if iBitmapValide then
begin
tmpBitmap.FreeImage;
tmpBitmap.Free;
iBitmapValide:=False;
end;
CaptureControl;
end;
procedure Tcashform.WMBarIcon (var Message:TMessage);
begin
if ((Message.LParam = WM_RBUTTONDOWN)or (Message.LParam = WM_LBUTTONDOWN)) then
//在任务栏图标上按下鼠标键退出
close;
end;
procedure Tcashform.FormCreate(Sender: TObject);
var
lpData:PNotifyIconData;
help:thelpform;
begin
help:=thelpform.create(self);
help.showmodal;
//注册热键为Ctrl+Shift+A
RegisterHotKey(cashform.handle,0,{MOD_SHIFT OR MOD_CONTROL}0,65);
//在任务栏上建立图标
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;
lpData.Wnd := cashform.Handle;
lpData.hIcon:=cashform.Icon.Handle;
lpData.uCallbackMessage := WM_ICONMESSAGE;
lpData.uID :=0;
lpData.szTip := '屏幕捕捉';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD,lpData);
dispose(lpData);
end;
procedure Tcashform.FormClose(Sender: TObject; var Action: TCloseAction);
var
lpData:PNotifyIconData;
begin
//退出时删除任务栏图标
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;
lpData.Wnd := cashform.Handle;
lpData.hIcon := cashform.Icon.Handle;
lpData.uCallbackMessage := WM_ICONMESSAGE;
lpData.uID :=0;
lpData.szTip := '屏幕捕捉';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_DELETE,lpData);
dispose(lpData);
//退出时注销窗口热键
UnRegisterHotKey(cashform.Handle,0);
if iBitmapValide then
begin
tmpBitmap.FreeImage;
tmpBitmap.Free;
iBitmapValide:=False;
end;
end;
procedure Tcashform.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
fenxi:tfenxiform;
begin
//按下鼠标左键保存窗口图象
if (Button=mbLeft)then
// with SavePic do
// begin
// DefaultExt := 'Bmp';
// Execute;
// if FileName<>'' then
tmpBitmap.SaveToFile(Getcurrentdir+'\microscope.bmp');
// end;
//保存完图象后删除位图
tmpBitmap.FreeImage;
tmpBitmap.Free;
iBitmapValide:=False;
cashform.Visible := False;
fenxi:=tfenxiform.create(self);
fenxi.showmodal;
end;
这里有一个取得与某个文件相关联的应用程序的图标:
uses shellapi;
var
IconIndex : word;
h : hIcon;
begin
IconIndex := 0;
h := ExtractAssociatedIcon(hInstance,
PChar(filename),
IconINdex);
//将图标显示出来DrawIcon(image1.Canvas.Handle,0,0,h);
end;
以下是例出任务栏窗体:
function GetText(Wnd : HWND):string;
var textlength : integer;
text : PChar;
begin
textlength:=SendMessage(Wnd,WM_GETTEXTLENGTH,0,0);
if textlength=0 then Result := ''
else
begin
getmem(text,textlength+1);
SendMessage(Wnd,WM_GETTEXT,textlength+1,Integer(text));
Result:=text;
freemem(text);
end;
end;
Function EnumWindowsProc (Wnd: HWND; LParam: LPARAM): BOOL; stdcall;
begin
Result := True;
if (IsWindowVisible(Wnd) or IsIconic(wnd)) and
((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
(GetWindowLong(Wnd, GWL_HWNDPARENT) = GetDesktopWindow)) and
(GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
Form1.Listbox1.items.add('Handle: ' + Inttostr(Wnd) + ',Text: ' +GetText(Wnd));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Param : Longint;
begin
EnumWindows(@EnumWindowsProc , Param);
end;
Public Function WndToProc(hwnd As Long) As String
Dim PID As Long
Dim pl As PROCESSENTRY32
Dim hSnapshot As Long
WndToProc = ""
GetWindowThreadProcessId hwnd, PID
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnapshot Then
pl.dwSize = Len(pl)
If (Process32First(hSnapshot, pl)) Then
Do
If PID = pl.th32ProcessID Then
WndToProc = pl.szExeFile
End If
Loop Until (Process32Next(hSnapshot, pl) < 1)
End If
CloseHandle (hSnapshot)
End If
Public Function WndToProc(hwnd As Long) As String
Dim PID As Long
Dim pl As PROCESSENTRY32
Dim hSnapshot As Long
WndToProc = ""
GetWindowThreadProcessId hwnd, PID
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnapshot Then
pl.dwSize = Len(pl)
If (Process32First(hSnapshot, pl)) Then
Do
If PID = pl.th32ProcessID Then
WndToProc = pl.szExeFile
End If
Loop Until (Process32Next(hSnapshot, pl) < 1)
End If
CloseHandle (hSnapshot)
End If