要实现这个功能:运行程序时,现在判断这个程序是否已经运行,是:就显示那个程序的窗口为当前窗口,没有就继续运行。

ansili 2003-03-17 02:27:17
要实现这个功能:运行程序时,现在判断这个程序是否已经运行,是:就显示那个程序的窗口为当前窗口,没有就继续运行。就是让程序只运行一次。最好给出代码。
...全文
467 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobox 2003-03-18
  • 打赏
  • 举报
回复
1。在程序启动的时候重载
procedure TMainForm.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WinClassName := APPNAME;
end;
设置应用程序的WinClassName
2。在工程文件中
RvHandle := FindWindow(APPNAME, nil);
if RvHandle > 0 then
begin
PostMessage(RvHandle, CM_RESTORE, 0, 0);
Exit;
end;
3。设置主窗体的CM_RESTORE事件,将窗体显示到最顶层就OK
ansili 2003-03-18
  • 打赏
  • 举报
回复
加上上面的代码,有这个错误,而且以后不能打开工程文件。
Error in module XXX:Call to Application createForm is Missing or Incorrect.


能不能说说这个错误的原因?应该怎样清除这个错误。谢谢!
分不是问题!!!!!!!!
ansili 2003-03-18
  • 打赏
  • 举报
回复
Thanks
youcheng1 2003-03-18
  • 打赏
  • 举报
回复
program Project1;

uses
Forms,
windows,
Unit1 in 'Unit1.pas' {Fmmain},
login in 'login.pas' {Fmlogin},
fm in 'fm.pas' {Formfm};

{$R *.res}
begin
CreateMutex(nil, True, 'appone');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '汽配管理管理系统已经运行了', '错误!' ,MB_ICONERROR);
Halt;
end;
begin
Application.Initialize;
application.Title:='欢迎使用汽配管理系统';
formfm:=tformfm.Create(application);
formfm.Show;
formfm.Update;
while formfm.Timer1.Enabled do
application.ProcessMessages;
formfm.Hide;
formfm.Free;
application.Title:='用户登陆';
fmlogin:=tfmlogin.Create(application);
fmlogin.ShowModal;
if fmlogin.Edit1.Text='' then
application.Terminate
else
begin
Application.Title := '汽配管理系统';
Application.CreateForm(TFmmain, Fmmain);
Application.Run;
end;
end;
end.
这个应该比较简单。
ansili 2003-03-18
  • 打赏
  • 举报
回复
加上上面的代码,都要出现有这个错误,而且以后不能打开工程文件。而且RUN选项不能使用。

Error in module XXX:Call to Application createForm is Missing or Incorrect.
能不能说说这个错误的原因?应该怎样清除这个错误。谢谢!
分不是问题!!!!!!!!
shang53 2003-03-18
  • 打赏
  • 举报
回复
program MP3Player;

uses
Forms,
Windows,
mp3 in 'mp3.pas' {Form1};
var
Hwnd : THandle;
{$R *.res}

{begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.}

begin
Hwnd:=FindWindow('TForm1','MP3Player'); //查找是否已有窗体MainForm
if Hwnd<>0 then
begin
SetForegroundWindow(Hwnd); //激活已運行的程序實例
Application.Terminate; //中止本次實例
end
else
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;//執行本次實例
end;
end.
  • 打赏
  • 举报
回复
首先在工程文件里定义
const
CM_RESTORE = WM_USER + $1000; //自定义消息
MyAppName ='方舟客户管理系统';
var
RvHandle : hWnd; //主窗口句柄
begin // 避免程序二次运行
RvHandle := FindWindow(MyAppName,nil);
if RvHandle > 0 then
begin
SendMessage(RvHandle,CM_RESTORE,0,0); //发送自定义消息
Application.MessageBox('客户管理系统已经运行!',
'感谢你的使用',
MB_OK);
Halt;
end;
Application.Initialize;

...
end.

然后在主窗口的Pas文件里自定义一条和工程文件里一样的消息(注意保持一致):
const
CM_RESTORE = WM_USER + $1000; //自定义消息
MyAppName = '方舟客户管理系统';
声明:
private
procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
protected
procedure CreateParams(var Params: TCreateParams); override;

implementation:

procedure TMainForm.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WinClassName := MyAppName;
end;

procedure TMainForm.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) then //判断程序是否最小化
Application.Restore //如果是则恢复窗体
else
Application.BringToFront; //否则移至屏幕最前端
end;
  • 打赏
  • 举报
回复
program Project1;

uses
Forms,
windows,
SysUtils,
Dialogs,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

var
hMutex:HWND;
Error:integer;

begin
Application.Initialize;
hMutex:=CreateMutex(nil,False,'Only One Instance');
Error:=GetLastError;
if Error<>ERROR_ALREADY_EXISTS Then
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
MessageDlg('已经有一个程序的实例在运行!',mtInformation,[mbOK],0);
ReleaseMutex(hMutex);
end;
end.

joky1981 2003-03-17
  • 打赏
  • 举报
回复
const
UniqueAppstr='I_AM_UNIQUE_APP';

var
MessageID:Integer;
Wproc:TFNWndProc;
MutHandle:Thandle;
Form1: TForm1;

implementation

{$R *.DFM}

//新的窗口处理过程
function NewWndProc(Handle:HWND;Msg:Integer;wParam,lParam:longint):
longint;stdcall;
begin
Result:=0;
if Msg=MessageID then
begin
//在这里您可以做其他事情,我在这里只是简单的显示一条消息
showmessage('我已经运行了,不要再运行我!');
end
else
//其他的消息则调用旧的窗口处理过程
Result:=CallWindowProc(WProc,Handle,Msg,wParam,lParam);
end;



procedure TForm1.FormCreate(Sender: TObject);
var
BSMRecipients:DWORD;
begin
BSMRecipients:=BSM_APPLICATIONS;
//注册一个消息,并创建一个唯一的消息标识符
MessageID:=RegisterWindowMessage(UniqueAppstr);
//子类化窗口,消息由新的窗口处理程序处理
WProc:=TFNWndProc(SetWindowLong(Application.Handle,GWL_WNDPROC,Longint(@NewWndProc)));

//打开互斥对象,判断是否存在
MutHandle:=OpenMutex(MUTEX_ALL_ACCESS,False,UniqueAppstr);

if MutHandle=0 then //若不存在,表明第一次运行,则创建一互斥对象
MutHandle:=CreateMutex(nil,False,UniqueAppstr)
else
begin
//若存在,表明已经有实例运行,则向广播消息给所有的顶级窗口,并退出程序
Application.ShowMainForm:=False;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,@BSMRecipients,MessageID,0,0);
Application.Terminate();
end;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SetWindowLong(Application.Handle,GWL_WNDPROC,Longint(WProc));
CloseHandle(MutHandle);
end;

end.
naughtyboy 2003-03-17
  • 打赏
  • 举报
回复
program Project1;

uses
Forms,
Windows,
Messages,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';

{$R *.res}

var
hMutex : Thandle;
WaitResult : word;
BroadcastList : DWORD;

begin

MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
// register a message to use later on
hMutex := createMutex(nil,false,pchar('App_Choice'));
// grab a mutex handle
WaitResult := WaitForSingleObject(hMutex,10); // wait to see if we can have exclusive use of the mutex
if ( waitResult = WAIT_TIMEOUT ) then
// if we can't then broadcast the message to make the owner of the mutex respond

{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0);
//32 bit - broadcast the message to all apps - only a prev inst will hear it.
end
else
begin
{ do the normal stuff}
Application.Title := 'Choice Organics Purchase & Sales System';
Application.CreateForm(TForm1, Form1);
Application.Run;
ReleaseMutex(hMutex);
// release the mutex as a politeness

end;
CloseHandle(hMutex);
// close the mutex handle


end.
knife_s 2003-03-17
  • 打赏
  • 举报
回复
up
DelphiStudy 2003-03-17
  • 打赏
  • 举报
回复
UP
ansili 2003-03-17
  • 打赏
  • 举报
回复
加上上面的代码,有这个错误,而且以后不能打开工程文件。
Error in module XXX:Call to Application createForm is Missing or Incorrect.
什么原因?
smilelhh 2003-03-17
  • 打赏
  • 举报
回复
在工程文件里:
var
hMutex: HWND;
Ret: Integer;

{$R *.res}

begin
Application.Initialize;
Application.Title := 'XXXX';
{hMutex:=CreateMutex(nil,False,'XXXX');
Ret:=GetLastError;
If Ret=ERROR_ALREADY_EXISTS Then
ReleaseMutex(hMutex)
else
begin
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end;
end.
sysu 2003-03-17
  • 打赏
  • 举报
回复
var
hMutex : Thandle;
WaitResult : word;
BroadcastList : DWORD;
begin
MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
// register a message to use later on
hMutex := createMutex(nil,false,pchar('App_Choice')); // grab a mutex
handle
WaitResult := WaitForSingleObject(hMutex,10); // wait to see
if we can have exclusive use of the mutex
if ( waitResult = WAIT_TIMEOUT ) then // if we can't then broadcast
the message to make the owner of the mutex respond

{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); //32 bit - broadcast the
message to all apps - only a prev inst will hear it.
end
else
begin
{ do the normal stuff}
Application.Title := 'Choice Organics Purchase & Sales System';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
ReleaseMutex(hMutex); // release the mutex as a politeness

end;
CloseHandle(hMutex); // close the mutex handle
end.

This goes in the MainForm

procedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);
begin
{ If it's the special message then focus on this window}
if Msg.Message = MessageID then // if we get the broadcast message from an
another instance of this app that is trying to start up
begin
show;
WindowState := wsMaximized;
BringToFront;
SetFocus;
Handled := true;

end;
end;

//And this goes in the TMainForm.FormCreate ;-

Application.OnMessage:= OnAppMessage;
【源码免费下载链接】:https://renmaiwang.cn/s/2gdnj 《R语言数据挖掘方法及应用》由薛薇编写而成的一本系统阐述R语言在数据挖掘领域前沿技术的著作。该书旨在指导读者学会使用R语言进行高效、实用的数据分析与建模工作,涵盖了从理论基础到实践操作的全过程。作为一款功能强大且开源的统计计算和图形处理平台,R语言凭借其丰富的工具库和社区支持,在数据分析与可视化方面展现出显著优势。在数据挖掘领域,R语言提供了包括`caret`、`randomForest`、`tm`、`e1071`等广泛使用的专用包,这些工具能够帮助用户更便捷地进行数据预处理、特征选择、模型构建和结果评估。全书首先介绍R语言的基本知识体系,涵盖环境配置与安装方法、基础语法规范以及常见数据类型分析等内容。这些基础知识是开展后续数据分析工作的必备技能,通过学习可以快速掌握R语言的核心功能。随后章节深入讲解了数据挖掘的主要概念与流程,包括数据清洗、转换整理和探索性分析等环节,同详细阐述了分类、聚类、关联规则挖掘及预测等多种典型任务的具体实施方法。这些内容有助于读者全面理解数据挖掘的整体架构及其核心工作步骤。在应用实践部分,薛薇老师结合真实案例展示了R语言在实际业务场景中的具体运用,例如市场细分分析、客户流失预测以及个性化推荐系统等。通过这些案例研究,读者可以深入学习如何利用相关工具包解决实际问题,并提升数据分析能力。此外,书中配套的“案例数据集”和“代码资源”为读者提供了实践操作的机会,使理论知识能够更好地转化为动手技能。通过实际操作分析,读者可以加深对R语言数据挖掘方法的理解并灵活运用。总之,《R语言数据挖掘方法及应用》是一部全面讲解R语言在数据分析与建模领域的教材,无论你是刚开始学习的新人还是经验丰富的专业人士,都能从中获益匪浅。通过深入研读此书,你可以掌握R语言的数据挖掘技巧,并将其应用到实
内容概要:本文提出了一种基于改进粒子滤波算法的无人机三维航迹预测方法,并通过Matlab代码实现仿真验证。该方法针对传统粒子滤波在无人机轨迹预测中存在的粒子退化和计算复杂度高等问题,引入优化策略提升滤波精度与效率,有效提高了对无人机运动轨迹的非线性、非高斯环境下的预测能力。文中详细阐述了算法原理、模型构建流程及关键步骤,包括状态转移建模、观测方程设计、重采样优化等,并结合三维空间中的实际飞行轨迹进行仿真实验,验证了所提方法相较于标准粒子滤波在位置预测误差和收敛速度方面的优越性。; 适合人群:具备一定信号处理、导航估计算法基础,熟悉Matlab编程,从事无人系统、智能交通、航空航天等相关领域研究的研究生或科研人员; 使用场景及目标:①应用于无人机实轨迹预测与状态估计系统中,提升飞行安全性与自主性;②为复杂环境下非线性动态系统的建模与滤波算法研究提供技术参考;③【预测】改进粒子滤波的无人机三维航迹预测方法(Matlab代码实现)支持后续扩展至多无人机协同跟踪与避障系统的设计与仿真; 阅读建议:建议结合Matlab代码逐模块分析算法实现细节,重点关注粒子滤波的改进机制与三维可视化结果对比,同可尝试替换不同运动模型或噪声条件以深入理解算法鲁棒性。

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧