【问题标题】:How to do Chat Bubbles in VB.NET WinForms application如何在 VB.NET WinForms 应用程序中进行聊天气泡
【发布时间】:2014-07-06 05:34:35
【问题描述】:

首先,我想声明我对整个聊天泡泡概念还很陌生。

我已经进行了大量研究,但关于如何在 WinForms 桌面应用程序中完成这项工作的结果却一无所获。

这个概念相当简单(而且很常见,尤其是在移动设备中)

我想这样做(最好是在 RichTextBox 中)。请记住,这是一个 WinForms 应用程序,因此 XAML 组件将不起作用。这只是一个聊天布局问题。发送/接收/显示消息已经完成,我只需要知道如何将这些消息包装在一个气泡中(根据通信方向向左或向右),并以正常的聊天流(当前使用RichTextBox,并为名称着色)。

提前致谢。

【问题讨论】:

  • 使用您的规格创建一个新控件。然后将新控件添加到面板、FlowLayoutPanel、列表框等...richtextbox 将不起作用...

标签: vb.net winforms chat


【解决方案1】:

您可以使用 FlowlayoutPanel 和新的 Chatbubble 用户控件。

首先,您需要向您的项目添加一个新的用户控件。叫它ChatBubble。 如果您使用 Control 类或 UserControl 类,这基本上取决于您。我在示例中偶然使用了Control,但这并没有太大的区别。 类型在自动创建的.Desiger 中定义。 Visual Studio 的文件。

没有定义控件的一些属性

Public Class ChatBubble
    'The "Inherits Control/Usercontrol statement is found in the designer

    Public Enum LeftRight
        Left = 0
        Right = 1
    End Enum
    Public Property Orientation As LeftRight
    Public Property BackBrush As Brush

方向定义聊天气泡是左对齐还是右对齐,Backbrush 是气泡的背景颜色。

构造函数用于设置控件的更多参数

Public Sub New()
   InitializeComponent()
   SetStyle(ControlStyles.ResizeRedraw, True) 'Redraw the control on resize
   SetStyle(ControlStyles.OptimizedDoubleBuffer, True) 'Double buffer the drawing
   SetStyle(ControlStyles.AllPaintingInWmPaint, True) 'Used to draw the control yourself
   SetStyle(ControlStyles.UserPaint, True) 'Used to draw the control yourself
   BackBrush = Brushes.Blue 'Default values
   Orientation = LeftRight.Left 'Default values
   Me.Padding = New Padding(5) 'Default values
End Sub

默认文本属性用于聊天文本。

现在你实际上需要绘制控件

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)
    'Smooth the output
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    'Measure the size of your content
    Dim textSize As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font)

    'Ellipse defines the extents of the bubble
    Dim Ellipse As Rectangle
    'Setup the ellipse depending on the orientation
    'Include margins (5 inside, padding of the control outside)
    If Orientation = LeftRight.Left Then
        Ellipse = New Rectangle(Me.ClientRectangle.Left + Me.Padding.Left, Me.ClientRectangle.Top + Me.Padding.Top, _
                             textSize.Width + 10, Me.ClientRectangle.Height - Me.Padding.Bottom - Me.Padding.Top)
    Else
        Ellipse = New Rectangle(Me.ClientRectangle.Right - (textSize.Width + 10) - Me.Padding.Right, Me.ClientRectangle.Top + Me.Padding.Top, _
                             textSize.Width + 10, Me.ClientRectangle.Height - Me.Padding.Bottom - Me.Padding.Top)
    End If
    'Define the text's location, center top/bottom
    Dim TextLocation As Point = New Point(Ellipse.Left + 5, CInt(Me.ClientSize.Height / 2 - textSize.Height / 2))
    'Draw the bubble
    e.Graphics.FillEllipse(BackBrush, Ellipse)
    'Draw the string
    e.Graphics.DrawString(Me.Text, Me.Font, Brushes.White, TextLocation)
End Sub

这是一个非常粗略的例子。您可以根据您的文本自动调整控件大小,包括文本换行,但这基本上可以满足您的需求。 像这样将气泡添加到顶部/底部 Flowlayoutpanel:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       'flp is the flowlayoutpanel

        Static count As Integer = 1

        Dim b As New ChatBubble
        With b
             .Text = "Hello World " & count.ToString
             .Orientation = If(count Mod 2 = 0, ChatBubble.LeftRight.Left, ChatBubble.LeftRight.Right)
             .Size = New Size(flp.Width, 30)
         End With

         flp.Controls.Add(b)
         count += 1
    End Sub

    Private Sub flp_Resize(sender As Object, e As EventArgs) Handles flp.Resize
        For Each c As Control In flp.Controls
             c.Width = flp.Width
         Next
    End Sub
End Class

样本输出:

【讨论】:

  • 给我一些时间来回顾一下。使用 FlowPanel 将消除某些事情,例如与说话的用户或时间/等之前的气泡。我也不确定它是否支持自动滚动(添加新文本时,我的 RTF 控件将滚动到新文本的底部)
  • @SanuelJackson 在自定义控件中,您可以放置​​任何您想要的东西。将 Chatname 属性添加到 ChatBubble 类,然后将名称绘制在气泡旁边。添加上下文菜单以获取更多选项以及自定义事件。上面的类是一个非常基本的例子。对于滚动问题,您可以将 Flowlayoutpanel 添加到另一个面板,自动调整大小并手动滚动面板。参见此处,例如 stackoverflow.com/questions/10980771/…
  • 我很抱歉。我还没有时间研究这个。请再给我几天时间。
  • 这有效,但仅适用于最基本的文本。似乎没有考虑多行消息
  • @SanuelJackson 不,这并不是要以 1:1 的方式实现,而是要展示一个基本概念。您必须自己投入一些精力。例如,通过考虑textSize.Height 值来调整椭圆高度。
【解决方案2】:

尝试为消息弹出创建自定义表单并将自定义区域添加到该表单。所以,当消息到来时,气球消息应该是这样调用的。

BalloonMessage balloon As New BalloonMessage(Message = "My Message goes here.", IsRecipient = true, Locantion= new Point(x,y))
ballon.Show()

//Balloon Message Form
Public Class BalloonMessage 
Inherits Form
    Public Sub New(Message As String, IsRecipient As Boolean, Location As Point)
         Initialization()
         Me.Location = Location
         Me.rtf.Text = Message
         Me.Region = new Region(getRegion(IsRecipient))
    End Sub

    Private Function getRegion(IsRecipient As Boolean) As GraphicsPath
        Dim gp As New GraphicsPath
        //Place condition to create balloon point position either left or right
        return gp
    End Function

End Class

阅读更多关于Region 和MSDN 的自定义形状表单和控件的信息

或者您可以将 PNG 透明图像设置为 BalloonMessage 表单并将背景颜色设置为绿色或蓝色,然后将该表单的 BackgroundTransperancy 设置为与背景颜色相同。这是使您的窗口窗体自定义形状的最简单方法。

将 DoubleBuffer 属性设置为 true 以使您的 BalloonMessage 表单平滑。

【讨论】:

  • 感谢您的贡献,但这有助于将聊天气泡显示为表单,但不适用于上述方法。例如,查看 iOS 或 Android 或 Windows8 移动设备的聊天流程。不是单独的表单,它们是表单上的一个组件。
  • 在 windows 应用程序中,没有其他方法可以像 windows 窗体那样在容器外显示气球控件。要么您必须编写一些代码来弹出下拉列表和日期时间选择器等控件,要么您可以使用简单的 Windows 表单。 Microsoft 是否在 WPF 中提供了该功能..
  • 这不是“弹出窗口”或不在窗口窗体中。恰恰相反。如果您拥有智能手机(iPhone、Android 或 Windows Phone),那么您将了解对话流程中的聊天气泡是什么。如果你看上面的图片,它描绘的正是“他们说”,..“你说”..气泡流的类型。这不是工具提示。这不是通知。这不是从系统托盘中弹出的东西。这是一个聊天对话。
【解决方案3】:

您可以使用图像和文本框,只需将其组合起来看起来像气泡。

【讨论】:

    猜你喜欢
    • 2017-05-20
    • 2022-11-17
    • 2016-11-14
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多