怎样才能关闭某个已打开的程序,怎样才能打开IE去到指定的网址?(150分)

  • 主题发起人 主题发起人 gravel
  • 开始时间 开始时间
G

gravel

Unregistered / Unconfirmed
GUEST, unregistred user!
1、怎样才能关闭某个已打开的程序?
2、怎样才能打开IE去到指定的网址?
3,怎样才能程序在不激活(最小化)时,依然能工作,比如可以监视某个
程序是否已打开不止一个,若是,关闭其中一个!
 
1, 找到句柄, 发送 WM_QUIT 消息。
2,ShellExecute 可以搞定,看看帮助。
3,程序不激活当然能工作, 要不多任务何在?不明白你的意思。
4,可能不太理想,关键是怎么判断程序是一个?
 
1. MyHandle:=FindWindow('classname','caption');
if MyHandle<>0 then
PostMessage(MyHandle,wm_close,0,0);

2. ShellExecute(handle,nil,xxx.exe','open','path',SW_SHOWNORMAL);

3. 拦截消息
 
1、见下面程序段:
var HWndCalculator : HWnd;
begin
// find the exist calculator window
HWndCalculator := Winprocs.FindWindow(nil, '计算器')

// close the exist Calculator
if HWndCalculator <> 0 then
SendMessage(HWndCalculator, WM_CLOSE, 0, 0);
end;
2、procedure TForm1.HttpClick(Sender: TObject);
begin
ShellExecute(handle,'open','http://liangming.163.net',
nil,nil,SW_ShowNormal);
end;
3、看看http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=135840
 
1, 对不起, WM_CLOSE.
 
对于问题3,其实不用这么复杂:
在程序开始产生一个互斥量,如果产生时出现错误,则关闭程序;
否则正常运行程序,程序退出时,关闭互斥量,这种方法比较合理。
 
To:
All: 怎样得出一个程序的类名,我用了delphi自带的winsight,但不会用。
Kill Night:能给出一段源程序吗?

 
用 vc 带的 spy++ 比较好用,用一下就知道了。
 
1。找到打开程序窗口句柄。用FindWindow
关闭窗口。向此窗口发送wm_close消息
2。用ShellExecute函数。它在ShellApi单元中。
3。用EnumWindows、GetWindowText、PostMessage(wm_close....)函数即可
 
unit multinst;

interface

uses Forms, Windows, Dialogs, SysUtils;

// The following declaration is necessary because of an error in
// the declaration of BroadcastSystemMessage() in the Windows unit
function BroadcastSystemMessage(Flags: DWORD
Recipients: PDWORD;
uiMessage: UINT
wParam: WPARAM
lParam: LPARAM): Longint
stdcall;
external 'user32.dll';


const
MI_NO_ERROR = 0;
MI_FAIL_SUBCLASS = 1;
MI_FAIL_CREATE_MUTEX = 2;

{ Query this function to determine if error occurred in startup. }
{ Value will be one or more of the MI_* error flags. }
function GetMIError: Integer;

implementation

const
UniqueAppStr : PChar = 'I am the Eggman!';

var
MessageId: Integer;
WProc: TFNWndProc = Nil;
MutHandle: THandle = 0;
MIError: Integer = 0;

function GetMIError: Integer;
begin
Result := MIError;
end;

function NewWndProc(Handle: HWND
Msg: Integer
wParam, lParam: Longint):
Longint
stdcall;
begin

{ If this is the registered message... }
if Msg = MessageID then
begin
{ if main form is minimized, normalize it }
{ set focus to application }
if IsIconic(Application.Handle) then
begin
Application.MainForm.WindowState := wsNormal;
Application.Restore;
end;
SetForegroundWindow(Application.MainForm.Handle);
Result := 0;
end
{ Otherwise, pass message on to old window proc }
else
Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;

procedure SubClassApplication;
begin
{ We subclass Application window procedure so that }
{ Application.OnMessage remains available for user. }
WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
Longint(@NewWndProc)));
{ Set appropriate error flag if error condition occurred }
if WProc = Nil then
MIError := MIError or MI_FAIL_SUBCLASS;
end;

procedure DoFirstInstance;
begin
SubClassApplication;
MutHandle := CreateMutex(Nil, False, UniqueAppStr);
if MutHandle = 0 then
MIError := MIError or MI_FAIL_CREATE_MUTEX;
end;

procedure BroadcastFocusMessage;
{ This is called when there is already an instance running. }
var
BSMRecipients: DWORD;
begin
{ Don't flash main form }
Application.ShowMainForm := False;
{ Post message and inform other instance to focus itself }
BSMRecipients := BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
@BSMRecipients, MessageID, 0, 0);
Application.Terminate;
end;

procedure InitInstance;
begin
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = 0 then
{ Mutex object has not yet been created, meaning that no previous }
{ instance has been created. }
DoFirstInstance
else
BroadcastFocusMessage;
end;

initialization
MessageID := RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
if WProc <> Nil then
{ Restore old window procedure }
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
end.

 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部