一个不是太简单的问题,呵,大家多多关注!!!! 火烧屁股了,大家都来看看呀! (100分)

  • 主题发起人 主题发起人 天真
  • 开始时间 开始时间

天真

Unregistered / Unconfirmed
GUEST, unregistred user!
program Project1;<br><br>uses<br>&nbsp; Forms,messages,windows,Dialogs;<br><br>const<br>&nbsp; WM_MYSELF = WM_USER +1000;<br>{$R *.RES}<br><br><br>procedure test(handle:HWnd);stdcall;External'project2.dll';<br>procedure ShowCCC;stdcall;External'project2.dll';<br>procedure InputCCC(Point: Pointer);stdcall;External'project2.dll';<br><br>procedure MYTEST(var message: TMessage); message WM_MYSELF;<br>begin<br>&nbsp; showmessage('ok');<br>end;<br><br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; showmessage('ok1');<br>&nbsp; test(application.Handle);<br>&nbsp; Application.Run;<br><br>end.
 
这样肯定编译通不过,你的消息谁来接受,应该写在每个窗口李
 
那么如果是没有窗体的程序就不能接受消息??????!!!!
 
其实我只想实现一个功能,就是向没有窗体的程序发消息,没有窗体的程序接收消息而已
 
在 windows 操作系统中,没有建立窗体的线程是不能接受消息的。当一个线程<br>没有创建窗体的时候,它被认为是一个工作者线程(计算),不接受任何消息。<br>当它创建的窗体,才有相应的消息队列和与之对应的窗口过程......
 
<br>&nbsp; 应该是这样。<br>&nbsp; Windows 的消息处理技术,是基于WIndow(窗体)上的输入而进行的处理技术。<br>没有窗体,谁来接收各种消息? 窗体接收到发给他的消息后,调用postmessage()将<br>消息发送到Windows的消息队列,等待Windowproc处理.
 
是这样子的
 
我在这儿再提个问题,<br>我看到APPLICATION。ONMESSAGE这么个事件,<br>我再看别人的讨论,他们说是由APPLICATION收到消息处理后再转发给窗体的,如果照你们这么说的话<br>如果没有窗体,APPLICATION。ONMESSAGE不是不能用了?<br>还有,我目前在做一个项目,我做一个DLL,由我的DLL监听别人发过来的数据,如果有数据,<br>我再通知别人写的服务程序,来接收我发给服务程序的数据,你们说该怎么做好????
 
还有你看NT中的有些服务还不是可以手工地把他们关掉,我觉得关闭该服务的方法也应该是用<br>消息的吧!!<br>大家帮帮我!<br>急!!!!!!!!!!!!!
 
不知道咋办:(<br>帮你提前。
 
其一: 建立线程,线程中用GetMessage捕捉消息,发送端PostThreadMessage;<br>其二: 用xxAPI(忘了这个API)了,建立虚窗口,返回值为窗口句柄,<br>&nbsp; &nbsp; &nbsp; &nbsp;好像参数其中一个为一消息回调函数!<br>&nbsp; &nbsp; &nbsp; 直接向该Handle发送消息了!<br>
 
to: 天真: <br>&nbsp;unit Unit1; <br>&nbsp; <br>&nbsp; interface <br>&nbsp; <br>&nbsp; uses <br>&nbsp; &nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, <br>&nbsp; &nbsp; Dialogs, DBTables, Db; <br>&nbsp; <br>&nbsp; const <br>&nbsp; &nbsp; WM_OPENDATASET = WM_USER + 1; <br>&nbsp; &nbsp; WM_EXECUTESQL &nbsp;= WM_USER + 2; <br>&nbsp; <br>&nbsp; type <br>&nbsp; &nbsp; TThreadDataSet = class(TThread) <br>&nbsp; &nbsp; private <br>&nbsp; &nbsp; &nbsp; procedure WMOpenDataSet(Msg: TMsg); <br>&nbsp; &nbsp; &nbsp; procedure WMExecSQL(Msg: TMsg); <br>&nbsp; &nbsp; protected <br>&nbsp; &nbsp; &nbsp; procedure Execute; override; <br>&nbsp; &nbsp; public <br>&nbsp; &nbsp; &nbsp; procedure Open(DataSet: TDataSet); <br>&nbsp; &nbsp; &nbsp; procedure ExecSQL(DataSet: TDataSet); <br>&nbsp; &nbsp; end; <br>&nbsp; <br>&nbsp; &nbsp; TForm1 = class(TForm) <br>&nbsp; &nbsp; &nbsp; procedure FormCreate(Sender: TObject); <br>&nbsp; &nbsp; &nbsp; procedure FormDestroy(Sender: TObject); <br>&nbsp; &nbsp; private <br>&nbsp; &nbsp; &nbsp; { Private declarations } <br>&nbsp; FThread : TThreadDataSet; <br>&nbsp; &nbsp; public <br>&nbsp; &nbsp; &nbsp; { Public declarations } <br>&nbsp; &nbsp; end; <br>&nbsp; <br>&nbsp; var <br>&nbsp; &nbsp; Form1: TForm1; <br>&nbsp; <br>&nbsp; implementation <br>&nbsp; <br>&nbsp; {$R *.DFM} <br>&nbsp; <br>&nbsp; procedure TThreadDataSet.ExecSQL(DataSet: TDataSet); <br>&nbsp; begin <br>&nbsp; &nbsp; PostThreadMessage(ThreadID, WM_EXECUTESQL, Integer(DataSet), 0); <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TThreadDataSet.Execute; <br>&nbsp; var <br>&nbsp; &nbsp; Msg : TMsg; <br>&nbsp; <br>&nbsp; begin <br>&nbsp; &nbsp; FreeOnTerminate := True; <br>&nbsp; &nbsp; PeekMessage(Msg, 0, WM_USER, WM_USER, PM_NOREMOVE); <br>&nbsp; <br>&nbsp; &nbsp; while not Terminated do begin <br>&nbsp; &nbsp; &nbsp; if GetMessage(Msg, 0, 0, 0) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case Msg.Message of <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WM_OPENDATASET: WMOpenDataSet(Msg); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WM_EXECUTESQL: &nbsp;WMExecSQL(Msg); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TThreadDataSet.Open(DataSet: TDataSet); <br>&nbsp; begin <br>&nbsp; &nbsp; PostThreadMessage(ThreadID, WM_OPENDATASET, Integer(DataSet), 0); <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TThreadDataSet.WMExecSQL(Msg: TMsg); <br>&nbsp; var <br>&nbsp; &nbsp; Qry : TQuery; <br>&nbsp; <br>&nbsp; begin <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; Qry := TQuery(Msg.wParam); <br>&nbsp; &nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; &nbsp; Qry.Open; <br>&nbsp; &nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; &nbsp; Qry.ExecSQL; <br>&nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; On E: Exception do <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage(E.Message); <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TThreadDataSet.WMOpenDataSet(Msg: TMsg); <br>&nbsp; var <br>&nbsp; &nbsp; Ds : TDataSet; <br>&nbsp; <br>&nbsp; begin <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; Ds := TDataSet(Msg.wParam); <br>&nbsp; &nbsp; &nbsp; Ds.Open; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; On E: Exception do <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage(E.Message); <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; // --------------------------------------- // <br>&nbsp; <br>&nbsp; procedure TForm1.FormCreate(Sender: TObject); <br>&nbsp; begin <br>&nbsp; &nbsp; FThread &nbsp;:= TThreadDataSet.Create(False); <br>&nbsp; &nbsp; FThread.Open(Table1); &nbsp; // Opening a dataset (table or query) <br>&nbsp; &nbsp; FThread.ExecSQL(Query1); &nbsp;// Executing a SQL <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TForm1.FormDestroy(Sender: TObject); <br>&nbsp; begin <br>&nbsp; &nbsp; FThread.Terminate; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; end. <br>
 
不用消息,也可以用事件内核对象.<br>在服务程序建立一工作者线程,初始化中:<br>Datareadevent := createevent(nil,true,false,'Datareadevent');<br>执行中:<br>while true do<br>begin<br>&nbsp;waitforsingleobject(Datareadevent,INFINITE);<br>&nbsp;resetevent(Datareadevent);<br>&nbsp;接受数据......<br>end;<br>在你的 Dll 中...<br>while true do<br>begin<br>&nbsp;if 有数据 then<br>&nbsp;begin<br>&nbsp; Datareadevent:= openevent(nil,false,'Datareadevent');<br>&nbsp; setevent(Datareadevent);<br>&nbsp;end; &nbsp;<br>end;<br>
 
谢谢tan_jian,<br>好象是可以了,但我先试了,再给你分好吗?<br>另开个贴子!
 
xeen, 也谢谢你,
 
欢迎大家来看看,希望也能从中得到点什么
 
谢谢大家
 
后退
顶部