diff --git a/qtc-net-client-2/Controls/ChatMessageControl.cs b/qtc-net-client-2/Controls/ChatMessageControl.cs index 08f0a09..6f6b1a1 100644 --- a/qtc-net-client-2/Controls/ChatMessageControl.cs +++ b/qtc-net-client-2/Controls/ChatMessageControl.cs @@ -83,4 +83,66 @@ namespace qtc_net_client_2.Controls } } } + + public class ChatMessageControlMinimal : Control + { + public string Username = "Username"; + public string Message = "Message"; + + private Font usernameFont = new("Segoe UI", 9, FontStyle.Bold); + private Font messageFont = new("Segoe UI", 9, FontStyle.Regular); + + public ChatMessageControlMinimal() + { + DoubleBuffered = true; + MinimumSize = new Size(150, 20); + } + + protected override void OnPaint(PaintEventArgs e) + { + base.OnPaint(e); + + int padding = 6; + + // first draw the username + int textWidth = Width - padding * 2; + + Rectangle usernameRect = new(padding, padding, textWidth, 20); + e.Graphics.DrawString(Username, usernameFont, Brushes.Black, usernameRect); + + // finally draw the message + int messageTop = usernameRect.Bottom + 2; + + Rectangle messageRect = new(padding, messageTop, textWidth, Height - messageTop - padding); + StringFormat fmt = new() + { + Trimming = StringTrimming.Word, + FormatFlags = 0, + Alignment = StringAlignment.Near + }; + + e.Graphics.DrawString(Message, messageFont, Brushes.Black, messageRect, fmt); + } + + protected override void OnResize(EventArgs e) + { + base.OnResize(e); + Height = CalculateHeight(Width); + } + + public int CalculateHeight(int width) + { + int padding = 6; + int textWidth = width - padding * 2; + + using (Graphics g = CreateGraphics()) + { + SizeF usernameSize = g.MeasureString(Username, usernameFont, textWidth); + SizeF messageSize = g.MeasureString(Message, messageFont, textWidth); + + int totalHeight = padding + (int)usernameSize.Height + 2 + (int)messageSize.Height + padding; + return totalHeight; + } + } + } } diff --git a/qtc-net-client-2/Forms/DirectMessage.Designer.cs b/qtc-net-client-2/Forms/DirectMessage.Designer.cs index 32440ae..5a79b37 100644 --- a/qtc-net-client-2/Forms/DirectMessage.Designer.cs +++ b/qtc-net-client-2/Forms/DirectMessage.Designer.cs @@ -33,7 +33,7 @@ rtxtChatbox = new RichTextBox(); lblUsername = new Label(); pbPfp = new PictureBox(); - lvChat = new ListView(); + fpnlMessages = new FlowLayoutPanel(); ((System.ComponentModel.ISupportInitialize)pbPfp).BeginInit(); SuspendLayout(); // @@ -82,17 +82,17 @@ pbPfp.TabIndex = 9; pbPfp.TabStop = false; // - // lvChat + // fpnlMessages // - lvChat.Alignment = ListViewAlignment.Left; - lvChat.Font = new Font("Segoe UI", 9F, FontStyle.Bold, GraphicsUnit.Point, 0); - lvChat.Location = new Point(12, 48); - lvChat.MultiSelect = false; - lvChat.Name = "lvChat"; - lvChat.Size = new Size(593, 325); - lvChat.TabIndex = 10; - lvChat.UseCompatibleStateImageBehavior = false; - lvChat.View = View.SmallIcon; + fpnlMessages.AutoScroll = true; + fpnlMessages.BackColor = Color.White; + fpnlMessages.BorderStyle = BorderStyle.Fixed3D; + fpnlMessages.FlowDirection = FlowDirection.TopDown; + fpnlMessages.Location = new Point(14, 49); + fpnlMessages.Name = "fpnlMessages"; + fpnlMessages.Size = new Size(591, 324); + fpnlMessages.TabIndex = 11; + fpnlMessages.WrapContents = false; // // DirectMessage // @@ -100,7 +100,7 @@ AutoScaleMode = AutoScaleMode.Font; BackColor = Color.DodgerBlue; ClientSize = new Size(618, 443); - Controls.Add(lvChat); + Controls.Add(fpnlMessages); Controls.Add(pbPfp); Controls.Add(lblUsername); Controls.Add(btnSend); @@ -125,6 +125,6 @@ private RichTextBox rtxtChatbox; private Label lblUsername; private PictureBox pbPfp; - private ListView lvChat; + private FlowLayoutPanel fpnlMessages; } } \ No newline at end of file diff --git a/qtc-net-client-2/Forms/DirectMessage.cs b/qtc-net-client-2/Forms/DirectMessage.cs index 40d1f1c..834c242 100644 --- a/qtc-net-client-2/Forms/DirectMessage.cs +++ b/qtc-net-client-2/Forms/DirectMessage.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc.ModelBinding; +using qtc_net_client_2.Controls; using qtc_net_client_2.Services; using QtCNETAPI.Dtos.User; using QtCNETAPI.Events; @@ -100,14 +101,36 @@ namespace qtc_net_client_2.Forms Invoke(delegate () { var msg = e.NewItems.Cast().FirstOrDefault(); - lvChat.Items.Add(msg); + + var ctrl = new ChatMessageControlMinimal() + { + Username = msg!.Split(':')[0], + Message = msg!.Split(":")[1].Trim(), + }; + ctrl.Height = ctrl.CalculateHeight(ctrl.Width); + + fpnlMessages.Controls.Add(ctrl); + fpnlMessages.VerticalScroll.Value = fpnlMessages.VerticalScroll.Maximum; + fpnlMessages.PerformLayout(); + if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage"); }); } else { var msg = e.NewItems.Cast().FirstOrDefault(); - lvChat.Items.Add(msg); + + var ctrl = new ChatMessageControlMinimal + { + Username = msg!.Split(':')[0], + Message = msg!.Split(":")[1].Trim(), + }; + ctrl.Height = ctrl.CalculateHeight(ctrl.Width); + + fpnlMessages.Controls.Add(ctrl); + fpnlMessages.VerticalScroll.Value = fpnlMessages.VerticalScroll.Maximum; + fpnlMessages.PerformLayout(); + if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage"); } }