- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我想声明我对整个聊天气泡概念相当陌生。
我已经进行了大量研究,但关于如何在 WinForms 桌面应用程序中完成此操作的结果无效。
这个概念相当简单(而且很常见,尤其是在移动设备中)
我想这样做(最好是在 RichTextBox 中)。请记住,这是一个 WinForms 应用程序,因此 XAML 组件将不起作用。这只是一个聊天布局问题。发送/接收/显示消息已经完成,我只需要知道如何将这些消息包装在气泡中(根据通信方向向左或向右),并以正常的聊天流程(当前使用RichTextBox,并为名称着色)。
提前致谢。
最佳答案
您可以使用 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
Orientation 定义聊天气泡是左对齐还是右对齐,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
这是一个非常粗略的例子。您可以根据文本自动调整控件大小,包括文本换行,但这基本上可以满足您的需求。像这样将气泡添加到顶部/底部流布局面板:
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
示例输出:
关于vb.net - 如何在 VB.NET WinForms 应用程序中创建聊天气泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24593009/
我有几个问题。我是 Visual Basic 这个领域的新手,所以不要取笑我。 1.) VB.NET之间有什么区别和 VB ? 2.) 我需要为 Windows 开发基本的应用程序。(如记事本)我应该
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我只是想知道 VB.NET 和 VB 2010 是否相同。 我只是想知道。 最佳答案 VB 2010 是 VB.Net 的最新版本。 Microsoft 在 VB 2005 版本中删除了 VB 的“.
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我正在尝试将 VB 函数移植到 VB.NET,但我无法使该函数正常工作并正确更新。 rFormat = Format(Format(Value, fmt), String$(Len(fmt), "@"
如何在VB中注释多行代码/代码块? 最佳答案 VB 在语言级别上没有这样的构造。它有使用撇号字符的单行注释: ' hello world ' this is a comment Rem this is
我正在使用我在 VB2005 中创建的表单在按下按钮时打开程序,然后在文本字段中显示进程 ID(再次按下按钮时)。当我运行它时,表单将打开程序 (Notepad.exe) 但当我单击按钮查看进程 ID
我正在尝试添加一个从 vb.net 创建的 dll,并且想将其导入到现有的 vb 6 项目中,但它给了我错误“无法添加对指定文件的引用”。 。有人知道如何解决这个问题吗? 最佳答案 需要遵循以下步骤:
我有一个数据 GridView 。右键单击它会显示一个上下文菜单,但它始终位于右上角。我想要它,以便菜单出现在用户右键单击的单元格上。它可能是单元格 1 或单元格 2 或其他。 谢谢福尔坎 最佳答案
我只是在 Visual Studio 2010 中使用 Visual Basic。有人知道我将如何制作“浏览文件夹(或文件)”按钮吗?我对 VB 真的很陌生,我只是在寻找一些简单的帮助:) 最佳答案
这次感到困惑... 最简单的代码行有时可能起作用,有时却没有。首先,我认为问题在于我试图读取DWORD的值,但是由于我可以从某些键读取DWORD值,所以这一定不是问题。现在的问题似乎是,如果 key
我的代码中有此方法: Private Sub Display() Received.AppendText(" - " & RXArray) End Sub 这两个调用之间有什么区别:
我正在创建一个宏程序来记录和回放鼠标和键盘输入。录制效果很好,鼠标播放也一样,但是我在播放键盘输入时遇到了麻烦——特别是在释放之前按住一个键几秒钟。这不等同于重复按键。这是我尝试过的: 技巧 1:Me
我最近刚刚了解了 VB.NET 中静态局部变量的使用,并想知道它在延迟加载属性中的潜在用途。 考虑以下示例代码。 Public Class Foo Implements IFoo End Clas
VB 有一个 C# 没有的特性,在项目级别导入命名空间(我的项目>引用>导入命名空间)。当新人在源代码控制之外检查项目时,我们的自定义导入不包括在内。这个 VB 特定的导入命名空间存储在哪里? 最佳答
我已将我的问题缩小到这个简单的案例,但似乎无法找到发生了什么: 我有两个表单,一个只有一个按钮,另一个是空的。 单击按钮时,form1 隐藏和显示 form2 出现时,form2隐藏,form1再次显
为什么下面的简单代码会失败?无论我使用 LinearGradientMode 的哪个值,这段代码总是用从左到右的渐变填充路径。 graphPath 是在别处创建的 GraphicPath 对象(基本上
我可以多快替换字符串中的字符? 所以这个问题的背景是这样的:我们有几个应用程序通过套接字相互通信并与客户端的应用程序通信。这些套接字消息包含不可打印的字符(例如 chr(0)),需要用预定的字符串(例
如何从任何文件中读取原始字节数组... Dim bytes() as Byte ..然后将该字节数组写回新文件? 我需要它作为一个字节数组来做一些处理。 我目前正在使用: 阅读 Dim fInfo
我是一名优秀的程序员,十分优秀!