Forms delphi Activex和父窗体错误

Forms delphi Activex和父窗体错误,forms,delphi,activex,delphi-xe7,Forms,Delphi,Activex,Delphi Xe7,我试图修复activex项目,但出现了错误,我的activex项目中有两个表单第一个表单保持tmemo和调用第二个表单作为父表单的按钮到目前为止一切正常,但我无法设置从第二个表单到第一个表单控件的任何记录始终获取访问冲突,所以我决定在第一个表单中设置tmemo.text控件之前显示结果,实际上结果正在显示,但无法设置为第一种形式这里是我的项目代码 unit main1; {$WARN SYMBOL_PLATFORM OFF} interface uses Windows, Messag

我试图修复activex项目,但出现了错误,我的activex项目中有两个表单第一个表单保持
tmemo
和调用第二个表单作为父表单的按钮到目前为止一切正常,但我无法设置从第二个表单到第一个表单控件的任何记录始终获取访问冲突,所以我决定在第一个表单中设置
tmemo.text
控件之前显示结果,实际上结果正在显示,但无法设置为第一种形式这里是我的项目代码

unit main1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, embed_TLB, StdVcl, Vcl.StdCtrls;

type
  Tform1 = class(TForm, Iform1)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }


  protected
    { Protected declarations }

  public
    { Public declarations }
    procedure showEmo(L,T:Integer);

  end;

  var
  Form1 : Tform1;

implementation

uses ComObj, ComServ, main2;

{$R *.DFM}

{ Tform1 }

procedure Tform1.Button1Click(Sender: TObject);
var
  Rect: TRect;
begin
  GetWindowRect(Self.button1.Handle, Rect);
  showEmo(Rect.Left + 70,(Rect.Top - 290));
end;

procedure Tform1.FormCreate(Sender: TObject);
begin
  Form2 := TForm2.Createparented(0);
end;

procedure TForm1.showEmo(L,T:Integer);
var
  Rect: TRect;
begin
  try
    GetWindowRect(button1.Handle, Rect);
    begin
      Form2.FormStyle := fsStayOnTop;
    end;
    Form2.Left := L;//Rect.Left;
    Form2.top := T;//Rect.Top - emo.Height;
  finally
    Form2.Visible := not (Form2.visible);
  end;

end;

initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    Tform1,
    Class_form1,
    0,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);
end.
表格2

unit main2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw_EWB, EwbCore,
  EmbeddedWB, MSHTML_EWB, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    ewbpage: TEmbeddedWB;
    load: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure ewbpageBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
      var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
      var Cancel: WordBool);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses main1;

{$R *.dfm}

procedure TForm2.ewbpageBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
  var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
  var Cancel: WordBool);
  var
  MousePos: TPoint;
  HtmlElement: IHTMLElement;
  iHTMLDoc: IHtmlDocument2;
begin
  if Pos('#sm',URL)>0 then
  begin
    if Supports(ewbpage.Document, IHtmlDocument2, iHTMLDoc) then
    begin
      if GetCursorPos(MousePos) then
      begin
        MousePos := ewbpage.ScreenToClient(MousePos);
        HtmlElement := iHTMLDoc.ElementFromPoint(MousePos.X, MousePos.Y);
        if Assigned(HtmlElement) then
          showmessage(HtmlElement.getAttribute('id', 0));
        form1.Memo1.Text :=  HtmlElement.getAttribute('id', 0);
        Cancel := True;
        Self.Close;
      end;
    end;
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  ewbpage.LoadFromStrings(load.Lines);
end;

end.
问题是我为什么会犯这个错误

模块“EMBEDA~1.OCX”中地址07C734FC处的访问冲突。读到 地址:000003B4

在这条线上

form1.Memo1.Text :=  HtmlElement.getAttribute('id', 0);
为什么我不能将结果从第二个表单设置为第一个表单?我在这里做错的是为了更好地理解整个项目


一旦开始正确格式化代码,您将看到此问题

procedure TForm2.ewbpageBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
  var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
  var Cancel: WordBool);
  var
  MousePos: TPoint;
  HtmlElement: IHTMLElement;
  iHTMLDoc: IHtmlDocument2;
begin
  if Pos('#sm',URL)>0 then
  begin
    if Supports(ewbpage.Document, IHtmlDocument2, iHTMLDoc) then
    begin
      if GetCursorPos(MousePos) then
      begin
        MousePos := ewbpage.ScreenToClient(MousePos);
        HtmlElement := iHTMLDoc.ElementFromPoint(MousePos.X, MousePos.Y);

        // if we have a valid HtmlElement ...
        if Assigned(HtmlElement) 
        then // show a message
          showmessage(HtmlElement.getAttribute('id', 0));

        // now we do not care about if HtmlElement is valid or not
        form1.Memo1.Text :=  HtmlElement.getAttribute('id', 0);

        Cancel := True;
        Self.Close;
      end;
    end;
  end;
end;
要仅解决当前的访问冲突,只需在将使用
HtmlElement
的所有行周围放置一个
begin-end

        HtmlElement := iHTMLDoc.ElementFromPoint( MousePos.X, MousePos.Y );

        if Assigned( HtmlElement ) 
        then
          begin
            showmessage( HtmlElement.getAttribute( 'id', 0 ) );
            form1.Memo1.Text := HtmlElement.getAttribute( 'id', 0 );
          end;

但是代码中还有一些问题。您不应该使用全局变量
form1
form2
。相反,将表单实例传递给创建的
TForm2
实例,或者更好的是传递回调方法。

我建议您进行一些调试。查找执行无效访问的代码。我已经调试了项目,始终在此行引发错误
form1.Memo1.Text:=HtmlElement.getAttribute('id',0)但我找不到itSo的任何错误AV在哪里?在rhs上的表达式中,或在lhs上的表达式中。深入挖掘。看来form1是form2无法访问的!你什么意思是无法访问?如果你能看到我在form2的使用中包含了form1。添加begin和end将定义起点和终点感谢这一通知,但这不会解决我的访问冲突,我对最后几句话很感兴趣,
而是传递表单实例以创建TFORM2更好的回调
有关于此的示例吗?