2015-11-01 121 views
0

我試圖創建一個圖像元素到Windows窗體web瀏覽器控件現有網站。然後我想將創建的元素移動到某個位置。我有這個代碼,但它不起作用。將圖像元素添加到Web瀏覽器控件頁面(現有網站)

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     webBrowser1.Document.GetElementById("gpu_notice").Style = "display:none"; 
     webBrowser1.Document.GetElementById("header").OuterHtml = ""; 
     webBrowser1.Document.GetElementById("ads").OuterHtml = ""; 
     webBrowser1.Document.GetElementById("the_game").Style += "position: absolute; top: 0px; right: 0px;"; 
     HtmlElement logo = webBrowser1.Document.CreateElement("logo"); 
     logo.SetAttribute("SRC", @"C:\Users\Susana\Documents\Projeto HaxballK\Design\Logo HK.png"); 
     webBrowser1.Document.Body.AppendChild(logo); 
     logo.SetAttribute("float", "right"); 
    } 

回答

1

該任務是否將元素插入使用WebBrowser類顯示的現有HTML文檔?如果是,則步驟可能是(沒有測試,不好意思)

  1. 下載使用WebClient作爲一個HTML字符串文件
  2. 使<body>相對的HTML字符串(插入style='position: relative'<body>元素)
  3. 插入元素到HTML字符串,只是<body>後,使元素絕對(設定style='position: absolute; left=...; right=...'
  4. 的修飾HTML串入WebBrowser
  5. 0123的 DocumentText財產
+0

是的,這是我正在尋找的exacly。我會嘗試一下,當你有時間的時候你可以用這些指令編寫代碼嗎? – Khoury39

1

您必須使用「img」標籤而不是「logo」並使用file protocol來設置文件路徑。

HtmlElement logo = webBrowser1.Document.CreateElement("img"); 
logo.SetAttribute("SRC", @"file:///C:/Users/Susana/Documents/Projeto HaxballK/Design/Logo HK.png"); 
webBrowser1.Document.Body.AppendChild(logo); 
logo.SetAttribute("float", "right"); 

修訂

如果您要添加的圖片所有的JavaScript初始化已經運行後,只需使用一次性Timer

System.Windows.Forms.Timer timer = new Timer(); 
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    // other processes 

    timer.Interval = 1 * 1000 * 5; // 5 seconds 
    timer.Tick += timer_Tick; 
    timer.Start(); 
} 

void timer_Tick(object sender, EventArgs e) 
{ 
    timer.Tick -= timer_Tick; 

    HtmlElement logo = webBrowser1.Document.CreateElement("img"); 
    logo.SetAttribute("SRC", @"file:///C:/Users/Susana/Documents/Projeto HaxballK/Design/Logo HK.png"); 
    webBrowser1.Document.Body.AppendChild(logo); 
    logo.SetAttribute("float", "right"); 
} 
+0

感謝您的回答,我試過了,當頁面加載時出現徽標,但當頁面完全加載後,圖像消失。 – Khoury39

+0

我不能在這個上使用DocumentCompleted事件嗎? – Khoury39

+0

@ Khoury39'DocumentCompleted'應該沒問題,這段代碼只是增加了''。我猜JavaScript可能會刪除你的標誌,所以試試我更新的答案。 – jhmt