L
L-M-delphi
Unregistered / Unconfirmed
GUEST, unregistred user!
我初接触delphi,因项目需要做一个数据采集的控件,不停扫描usb端口,收到数据后产生一个事件,把数据发送出去,自己摸索写了下,用activeform,却在postmessage时产生错误,不能通过,请高手帮看看或者改写下,以下是部分相关代码:另外我的代码在delphi下能很好执行,就是想做成控件时不会了unit FhsUsb372Impl1;{$WARN SYMBOL_PLATFORM OFF}interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ActiveX, AxCtrls, FhsUsb372Proj1_TLB, StdVcl, jpeg, ExtCtrls;const WM_DATA=WM_USER+108;type TFhsUsb372 = class(TActiveForm, IFhsUsb372) Image1: TImage; private { Private declarations } FEvents: IFhsUsb372Events; procedure Data(var msg:TMSG);message WM_DATA; protected { Protected declarations } procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override; procedure EventSinkChanged(const EventSink: IUnknown); override; function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall; procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall; function closePort: WordBool; safecall; function openPort: WordBool; safecall; function setPort(wCom: SYSINT): WordBool; safecall; function writePort(const OutS: WideString): WordBool; safecall; function Destory: WordBool; safecall; function MyThreadFunc(Pointer):Longint;stdcall; public { Public declarations } procedure Initialize; override; end;var hThread:Thandle;//定义一个句柄 ThreadIDWord; Wcom1:integer; dData:string; //采集的结果implementationuses ComObj, ComServ,CH375DLL;{$R *.DFM}function TFhsUsb372.MyThreadFunc(Pointer):Longint;//stdcall;//var// buff void;//之前的处理函数都去掉了,下一行postmessage不能通过,句柄换了好多个都没解决 postmessage(handle,WM_DATA,0,0); Application.ProcessMessages ; end;{ TFhsUsb372 }Procedure TFhsUsb372.Data(var msg:TMSG);begin FEvents.Recieve(12,'abcd') ; localFree(msg.lParam );end ;procedure TFhsUsb372.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);begin { Define property pages here. Property pages are defined by calling DefinePropertyPage with the class id of the page. For example, DefinePropertyPage(Class_FhsUsb372Page); }end;procedure TFhsUsb372.EventSinkChanged(const EventSink: IUnknown);begin FEvents := EventSink as IFhsUsb372Events; inherited EventSinkChanged(EventSink);end;procedure TFhsUsb372.Initialize;begin Wcom1:=0; inherited Initialize;end;function TFhsUsb372.openPort: WordBool;begin if hThread=0 then hthread:=CreateThread(nil,0,@TFhsUsb372.MyThreadfunc,nil,0,ThreadID); if hThread=0 then Result:=false; if hThread<>0 then begin ResumeThread(hThread); // 激活线程 Result:=true; end;end;function TFhsUsb372.Destory: WordBool;begin if hThread<>0 then TerminateThread(hThread,0); // 终止线程 CH375CloseDevice(Wcom1);end;initialization TActiveFormFactory.Create( ComServer, TActiveFormControl, TFhsUsb372, Class_FhsUsb372, 1, '', OLEMISC_INVISIBLEATRUNTIME, //OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL, tmBoth);end.