Implement New Message Control In DM's

This commit is contained in:
Alan Moon 2025-11-18 16:13:00 -08:00
parent 95daf24473
commit 987fae22f0
3 changed files with 100 additions and 15 deletions

View File

@ -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;
}
}
}
} }

View File

@ -33,7 +33,7 @@
rtxtChatbox = new RichTextBox(); rtxtChatbox = new RichTextBox();
lblUsername = new Label(); lblUsername = new Label();
pbPfp = new PictureBox(); pbPfp = new PictureBox();
lvChat = new ListView(); fpnlMessages = new FlowLayoutPanel();
((System.ComponentModel.ISupportInitialize)pbPfp).BeginInit(); ((System.ComponentModel.ISupportInitialize)pbPfp).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -82,17 +82,17 @@
pbPfp.TabIndex = 9; pbPfp.TabIndex = 9;
pbPfp.TabStop = false; pbPfp.TabStop = false;
// //
// lvChat // fpnlMessages
// //
lvChat.Alignment = ListViewAlignment.Left; fpnlMessages.AutoScroll = true;
lvChat.Font = new Font("Segoe UI", 9F, FontStyle.Bold, GraphicsUnit.Point, 0); fpnlMessages.BackColor = Color.White;
lvChat.Location = new Point(12, 48); fpnlMessages.BorderStyle = BorderStyle.Fixed3D;
lvChat.MultiSelect = false; fpnlMessages.FlowDirection = FlowDirection.TopDown;
lvChat.Name = "lvChat"; fpnlMessages.Location = new Point(14, 49);
lvChat.Size = new Size(593, 325); fpnlMessages.Name = "fpnlMessages";
lvChat.TabIndex = 10; fpnlMessages.Size = new Size(591, 324);
lvChat.UseCompatibleStateImageBehavior = false; fpnlMessages.TabIndex = 11;
lvChat.View = View.SmallIcon; fpnlMessages.WrapContents = false;
// //
// DirectMessage // DirectMessage
// //
@ -100,7 +100,7 @@
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BackColor = Color.DodgerBlue; BackColor = Color.DodgerBlue;
ClientSize = new Size(618, 443); ClientSize = new Size(618, 443);
Controls.Add(lvChat); Controls.Add(fpnlMessages);
Controls.Add(pbPfp); Controls.Add(pbPfp);
Controls.Add(lblUsername); Controls.Add(lblUsername);
Controls.Add(btnSend); Controls.Add(btnSend);
@ -125,6 +125,6 @@
private RichTextBox rtxtChatbox; private RichTextBox rtxtChatbox;
private Label lblUsername; private Label lblUsername;
private PictureBox pbPfp; private PictureBox pbPfp;
private ListView lvChat; private FlowLayoutPanel fpnlMessages;
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding;
using qtc_net_client_2.Controls;
using qtc_net_client_2.Services; using qtc_net_client_2.Services;
using QtCNETAPI.Dtos.User; using QtCNETAPI.Dtos.User;
using QtCNETAPI.Events; using QtCNETAPI.Events;
@ -100,14 +101,36 @@ namespace qtc_net_client_2.Forms
Invoke(delegate () Invoke(delegate ()
{ {
var msg = e.NewItems.Cast<string>().FirstOrDefault(); var msg = e.NewItems.Cast<string>().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"); if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
}); });
} }
else else
{ {
var msg = e.NewItems.Cast<string>().FirstOrDefault(); var msg = e.NewItems.Cast<string>().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"); if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
} }
} }