>>644 hWndを比較しなくても、オブジェクトを比較すればいい。 On Error Resume Next Dim SW As New SHDocVw.ShellWindows Dim IE As SHDocVw.InternetExplorer For Each IE In SW If IE.Document.parentWindow.Document Is UserControl.Parent.Script.Document Then MsgBox IE.LocationURL Exit For End If Next
ちなみに今は、とりあえずこんな感じにしてある。 procedure THoge.BrowserNewWindow2(Sender: TObject; var ppDisp: IDispatch; var Cancel: WordBool); var Doc2: IHTMLDocument2; begin Doc2 := FBrowser.Document as IHTMLDocument2; //FBrowser: TWebBrowser; // Scriptで勝手に開くウインドウの場合は、大抵がactiveElement = nilになるっぽい。 if Doc2.activeElement = nil then begin // 以下略
MFCでWebブラウザコントロールを用いてそれを印刷したい場合、 // Verify the Web Browser control is valid. LPDISPATCH lpDispApp = m_web_view1.GetApplication(); if(lpDispApp) { // Get the HTMLDocument interface. LPDISPATCH lpDispDoc = m_web_view1.GetDocument(); if (lpDispDoc != NULL) { // Get the IOleCommandTarget interface so that we can dispatch the command. LPOLECOMMANDTARGET lpTarget = NULL; if (SUCCEEDED(lpDispDoc->QueryInterface(IID_IOleCommandTarget,(LPVOID*) &lpTarget))) { // Execute the print preview command. The control will handle the print preview GUI. // OLECMDID_PRINTPREVIEW is defined in "docobj.h". lpTarget->Exec(NULL, OLECMDID_PRINTPREVIEW, 0, NULL, NULL); lpTarget->Release(); } lpDispDoc->Release(); } lpDispApp->Release(); } みたいなコードで出来ますが、URLが印刷されちゃいますよね。 それを隠す方法があれば、ご教授ください
標準モジュールに書いてね。 Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private ShellEmbeddingHWND As Long Private ShellDocObjectHWND As Long Private InternetExplorerServerHWND As Long
Public Function GetWebBrowserHWND(ByVal OwnerHWND As Long) As Long InternetExplorerServerHWND = 0 ShellEmbeddingHWND = 0 ShellDocObjectHWND = 0 Call EnumChildWindows(OwnerHWND, AddressOf EnumChildProc, 0) GetWebBrowserHWND = InternetExplorerServerHWND End Function
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim Length As Long Dim Name As String Dim NullPos As Long
Name = String(255, vbNullChar) Length = Len(Name) If GetClassName(hwnd, Name, Length) <> 0 Then NullPos = InStr(Name, vbNullChar) If NullPos > 0 Then Name = Left$(Name, NullPos - 1) If Name = "Shell Embedding" Then ShellEmbeddingHWND = hwnd If Name = "Shell DocObject" Then ShellDocObjectHWND = hwnd If Name = "Internet Explorer_Server" Then InternetExplorerServerHWND = hwnd End If EnumChildProc = True End Function
修正 こっちのほうがいいか。GetWebBrowserHWND(WebBrowser1)として呼び出せるし。 Public Function GetWebBrowserHWND(ByVal wb As WebBrowser) As Long Dim OwnerHWND As Long OwnerHWND = wb.Parent.hWnd InternetExplorerServerHWND = 0 ShellEmbeddingHWND = 0 ShellDocObjectHWND = 0 Call EnumChildWindows(OwnerHWND, AddressOf EnumChildProc, 0) GetWebBrowserHWND = InternetExplorerServerHWND End Function