Autor Beitrag
Sagador76
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Fr 16.09.11 12:00 
Hi ihr,

ich hab ein Problem.
Ich schreibe gerade ein Programm um von VB nach VC# doch hänge ich an einem Problem.
Wenn ich in meinem eigenen "Browser" per Rechtsklick auf einem Link klicke und sage "In neuem Fenster öffnen"
kann ich zwar die Seite abbrechen also Blocken aber ich kann sie nicht in meinem eigenen Browser in einem neuen Tab öffnen da ich nicht an die URL für dieses neue Fenster dran komme.
Weiss einer wie und wo ich diese auslesen könnte? bzw. was ich in meinem Code hinzufügen müsste damit ich diese URL auslesen kann ?

Ich suche nun seit 2 Wochen danach und Teste alles mögliche aus doch bisher bekomme ich entweder nur Fehlermeldungen oder kann auch weiterhin nichts auslesen.

Hier der bisher funktionierende Code.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {



        public Form1()
        {
            InitializeComponent();
        }

        ExtendedWebBrowser web = new ExtendedWebBrowser();
        int i = 0;

        
        public class NewWindow2EventArgs : CancelEventArgs
        {

            object ppDisp;
            string url;

            public object PPDisp
            {
                get { return ppDisp; }
                set { ppDisp = value; }
            }



            public NewWindow2EventArgs(ref object ppDisp, ref bool cancel)
                : base()
            {
                this.ppDisp = ppDisp;
                this.Cancel = cancel;
            }

        }

        public class DocumentCompleteEventArgs : EventArgs
        {
            private object ppDisp;
            private object url;

            public object PPDisp
            {
                get { return ppDisp; }
                set { ppDisp = value; }
            }

            public object Url
            {
                get { return url; }
                set { url = value; }
            }

            public DocumentCompleteEventArgs(object ppDisp, object url)
            {
                this.ppDisp = ppDisp;
                this.url = url;

            }
        }

        public class CommandStateChangeEventArgs : EventArgs
        {
            private long command;
            private bool enable;
            public CommandStateChangeEventArgs(long command, ref bool enable)
            {
                this.command = command;
                this.enable = enable;
            }

            public long Command
            {
                get { return command; }
                set { command = value; }
            }

            public bool Enable
            {
                get { return enable; }
                set { enable = value; }
            }
        }


        
        public class ExtendedWebBrowser : WebBrowser
        {
            AxHost.ConnectionPointCookie cookie;
            WebBrowserExtendedEvents events;


            
            protected override void CreateSink()
            {
                
                base.CreateSink();
                events = new WebBrowserExtendedEvents(this);
                cookie = new AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));

            }

            public object Application
            {
                get
                {
                    IWebBrowser2 axWebBrowser = this.ActiveXInstance as IWebBrowser2;
                    if (axWebBrowser != null)
                    {
                        return axWebBrowser.Application;
                    }
                    else
                        return null;
                }
            }

            protected override void DetachSink()
            {
                if (null != cookie)
                {
                    cookie.Disconnect();
                    cookie = null;
                }
                base.DetachSink();
            }

            
            public event EventHandler<NewWindow2EventArgs> NewWindow2;

            protected void OnNewWindow2(ref object ppDisp, ref bool cancel)
            {
                EventHandler<NewWindow2EventArgs> h = NewWindow2;
                NewWindow2EventArgs args = new NewWindow2EventArgs(ref ppDisp, ref cancel);
                if (null != h)
                {
                    h(this, args);
                }
                
                cancel = args.Cancel;
                ppDisp = args.PPDisp;
            }


            
            public event EventHandler<DocumentCompleteEventArgs> DocumentComplete;

            protected void OnDocumentComplete(object ppDisp, object url)
            {
                EventHandler<DocumentCompleteEventArgs> h = DocumentComplete;
                DocumentCompleteEventArgs args = new DocumentCompleteEventArgs(ppDisp, url);
                if (null != h)
                {
                    h(this, args);
                }
               
                ppDisp = args.PPDisp;
                
            }

           
            public event EventHandler<CommandStateChangeEventArgs> CommandStateChange;

            protected void OnCommandStateChange(long command, ref bool enable)
            {
                EventHandler<CommandStateChangeEventArgs> h = CommandStateChange;
                CommandStateChangeEventArgs args = new CommandStateChangeEventArgs(command, ref enable);
                if (null != h)
                {
                    h(this, args);
                }
            }


            
            public class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
            {
                ExtendedWebBrowser _Browser;
                public WebBrowserExtendedEvents(ExtendedWebBrowser browser)
                { _Browser = browser; }

               
                public void NewWindow2(ref object pDisp, ref bool cancel)
                {
                    _Browser.OnNewWindow2(ref pDisp, ref cancel);
                }

                
                public void DocumentComplete(object pDisp, ref object url)
                {
                    _Browser.OnDocumentComplete(pDisp, url);
                }

               
                public void CommandStateChange(long command, bool enable)
                {
                    _Browser.OnCommandStateChange(command, ref enable);
                }


            }
            [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), TypeLibType(TypeLibTypeFlags.FHidden)]
            public interface DWebBrowserEvents2
            {
                [DispId(0x69)]
                void CommandStateChange([In] long command, [In] bool enable);
                [DispId(0x103)]
                void DocumentComplete([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL);
                [DispId(0xfb)]
                void NewWindow2([In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object pDisp, [In, Out] ref bool cancel);
            }

            [ComImport, Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E"), TypeLibType(TypeLibTypeFlags.FOleAutomation | TypeLibTypeFlags.FDual | TypeLibTypeFlags.FHidden)]
            public interface IWebBrowser2
            {
                [DispId(100)]
                void GoBack();
                [DispId(0x65)]
                void GoForward();
                [DispId(0x66)]
                void GoHome();
                [DispId(0x67)]
                void GoSearch();
                [DispId(0x68)]
                void Navigate([In] string Url, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers);
                [DispId(-550)]
                void Refresh();
                [DispId(0x69)]
                void Refresh2([In] ref object level);
                [DispId(0x6a)]
                void Stop();
                [DispId(200)]
                object Application { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
                [DispId(0xc9)]
                object Parent { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
                [DispId(0xca)]
                object Container { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
                [DispId(0xcb)]
                object Document { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
                [DispId(0xcc)]
                bool TopLevelContainer { get; }
                [DispId(0xcd)]
                string Type { get; }
                [DispId(0xce)]
                int Left { get; set; }
                [DispId(0xcf)]
                int Top { get; set; }
                [DispId(0xd0)]
                int Width { get; set; }
                [DispId(0xd1)]
                int Height { get; set; }
                [DispId(210)]
                string LocationName { get; }
                [DispId(0xd3)]
                string LocationURL { get; }
                [DispId(0xd4)]
                bool Busy { get; }
                [DispId(300)]
                void Quit();
                [DispId(0x12d)]
                void ClientToWindow(out int pcx, out int pcy);
                [DispId(0x12e)]
                void PutProperty([In] string property, [In] object vtValue);
                [DispId(0x12f)]
                object GetProperty([In] string property);
                [DispId(0)]
                string Name { get; }
                [DispId(-515)]
                int HWND { get; }
                [DispId(400)]
                string FullName { get; }
                [DispId(0x191)]
                string Path { get; }
                [DispId(0x192)]
                bool Visible { get; set; }
                [DispId(0x193)]
                bool StatusBar { get; set; }
                [DispId(0x194)]
                string StatusText { get; set; }
                [DispId(0x195)]
                int ToolBar { get; set; }
                [DispId(0x196)]
                bool MenuBar { get; set; }
                [DispId(0x197)]
                bool FullScreen { get; set; }
                [DispId(500)]
                void Navigate2([In] ref object URL, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers);
                [DispId(0x1f7)]
                void ShowBrowserBar([In] ref object pvaClsid, [In] ref object pvarShow, [In] ref object pvarSize);
                [DispId(-525)]
                WebBrowserReadyState ReadyState { get; }
                [DispId(550)]
                bool Offline { get; set; }
                [DispId(0x227)]
                bool Silent { get; set; }
                [DispId(0x228)]
                bool RegisterAsBrowser { get; set; }
                [DispId(0x229)]
                bool RegisterAsDropTarget { get; set; }
                [DispId(0x22a)]
                bool TheaterMode { get; set; }
                [DispId(0x22b)]
                bool AddressBar { get; set; }
                [DispId(0x22c)]
                bool Resizable { get; set; }
            }
        }

       





        private void Form1_Load(object sender, EventArgs e)
        {
            ExtendedWebBrowser web = new ExtendedWebBrowser();
            web.ScriptErrorsSuppressed = true;
            web.Dock = DockStyle.Fill;
            web.Visible = true;
            web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_Documentcompleted);
            web.NewWindow += new CancelEventHandler(web_NewWindow);

            WebControl1.TabPages.Add("OTRS");
            WebControl1.SelectTab(i);
            WebControl1.SelectedTab.Controls.Add(web);
            i += 1;

            //web.NewWindow2 += new EventHandler<NewWindow2EventArgs>(Neues_Fenster);

            ((WebBrowser)WebControl1.SelectedTab.Controls[0]).Navigate("http://google.de");
        }

        public void web_Documentcompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebControl1.SelectedTab.Text = "OTRS";
        }

        public void web_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }


    }
}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 16.09.11 12:28 
Hallo und :welcome:,

kannst du evtl. das WebBrowser.Navigating-Ereignis behandeln? Oder wird erst das neue Fenster (im StandardBrowser) geöffnet und anschließend dieses Ereignis aufgerufen?