Delphi: TOleControl ทำให้ ActiveControl อยู่ในสถานะผิดใช่ไหม

ใน Virtual Treeview ของ Mike Lischke มีการเพิ่มโค้ดวิธีแก้ปัญหาเพื่อแก้ไขข้อบกพร่องเมื่อใช้ < strong>TWebBrowser ควบคุมในรูปแบบเดียวกัน

ปัญหาคือหากผู้ใช้พยายามโต้ตอบกับ TOleControl (ซึ่ง TWebBrowser ลงมา) การคลิกเมาส์ครั้งแรกจะถูกกิน พวกเขาจะต้องคลิกอีกครั้งเพื่อให้มีสมาธิในการควบคุม จากนั้น พวกเขาสามารถโต้ตอบกับการควบคุมได้

เขามีความคิดเห็นที่จะอธิบาย:

ทุกการควบคุมที่ได้รับจาก TOleControl อาจมีปัญหาในการโฟกัส

เพื่อหลีกเลี่ยงการรวมหน่วย OleCtrls (ซึ่งจะรวมถึง ตัวแปร) ซึ่งจะอนุญาตให้ทดสอบคลาส TOleControl อินเทอร์เฟซ IOleClientSite จะใช้สำหรับ ทดสอบซึ่งได้รับการสนับสนุนโดย TOleControl และเป็นตัวบ่งชี้ที่ดี

จากตัวอย่างเต็ม:

procedure TBaseVirtualTree.WMKillFocus(var Msg: TWMKillFocus);
var
  Form: TCustomForm;
  Control: TWinControl;
  Pos: TSmallPoint;
  Unknown: IUnknown;
begin
  inherited;

  [snip]

  {
    Workaround for wrapped non-VCL controls (like TWebBrowser), 
    which do not use VCL mechanisms and 
    leave the ActiveControl property in the wrong state, 
    which causes trouble when the control is refocused.
  }
  Form := GetParentForm(Self);
  if Assigned(Form) and (Form.ActiveControl = Self) then
  begin
    Cardinal(Pos) := GetMessagePos;
    Control := FindVCLWindow(SmallPointToPoint(Pos));
    {
      Every control derived from TOleControl has potentially 
      the focus problem. In order to avoid including 
      the OleCtrls unit (which will, among others, include Variants),  
      which would allow to test for the TOleControl
      class, the IOleClientSite interface is used for the test, 
      which is supported by TOleControl and a good indicator.
    }
    if Assigned(Control) and Control.GetInterface(IOleClientSite, Unknown) then
      Form.ActiveControl := nil;

    // For other classes the active control should not be modified. Otherwise you need two clicks to select it.
  end;
end;

ปัญหาคือวิธีแก้ปัญหาไม่ทำงานสำหรับฉันอีกต่อไป และบอกตามตรงว่าฉันไม่รู้ว่าจริงๆ แล้วปัญหาคืออะไร และวิธีแก้ปัญหาของเขาแก้ไขได้อย่างไร

มีใครบ้างที่รู้ว่าความคิดเห็นของเขาเข้าใจในสิ่งที่เขาพูดถึง สามารถอธิบายได้ว่าปัญหาคืออะไร และสิ่งที่เขาทำอยู่จะแก้ไขได้อย่างไร

วิธีแก้ปัญหาสำหรับการควบคุมที่ไม่ใช่ VCL แบบห่อ (เช่น TWebBrowser) ซึ่งไม่ได้ใช้กลไก VCL และปล่อยให้คุณสมบัติ ActiveControl อยู่ในสถานะที่ไม่ถูกต้อง ซึ่งทำให้เกิดปัญหาเมื่อมีการปรับโฟกัสการควบคุม การควบคุมทุกอย่างที่ได้รับจาก TOleControl อาจมีปัญหาในการโฟกัส

รหัสไปถึงที่ตั้งใจไว้

Form.ActiveControl := nil; 

คำสั่ง แต่มันก็ไม่ได้ทำเคล็ดลับ

ฉันจะแก้ไข แต่ฉันไม่รู้ว่าเขาพบมันได้อย่างไร หรือเหตุใด TOleControl จึงไม่ ใช้กลไก VCL และปล่อยให้คุณสมบัติ ActiveControl อยู่ในสถานะที่ไม่ถูกต้อง< /em>.


โบนัสการอ่าน

เดิมทีฉันถามคำถามนี้ ใน borland.public.delphi.nativeapi.win32 newsgroup ในปี 2551

คำถามเกี่ยวกับฟอรัม Soft-Gems

บั๊ม 20110515 (12 เดือนต่อมา)

Bump 20150401 (7 ปีต่อมา): ยังใช้งานไม่ได้ใน XE6

บั๊ม 20210309 (11 ปีต่อมา)


person Ian Boyd    schedule 13.05.2009    source แหล่งที่มา
comment
Virtual TreeView หมดอายุแล้ว เวอร์ชั่นล่าสุดเมื่อ 14.mars 09? อาจเป็นส่วนประกอบ Delphi ที่ยิ่งใหญ่ที่สุดเท่าที่เคยมีมา IMHO   -  person Tom    schedule 13.05.2009
comment
มันหายไปเมื่อฉันถามคำถามครั้งแรก เมื่อ Mike เปลี่ยนไปใช้การพัฒนา Mac และ MySQL พบว่า VT ไม่ค่อยดีนัก มีการสนับสนุนเป็นระยะๆ แต่ไม่มีอะไรเป็นทางการ   -  person Ian Boyd    schedule 13.05.2009
comment
ขณะติดแท็กคำถามนี้ใหม่เพื่อใช้แท็กที่มีอยู่ ฉันพบว่าแท็ก tvirtualtreeview มีอยู่แล้วในคำถามนี้: stackoverflow.com/questions/687438 . คุณจะรู้ดีกว่าว่าอันไหนถูกต้อง ดังนั้นคุณจะเปลี่ยนอันใดอันหนึ่งได้ไหม ขอบคุณ.   -  person Michael Myers    schedule 14.05.2009
comment
ไมค์เรียกผลิตภัณฑ์ดังกล่าวว่า Virtual Treeview และหากคุณค้นหา virtualtreeview และ tvirtualtreeview ใน Google คุณจะพบรายการอื่นๆ อีกมากมายสำหรับรายการแรก ดังนั้นฉันจึงเปลี่ยนแท็กของคำถามอื่นจาก tvirtualtreeview เป็น virtualtreeview   -  person lkessler    schedule 14.05.2009
comment
เวอร์ชันล่าสุดของ VirtualTreeview 4.8.5 ไม่ทำงานใน Delphi 2009 และไม่มีข้อบ่งชี้ในฟอรัมหากใครก็ตามกำลังตรวจสอบเรื่องนี้   -  person lkessler    schedule 14.05.2009
comment
ไม่ทำงานใน Delphi 2009 แล้วตอนนี้ฉันใช้อะไรอยู่? รหัสจากดวงดาว? 4.8.5 ใช้งานได้ แม้แต่ 4.7.0 ก็ใช้งานได้กับ Delphi 2009   -  person Andreas Hausladen    schedule 14.05.2009
comment
การอัปเดตในวันที่ 2009-03-16 หมายความว่าเว็บไซต์นั้นใช้งานไม่ได้หรือไม่ soft-gems.net/   -  person Andreas Hausladen    schedule 14.05.2009
comment
ฉันอยากจะบอกว่าไม่มีศูนย์กลางสำหรับการอภิปรายเรื่องการพัฒนา (เช่น Mantis) การอภิปรายเกิดขึ้นเช่น ที่เดลฟี-แพรซิส ขอโทษสำหรับความสับสน.   -  person Uli Gerhardt    schedule 14.05.2009


คำตอบ (1)


ฉันได้แก้ไขปัญหานี้โดยใช้ TEmbeddedWB (ซึ่งดีกว่า TWebBrowser มาตรฐานมาก) จากนั้นฉันต้องเพิ่มเหตุการณ์ OnShowUI นี้:

function THtmlFrame.webBrowserShowUI(const dwID: Cardinal;
  const pActiveObject: IOleInPlaceActiveObject;
  const pCommandTarget: IOleCommandTarget; const pFrame: IOleInPlaceFrame;
  const pDoc: IOleInPlaceUIWindow): HRESULT;
begin
  try
    if WebBrowser.CanFocus then
      WebBrowser.SetFocus; // tell the VCL that the web-browser is focused
  except
    on E: EInvalidOperation do
      ; // ignore "Cannot focus inactive or invisible control"
  end;
  Result := S_FALSE;
end;


แต่ถ้าคุณต้องใช้ TWebBrowser คุณต้องเขียนโค้ดเพิ่มเติม:

type
  IDocHostUIHandler = interface(IUnknown)
    ['{bd3f23c0-d43e-11cf-893b-00aa00bdce1a}']
    function ShowContextMenu(const dwID: DWORD; const ppt: PPOINT;
      const CommandTarget: IUnknown; const Context: IDispatch): HRESULT; stdcall;
    function GetHostInfo(var pInfo: TDOCHOSTUIINFO): HRESULT; stdcall;
    function ShowUI(const dwID: DWORD; const pActiveObject: IOleInPlaceActiveObject;
      const pCommandTarget: IOleCommandTarget; const pFrame: IOleInPlaceFrame;
      const pDoc: IOleInPlaceUIWindow): HRESULT; stdcall;
    function HideUI: HRESULT; stdcall;
    function UpdateUI: HRESULT; stdcall;
    function EnableModeless(const fEnable: BOOL): HRESULT; stdcall;
    function OnDocWindowActivate(const fActivate: BOOL): HRESULT; stdcall;
    function OnFrameWindowActivate(const fActivate: BOOL): HRESULT; stdcall;
    function ResizeBorder(const prcBorder: PRECT; const pUIWindow: IOleInPlaceUIWindow; const fRameWindow: BOOL): HRESULT; stdcall;
    function TranslateAccelerator(const lpMsg: PMSG; const pguidCmdGroup: PGUID; const nCmdID: DWORD): HRESULT; stdcall;
    function GetOptionKeyPath(out pchKey: POLESTR; const dw: DWORD): HRESULT; stdcall;
    function GetDropTarget(const pDropTarget: IDropTarget; out ppDropTarget: IDropTarget): HRESULT; stdcall;
    function GetExternal(out ppDispatch: IDispatch): HRESULT; stdcall;
    function TranslateUrl(const dwTranslate: DWORD; const pchURLIn: POLESTR; out ppchURLOut: POLESTR): HRESULT; stdcall;
    function FilterDataObject(const pDO: IDataObject; out ppDORet: IDataObject): HRESULT; stdcall;
  end; // IDocHostUIHandler

  ICustomDoc = interface(IUnknown)
    ['{3050f3f0-98b5-11cf-bb82-00aa00bdce0b}']
    function SetUIHandler(const pUIHandler: IDocHostUIHandler): HResult; stdcall;
  end;

  TDocHostUIHandler = class(TInterfacedObject, IDocHostUIHandler)
  private
    FWebBrowser: TWebBrowser;
  protected
    function EnableModeless(const fEnable: BOOL): HResult; stdcall;
    function FilterDataObject(const pDO: IDataObject; out ppDORet: IDataObject): HResult; stdcall;
    function GetDropTarget(const pDropTarget: IDropTarget; out ppDropTarget: IDropTarget): HResult; stdcall;
    function GetExternal(out ppDispatch: IDispatch): HResult; stdcall;
    function GetHostInfo(var pInfo: TDocHostUIInfo): HResult; stdcall;
    function GetOptionKeyPath(var pchKey: POLESTR; const dw: DWORD): HResult; stdcall;
    function HideUI: HResult; stdcall;
    function OnDocWindowActivate(const fActivate: BOOL): HResult; stdcall;
    function OnFrameWindowActivate(const fActivate: BOOL): HResult; stdcall;
    function ResizeBorder(const prcBorder: PRECT; const pUIWindow: IOleInPlaceUIWindow;
      const fFrameWindow: BOOL): HResult; stdcall;
    function ShowContextMenu(const dwID: DWORD; const ppt: PPOINT;
      const pcmdtReserved: IInterface; const pdispReserved: IDispatch): HResult; stdcall;
    function ShowUI(const dwID: DWORD; const pActiveObject: IOleInPlaceActiveObject;
      const pCommandTarget: IOleCommandTarget; const pFrame: IOleInPlaceFrame;
      const pDoc: IOleInPlaceUIWindow): HResult; stdcall;
    function TranslateAccelerator(const lpMsg: PMSG; const pguidCmdGroup: PGUID; const nCmdID: DWORD): HResult; stdcall;
    function TranslateUrl(const dwTranslate: DWORD; const pchURLIn: POLESTR; var ppchURLOut: POLESTR): HResult; stdcall;
    function UpdateUI: HResult; stdcall;
  public
    constructor Create(AWebBrowser: TWebBrowser);
    property WebBrowser: TWebBrowser read FWebBrowser;
  end;


{ TDocHostUIHandler }

function TDocHostUIHandler.EnableModeless(const fEnable: BOOL): HResult;
begin
  Result := S_OK;
end;

function TDocHostUIHandler.FilterDataObject(const pDO: IDataObject; out ppDORet: IDataObject): HResult;
begin
  ppDORet := nil;
  Result := S_FALSE;
end;

function TDocHostUIHandler.GetDropTarget(const pDropTarget: IDropTarget; out ppDropTarget: IDropTarget): HResult;
begin
  ppDropTarget := nil;
  Result := E_FAIL;
end;

function TDocHostUIHandler.GetExternal(out ppDispatch: IDispatch): HResult;
begin
  ppDispatch := nil;
  Result := E_FAIL;
end;

function TDocHostUIHandler.GetHostInfo(var pInfo: TDocHostUIInfo): HResult;
begin
  Result := S_OK;
end;

function TDocHostUIHandler.GetOptionKeyPath(var pchKey: POLESTR; const dw: DWORD): HResult;
begin
  Result := E_FAIL;
end;

function TDocHostUIHandler.HideUI: HResult;
begin
  Result := S_OK;
end;

function TDocHostUIHandler.OnDocWindowActivate(const fActivate: BOOL): HResult;
begin
  Result := S_OK;
end;

function TDocHostUIHandler.OnFrameWindowActivate(const fActivate: BOOL): HResult;
begin
  Result := S_OK;
end;

function TDocHostUIHandler.ResizeBorder(const prcBorder: PRECT; const pUIWindow: IOleInPlaceUIWindow; const fFrameWindow: BOOL): HResult;
begin
  Result := S_FALSE;
end;

function TDocHostUIHandler.ShowContextMenu(const dwID: DWORD; const ppt: PPOINT; const pcmdtReserved: IInterface; const pdispReserved: IDispatch): HResult;
begin
  Result := S_FALSE
end;

function TDocHostUIHandler.TranslateAccelerator(const lpMsg: PMSG; const pguidCmdGroup: PGUID; const nCmdID: DWORD): HResult;
begin
  Result := S_FALSE;
end;

function TDocHostUIHandler.TranslateUrl(const dwTranslate: DWORD; const pchURLIn: POLESTR; var ppchURLOut: POLESTR): HResult;
begin
  Result := E_FAIL;
end;

function TDocHostUIHandler.UpdateUI: HResult;
begin
  Result := S_OK;
end;

function TDocHostUIHandler.ShowUI(const dwID: DWORD; const pActiveObject: IOleInPlaceActiveObject; const pCommandTarget: IOleCommandTarget;
  const pFrame: IOleInPlaceFrame; const pDoc: IOleInPlaceUIWindow): HResult;
begin
  try
    if WebBrowser.CanFocus then
      WebBrowser.SetFocus; // tell the VCL that the web-browser is focused
  except
    on E: EInvalidOperation do
      ; // ignore "Cannot focus inactive or invisible control"
  end;
  Result := S_OK;
end;



// install the DocHostUIHandler into the WebBrowser
var
  CustomDoc: ICustomDoc;
begin
  if WebBrowser1.Document.QueryInterface(ICustomDoc, CustomDoc) = S_OK then
    CustomDoc.SetUIHandler(TDocHostUIHandler.Create(WebBrowser1));
end;
person Andreas Hausladen    schedule 14.05.2009
comment
ฉันใช้ TEmbeddedWB แต่ปัญหาก็เกิดขึ้นกับ TWebBrowser ด้วย - และฉันไม่ต้องการให้ผู้คนออกไปนอกเรื่องแทนเจนต์ที่กล่าวโทษ TEmbeddedWB ฉันลองเพิ่ม CanFocus/SetFocus ให้กับเหตุการณ์ OnShowUI - ไม่ทำงาน สาเหตุหลักมาจากเหตุการณ์ OnShowUI ไม่ได้ถูกไล่ออกจริง ๆ จนกว่าฉันจะคลิกที่การควบคุมเว็บเบราว์เซอร์ - person Ian Boyd; 14.05.2009