다음을 통해 공유


방법: Windows Forms RichTextBox 컨트롤을 사용하여 웹 스타일 링크 표시

Windows Forms RichTextBox 컨트롤은 웹 링크를 색 밑줄로 표시할 수 있습니다. 링크를 클릭할 때 링크 텍스트에 지정된 웹 사이트를 보여 주는 브라우저 창을 여는 코드를 작성할 수 있습니다.

  1. 유효한 URL(예: https://www.microsoft.com/)을 포함하는 문자열로 Text 속성을 설정합니다.

  2. DetectUrls 속성은 true(기본값)로 설정해야 합니다.

  3. Process 개체의 새 전역 인스턴스를 만듭니다.

  4. 브라우저에 원하는 텍스트를 보내는 LinkClicked 이벤트에 대한 이벤트 처리기를 작성합니다.

    아래 예제에서 LinkClicked 이벤트는 RichTextBox 컨트롤의 Text 속성에 지정된 URL에 대한 Internet Explorer 인스턴스를 엽니다. 이 예에서는 양식에 RichTextBox 컨트롤이 있다고 가정합니다.

    중요

    Process.Start 메서드를 호출할 때 권한이 부족하여 부분 신뢰 컨텍스트에서 코드를 실행하는 경우 SecurityException 예외가 발생합니다. 자세한 내용은 Code Access Security Basics을 참조하세요.

    Public p As New System.Diagnostics.Process
    Private Sub RichTextBox1_LinkClicked _
       (ByVal sender As Object, ByVal e As _
       System.Windows.Forms.LinkClickedEventArgs) _
       Handles RichTextBox1.LinkClicked
          ' Call Process.Start method to open a browser
          ' with link text as URL.
          p = System.Diagnostics.Process.Start("IExplore.exe", e.LinkText)
    End Sub
    
    public System.Diagnostics.Process p = new System.Diagnostics.Process();
    
    private void richTextBox1_LinkClicked(object sender,
    System.Windows.Forms.LinkClickedEventArgs e)
    {
       // Call Process.Start method to open a browser
       // with link text as URL.
       p = System.Diagnostics.Process.Start("IExplore.exe", e.LinkText);
    }
    
    public:
       System::Diagnostics::Process ^ p;
    
    private:
       void richTextBox1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkClickedEventArgs ^  e)
       {
          // Call Process.Start method to open a browser
          // with link text as URL.
          p = System::Diagnostics::Process::Start("IExplore.exe",
             e->LinkText);
       }
    

    (Visual C++) 양식의 생성자에 다음 문을 포함하여 수행할 수 있는 프로세스 p를 초기화해야 합니다.

    p = gcnew System::Diagnostics::Process();
    

    (Visual C#, Visual C++) 양식 생성자에 다음 코드를 추가하여 이벤트 처리기를 등록합니다.

    this.richTextBox1.LinkClicked += new
       System.Windows.Forms.LinkClickedEventHandler
       (this.richTextBox1_LinkClicked);
    
    this->richTextBox1->LinkClicked += gcnew
       System::Windows::Forms::LinkClickedEventHandler
       (this, &Form1::richTextBox1_LinkClicked);
    

    사용을 완료한 후 만든 프로세스를 즉시 중지하는 것이 중요합니다. 위의 코드를 참조하여 프로세스를 중지하는 코드는 다음과 같습니다.

    Public Sub StopWebProcess()
       p.Kill()
    End Sub
    
    public void StopWebProcess()
    {
       p.Kill();
    }
    
    public: void StopWebProcess()
    {
       p->Kill();
    }
    

참고 항목