2008-10-10 7 views
6

Text 속성을 자주 업데이트해야하는 RichTextBox가 있는데, 이렇게하면 RichTextBox가 메서드 호출 전체에서 새로 고침되어 불필요하게 "깜박입니다".RichTextBox가 화면을 새로 고치는 것을 어떻게 방지합니까?

내 방법이 완료 될 때까지 화면 새로 고침을 일시적으로 억제하는 쉬운 방법을 찾고 싶었지만 웹에서 찾은 유일한 방법은 WndProc 메서드를 재정의하는 것입니다. 이 방법을 사용했지만 몇 가지 어려움과 부작용이 있으며 디버깅이 어려워집니다. 이렇게하는 더 좋은 방법이있는 것처럼 보입니다. 누군가가 나를 더 나은 해결책으로 안내 할 수 있습니까? 문자열에 조작을 수행

myRichTextBox.SuspendLayout(); 
DoStuff(); 
myRichTextBox.ResumeLayout(); 

답변

-3

이 밖으로 시도?

+0

당신은 또한 myRichTextBox.Enabled를 추가 할 수 있습니다 = 거짓; 나중에 Enabled = true; – Geoff

+4

SuspendLayout()이 실제로 여기서 도움이되지 않습니다. –

0

방금 ​​문자열로 텍스트를 저장할 수 있고, 방법의 끝에서, Text 속성에 다시 저장 :

3

여기 찾았 http://bytes.com/forum/thread276845.html

은 내가 업데이트를 완료 한 후 다음 무효화() 다음에 다시 활성화 을 사용하지 sendMessage 첨부를 통해 WM_SETREDRAW를 보내기까지했다. 그것은 작동하는 것처럼 보였다.

나는이 방법을 사용해 본적이 없습니다. 구문 강조 표시가있는 RTB가 포함 된 응용 프로그램을 작성하고 RTB 클래스에서 다음을 사용했습니다.

protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == paint) 
    { 
     if (!highlighting) 
     { 
      base.WndProc(ref m); // if we decided to paint this control, just call the RichTextBox WndProc 
     } 
     else 
     { 
      m.Result = IntPtr.Zero; // not painting, must set this to IntPtr.Zero if not painting otherwise serious problems. 
     } 
    } 
    else 
    { 
     base.WndProc(ref m); // message other than paint, just do what you normally do. 
    } 
} 

희망이 있습니다.

+0

WM_SETREDRAW가 나를 위해 일했습니다. LockWindowUpdate와 같아야합니다. –

-1

내가 LockWindowUpdate

 

[DllImport("user32.dll", EntryPoint="LockWindowUpdate", SetLastError=true, 
ExactSpelling=true, CharSet=CharSet.Auto, 
CallingConvention=CallingConvention.StdCall)] 
+3

-1. 이것은 적절한 사용이 아닙니다. http://blogs.msdn.com/b/oldnewthing/archive/2007/02/19/1716211.aspx를 참조하십시오. –

9

보고 제안 나는 원래의 질문, 그리고 WM_SETREDRAW로했다 나를 위해 sendMessage 첨부의 BoltBait의 사용을() 최선을 다했다 대답을 요구했다. WndProc 메서드를 사용하는 것보다 부작용이 적으며 내 응용 프로그램에서는 LockWindowUpdate의 두 배 빠른 성능을 보입니다.

확장 된 RichTextBox 클래스 내에서이 두 메서드를 추가했으며 일부 처리를 수행하는 동안 다시 그리기를 중지해야 할 때마다 호출합니다. RichTextBox 클래스 외부에서이 작업을 원한다면 "This"를 RichTextBox 인스턴스에 대한 참조로 바꾸면됩니다. 여기

private void StopRepaint() 
    { 
     // Stop redrawing: 
     SendMessage(this.Handle, WM_SETREDRAW, 0, IntPtr.Zero); 
     // Stop sending of events: 
     eventMask = SendMessage(this.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero); 
    } 

    private void StartRepaint() 
    { 
     // turn on events 
     SendMessage(this.Handle, EM_SETEVENTMASK, 0, eventMask); 
     // turn on redrawing 
     SendMessage(this.Handle, WM_SETREDRAW, 1, IntPtr.Zero); 
     // this forces a repaint, which for some reason is necessary in some cases. 
     this.Invalidate(); 
    } 
+0

코드를 완료 할 기회가 있으십니까? 이것은 현재 작성된 것처럼 거의 편집 할 수 없습니다. –

10

입니다 완전하고 작업 예 :

private const int WM_USER = 0x0400; 
    private const int EM_SETEVENTMASK = (WM_USER + 69); 
    private const int WM_SETREDRAW = 0x0b; 
    private IntPtr OldEventMask;  

    [DllImport("user32.dll", CharSet=CharSet.Auto)] 
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); 

    public void BeginUpdate() 
    { 
     SendMessage(this.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero); 
     OldEventMask = (IntPtr)SendMessage(this.Handle, EM_SETEVENTMASK, IntPtr.Zero, IntPtr.Zero); 
    }  

    public void EndUpdate() 
    { 
     SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); 
     SendMessage(this.Handle, EM_SETEVENTMASK, IntPtr.Zero, OldEventMask); 
    } 
관련 문제