2009-08-11 6 views
1

편집 모드에서 Windows.Forms WebBrowser 컨트롤을 사용하여 응용 프로그램 (.net 3.5 C# Windows 양식)에서 html 편집이 가능합니다. 문제는 편집 모드에서 HTML의 링크를 클릭 할 수 없다는 것입니다. 즉, 마우스를 가리키면 손으로 마우스 커서가 표시되지 않고 클릭하면 해당 링크로 이동하는 대신 해당 위치에 커서가 삽입됩니다.편집 모드에서 WebBrowser 컨트롤의 클릭 가능한 링크

이것은 의도적으로 설계된 것이며 링크 기능을 유지하면서 편집 모드를 활성화 할 수 있습니까? 우리는 사용자가 콘텐츠를 편집 할 수있는 동안 로컬 html 페이지 세트를 탐색하기를 원합니다.

나는이 같은 편집 모드를 설정하고있어 그 변경 아무것도 경우 : 나는 사용자가 링크 위로 가져갈 때를 확인하고 선택적으로 상태 표시 줄에 뭔가를 보여줄 것

webBrowser.Document.ExecCommand("EditMode", false, null); 
+0

Ctrl + 클릭하면 어떻게됩니까? – hannson

+0

절대적으로 아무것도 아님 :) – Gareth

답변

2

여기에 작동하는 것 같다 작은의 WinForm 응용 프로그램입니다. 편집 모드를 설정하면 모든 A 태그의 모든 링크 위치를 기록한 다음 마우스의 커서 위치를 확인합니다. 프레임이나 태그 중첩이있는 경우 OffsetRectangle이 올바른 값을 제공하는지 잘 모르겠지만 앱의 샘플 HTML은 작동합니다.

필요할 경우 다른 태그에서 onclick 등을 캡처하도록 수정할 수 있습니다.

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Text.RegularExpressions; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     private readonly Dictionary<Rectangle, Uri> _links = new Dictionary<Rectangle, Uri>(); 
     private readonly Regex _reLink = new Regex("href=['\"](?<link>.*?)['\"]"); 

     private const string _html = 
      "<html><body><br><div style='top:20px;left:50px;border:solid 1px red'><a href='http://www.cnn.com'>CNN</a></div><br><font size='14'>xx</font><a href='http://stackoverflow.com'>stackoverflow</a></body></html>"; 

     private bool _isEditMode; 

     public Form1() 
     { 
      InitializeComponent(); 
      webBrowser1.DocumentText = _html; 
      webBrowser1.Document.Click += Document_Click; 
      webBrowser1.Document.MouseMove += Document_MouseMove; 
     } 

     private void Document_MouseMove(object sender, HtmlElementEventArgs e) 
     { 
      if (!_isEditMode) return; 
      ChangeCursorIfOverLink(e); 
     } 

     private void ChangeCursorIfOverLink(HtmlElementEventArgs e) 
     { 
      foreach (KeyValuePair<Rectangle, Uri> link in _links) 
      { 
       if (CursorWithinControl(e, link.Key)) 
       { 
        if (Cursor.Current != Cursors.Hand) 
         Cursor.Current = Cursors.Hand; 
        return; 
       } 
      } 
      Cursor.Current = Cursors.Default; 
     } 

     private void Document_Click(object sender, HtmlElementEventArgs e) 
     { 
      NavigateLinkInEditMode(e); 
     } 

     private void NavigateLinkInEditMode(HtmlElementEventArgs e) 
     { 
      if (_isEditMode) 
      { 
       foreach (KeyValuePair<Rectangle, Uri> link in _links) 
       { 
        if (CursorWithinControl(e, link.Key)) 
        { 
         webBrowser1.Navigate(link.Value); 
         return; 
        } 
       } 
      } 
     } 

     private bool CursorWithinControl(HtmlElementEventArgs e, Rectangle rectangle) 
     { 
      return e.MousePosition.X >= rectangle.Left 
        && e.MousePosition.X <= rectangle.Left + rectangle.Width 
        && e.MousePosition.Y >= rectangle.Top 
        && e.MousePosition.Y <= rectangle.Top + rectangle.Height; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      RecordLinkPositions(); 
      webBrowser1.Document.ExecCommand("EditMode", false, null); 
      webBrowser1.DocumentText = _html; 
      _isEditMode = true; 
     } 

     private void RecordLinkPositions() 
     { 
      foreach (HtmlElement element in webBrowser1.Document.All) 
      { 
       if (element.TagName == "A") 
       { 
        string url = _reLink.Match(element.OuterHtml).Groups["link"].Value; 
        _links.Add(element.OffsetRectangle, new Uri(url)); 
       } 
      } 
     } 
    } 
} 
+0

굉장! 고마워,이 작품. 나는 또한 문서에서 GetElementFromPoint라는 메서드를 사용하여 요소 위치를 저장하지 않아도된다는 것을 발견했습니다. 여기에서 주요 아이디어는 Cursor.Current를 설정하고 웹 브라우저 컨트롤의 Cursor 속성이 아닌데, 이는 커서가 나를 위해 충돌하게 만들었습니다. – Gareth

+0

잠에서 나는 생각해야 할 문제에 관해 생각하게되었습니다. 사용자가 실제로 편집하면 OffsetRectangles가 이동합니다. 하나는 숨겨져있는 두 개의 웹 컨트롤을 사용하면이 문제를 해결할 수 있습니다. EditMode 키 입력에 키 입력이 입력 될 때마다 내용을 숨김으로 복사하고 Offset 목록을 다시 만듭니다. 이 샘플이 필요한 경우 알려주십시오. –

0

. 사용자가 CTRL 키를 누른 상태에서 링크를 클릭하여 열 수 있습니다.

편집 : 유용하게 사용할 수 있습니다 : http://www.codeproject.com/KB/mobile/browsermouseevents.aspx?msg=2732899

+0

Andrew, 네, 저는 이미 그걸 가지고 놀았으며 마우스 커서 아래에있는 요소를 찾아서 작동 시켰습니다. 문제는 사용자가 손 모양 커서를 원하고 웹 브라우저의 커서 속성 설정이 작동하지 않는다는 것입니다. 부모 컨트롤 (패널)의 커서를 설정하면 문제가 발생하여 전체 앱이 충돌합니다. 이상적으로, 나는 브라우저 컨트롤 내에서 지원되는 것을 원하지만 이것이 가장 좋은 방법이라면 무엇이 일어나야 할 것인가? – Gareth

관련 문제