开发者

Creating a chat window that displays specific text in different colours WPF 4 [closed]

开发者 https://www.devze.com 2023-02-13 03:03 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have written a multipurpose application that will be used over a network that extends across the country. One part of this application is an order wire system which allows one station to communicate with another or all connected using the software, which runs in fullscreen mode.

Using WPF I have a sexy interface with a lovely colour scheme that I wanted to retain when using WPF's RichEditBox control. The last time I used a rich edit control was back in the hey day of MFC; I was in for a rude shock trying to accomplish my task with WPF.

So basically what happens (Like in most chat apps) when a user sends a message,

"UserName: Message blah blah blah" will be displayed in the text box. All I wanted was to have UserName: one colour and the message another. Simple I thought.

I mucked around for a while to no avail, read some tutorials, ripped out some hair, threw my keyboard in disgust, stormed off etc... only to come back and have another crack. Either the articles I had read were too complicated for what I wanted, would slow the program down to a crawl or not do what I needed.... So, I have come up with this simplistic approach. Please feel free to amend my solution with something better. This does exactly what I wanted it to do.

Sorry if this step by step approach is to n00b for you, I try to be as descriptive as I can for the less experienced out there, as we all were once.

<RichTextBox x:Name="textbox_chat_wnd" Height="100" Background="#FF2A2D30" IsReadOnly="True">
    <FlowDocument Name="flowdoc_chat_wnd"/>
</RichTextBox>

So we've created the RichTextBox, named it and named the flow document too.

Okay, so I have received my message from another user and it is now stored 开发者_如何学Goin a string strMessage. Good. The message consists of the user name of the person who sent it and the message, separated by a semi colon, hence why I am calling string.split on it :)

//Split message by first semi-colon. Ignore all others
string[] strSplit = strMessage.Split(new string[]{":"}, 2, StringSplitOptions.None);

//Create new paragraph and set line spacing to 1. Prevents massive line gaps
Paragraph p_Paragraph = new Paragraph();
p_Paragraph.LineHeight = 1;

//Create Run object that contains the name of the user + semicolon
//then set the colour of the text.
Run r_User = new Run(strSplit[0] + ": ");
r_User.Foreground = Brushes.GreenYellow;

//Message run object, set colour.
Run r_Message = new Run(strSplit[1]);
r_User.Foreground = Brushes.Silver;

//Add the two run objects to our paragraph:
p_Paragraph.Inlines.Add(r_User);
p_Paragraph.Inlines.Add(r_Message);

//Add the paragraph to the flow document as a block
flowdoc_chat_wnd.Blocks.Add(p_Paragraph);

//Scroll to the end of the control
textbox_chat_wnd.ScrollToEnd();

I hope this helps someone and is clear enough to understand what is trying to be achieved and how.

Again, I anticipate seeing improved methods of accomplishing this and to hear your thoughts.

Thanks,

Ash


I wrote some time ago a similar chat application and I used the WebBrowser control, modifying the HTMLDocument object behind it and using HTML for changing colors, displaying emoticons. I remember I've tested at that time a little bit the WPF RichTextBox control and it became quite slow after adding about 1000 lines in it, in a simple Add(new Run()) Add(new Paragraph()) scenario, so I went with the WebBrowser control which does ok for chat applications (as long as you don't need editing capabilities).

0

精彩评论

暂无评论...
验证码 换一张
取 消