using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DbxNotifier
{
///
/// 큰 알림 창 — 웹 프로젝트 화면의 큰 팝업(.pj-alert-pop)과 같은 모양: 흰 카드·둥근 모서리 16·연분홍 머리(종 + "새 알림 N건" + 닫기)·
/// 줄마다 배지(업무 배정/나를 태그)·굵은 제목·회색 둘째 줄·"열기 ›", 보라↔주황으로 1초마다 깜박이는 테두리와 빛.
/// 다른 프로그램 위(맨 위)에 뜨지만 포커스는 빼앗지 않는다(입력 중이던 창 그대로). 둥근 모서리·그림자를 위해
/// 픽셀 단위 투명 창(UpdateLayeredWindow)으로 직접 그린다.
///
internal sealed class AlertForm : Form
{
public event Action OpenClicked;
public event Action CloseClicked;
public event Action MoreClicked;
private const int MaxRows = 6;
private const float CardW = 560, HeadH = 54, RowH = 62, Pad = 6, MoreH = 38, ShadowM = 40, BottomGap = 28, Radius = 16;
private static readonly Color Accent = Gfx.Hex("#7b68ee");
private static readonly Color AccentDark = Gfx.Hex("#6453d6");
private static readonly Color Orange = Gfx.Hex("#ff6b4a");
private static readonly Color Ink = Gfx.Hex("#050505");
private static readonly Color InkSoft = Gfx.Hex("#636363");
private const string FontName = "Malgun Gothic";
private readonly List _items = new List();
private readonly List _rows = new List();
private readonly Timer _blink = new Timer { Interval = 70 };
private readonly DateTime _t0 = DateTime.Now;
private int _total;
private float _s = 1f, _fontScale = -1f;
private RectangleF _card, _close, _more;
private int _hoverRow = -1;
private bool _hoverClose, _hoverMore;
private Font _fHead, _fTitle, _fSmall, _fBadge, _fGo, _fBtn;
public AlertForm()
{
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
TopMost = true;
Text = "DBX 알림";
_blink.Tick += (s, e) => Render();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
return cp;
}
}
protected override bool ShowWithoutActivation => true;
/// 알림 목록을 보여 준다(처음이면 띄우고, 이미 떠 있으면 내용만 바꾼다).
public void SetAlerts(IList all)
{
_items.Clear();
for (int i = 0; i < all.Count && i < MaxRows; i++) _items.Add(all[i]);
_total = all.Count;
_hoverRow = -1;
_hoverClose = _hoverMore = false;
if (!IsHandleCreated) CreateHandle();
LayoutCard();
Render();
if (!Visible) Show();
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
_blink.Start();
}
public void HidePopup()
{
_blink.Stop();
Hide();
}
private void EnsureFonts()
{
if (Math.Abs(_fontScale - _s) < 0.01f) return;
DisposeFonts();
_fontScale = _s;
_fHead = new Font(FontName, 15f * _s, FontStyle.Bold, GraphicsUnit.Pixel);
_fTitle = new Font(FontName, 14.5f * _s, FontStyle.Bold, GraphicsUnit.Pixel);
_fSmall = new Font(FontName, 12f * _s, FontStyle.Regular, GraphicsUnit.Pixel);
_fBadge = new Font(FontName, 11.5f * _s, FontStyle.Bold, GraphicsUnit.Pixel);
_fGo = new Font(FontName, 12.5f * _s, FontStyle.Bold, GraphicsUnit.Pixel);
_fBtn = new Font(FontName, 13f * _s, FontStyle.Regular, GraphicsUnit.Pixel);
}
private void DisposeFonts()
{
foreach (Font f in new[] { _fHead, _fTitle, _fSmall, _fBadge, _fGo, _fBtn }) f?.Dispose();
}
/// 카드 크기·위치 — 마우스가 있는 화면의 작업 영역 아래 가운데(웹 팝업처럼 아래에서 28px 위).
private void LayoutCard()
{
_s = Gfx.SystemScale();
EnsureFonts();
int extra = _total - _items.Count;
float cardH = HeadH + Pad * 2 + _items.Count * RowH + (extra > 0 ? MoreH : 0);
float m = ShadowM * _s;
_card = new RectangleF(m, m, CardW * _s, cardH * _s);
_close = new RectangleF(_card.Right - 72 * _s, _card.Y + (HeadH - 32) / 2 * _s, 58 * _s, 32 * _s);
_rows.Clear();
float y = _card.Y + (HeadH + Pad) * _s;
foreach (Alert _ in _items)
{
_rows.Add(new RectangleF(_card.X + Pad * _s, y, _card.Width - 2 * Pad * _s, RowH * _s));
y += RowH * _s;
}
_more = extra > 0 ? new RectangleF(_card.X + Pad * _s, y, _card.Width - 2 * Pad * _s, MoreH * _s) : RectangleF.Empty;
int bw = (int)Math.Ceiling(_card.Width + 2 * m), bh = (int)Math.Ceiling(_card.Height + 2 * m);
Rectangle wa = Screen.FromPoint(Cursor.Position).WorkingArea;
int x = wa.Left + (wa.Width - bw) / 2;
int top = (int)(wa.Bottom - BottomGap * _s - _card.Height - m);
Bounds = new Rectangle(x, Math.Max(wa.Top, top), bw, bh);
}
private static void DrawText(Graphics g, string t, Font f, Color c, RectangleF r,
StringAlignment h = StringAlignment.Near, StringAlignment v = StringAlignment.Center)
{
// GenericTypographic — 한글 글자 사이가 벌어지지 않게(기본 StringFormat 은 글자마다 여백을 둔다)
using (var sf = (StringFormat)StringFormat.GenericTypographic.Clone())
using (var b = new SolidBrush(c))
{
sf.FormatFlags |= StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
sf.Alignment = h;
sf.LineAlignment = v;
g.DrawString(t ?? "", f, b, r, sf);
}
}
private static void Fill(Graphics g, RectangleF r, float radius, Color c)
{
using (GraphicsPath p = Gfx.Round(r, radius))
using (var b = new SolidBrush(c))
g.FillPath(b, p);
}
private void Render()
{
if (!IsHandleCreated || Width <= 0 || Height <= 0) return;
double phase = (DateTime.Now - _t0).TotalSeconds % 1.0;
double k = (1 - Math.Cos(phase * 2 * Math.PI)) / 2; // 0 → 1 → 0 (보라 → 주황 → 보라)
float s = _s, rad = Radius * s;
RectangleF c = _card;
using (var bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb))
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.Transparent);
// 그림자(0 18px 50px rgba(16,24,40,.28) 비슷하게 — 겹겹이 옅게)
for (int i = 12; i >= 1; i--)
{
RectangleF r = c;
r.Offset(0, 12 * s);
r.Inflate(i * 2.2f * s, i * 2.2f * s);
Fill(g, r, rad + i * 2.2f * s, Color.FromArgb(6, 16, 24, 40));
}
// 깜박이는 주황 빛(0 0 0 10px rgba(255,107,74,.18))
if (k > 0.02)
{
RectangleF r = c;
float sp = (float)(k * 10 * s);
r.Inflate(sp, sp);
Fill(g, r, rad + sp, Color.FromArgb((int)(k * 0.18 * 255), Orange));
}
using (GraphicsPath cardPath = Gfx.Round(c, rad))
{
using (var b = new SolidBrush(Color.White)) g.FillPath(b, cardPath);
GraphicsState gs = g.Save();
g.SetClip(cardPath, CombineMode.Intersect);
var head = new RectangleF(c.X, c.Y, c.Width, HeadH * s);
using (var b = new SolidBrush(Gfx.Hex("#fff4f0"))) g.FillRectangle(b, head);
using (var p = new Pen(Gfx.Hex("#f6d6cc"), Math.Max(1f, s))) g.DrawLine(p, c.X, head.Bottom, c.Right, head.Bottom);
Gfx.Bell(g, new RectangleF(c.X + 16 * s, head.Y + (HeadH - 24) / 2 * s, 24 * s, 24 * s), Gfx.Hex("#e5532f"));
DrawText(g, "새 알림 " + _total + "건", _fHead, Ink, new RectangleF(c.X + 50 * s, head.Y, _close.X - c.X - 60 * s, head.Height));
Fill(g, _close, 8 * s, _hoverClose ? Gfx.Hex("#f2f4f7") : Color.White);
using (GraphicsPath bp = Gfx.Round(_close, 8 * s))
using (var p = new Pen(Gfx.Hex("#d0d5dd"), Math.Max(1f, s)))
g.DrawPath(p, bp);
DrawText(g, "닫기", _fBtn, Gfx.Hex("#344054"), _close, StringAlignment.Center);
for (int i = 0; i < _items.Count; i++) DrawRow(g, _items[i], _rows[i], i == _hoverRow);
if (!_more.IsEmpty)
{
if (_hoverMore) Fill(g, _more, 10 * s, Gfx.Hex("#f5f6f8"));
DrawText(g, "외 " + (_total - _items.Count) + "건 더 — 수신함에서 보기 ›", _fGo, AccentDark, _more, StringAlignment.Center);
}
g.Restore(gs);
RectangleF br = c;
br.Inflate(-s, -s);
using (GraphicsPath bp = Gfx.Round(br, rad - s))
using (var pen = new Pen(Gfx.Mix(Accent, Orange, k), 2 * s))
g.DrawPath(pen, bp);
}
Push(bmp);
}
}
private void DrawRow(Graphics g, Alert a, RectangleF r, bool hover)
{
float s = _s;
if (hover) Fill(g, r, 10 * s, Gfx.Hex("#f5f6f8"));
string kind = a.IsMention ? "나를 태그" : "업무 배정";
SizeF ks = g.MeasureString(kind, _fBadge, PointF.Empty, StringFormat.GenericTypographic);
var kr = new RectangleF(r.X + 10 * s, r.Y + (r.Height - 22 * s) / 2, ks.Width + 18 * s, 22 * s);
Fill(g, kr, 11 * s, a.IsMention ? Gfx.Hex("#eef2ff") : Gfx.Hex("#e7f6ec"));
DrawText(g, kind, _fBadge, a.IsMention ? Gfx.Hex("#4053c8") : Gfx.Hex("#1f8a4c"), kr, StringAlignment.Center);
const string go = "열기 ›";
SizeF gsz = g.MeasureString(go, _fGo, PointF.Empty, StringFormat.GenericTypographic);
float goX = r.Right - 12 * s - gsz.Width;
DrawText(g, go, _fGo, AccentDark, new RectangleF(goX - 2 * s, r.Y, gsz.Width + 8 * s, r.Height));
float tx = kr.Right + 12 * s, tw = Math.Max(10, goX - 14 * s - tx);
DrawText(g, a.CleanTitle, _fTitle, Ink, new RectangleF(tx, r.Y + 8 * s, tw, 24 * s));
DrawText(g, a.SubLine, _fSmall, InkSoft, new RectangleF(tx, r.Y + 33 * s, tw, 20 * s));
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
int row = -1;
for (int i = 0; i < _rows.Count; i++) if (_rows[i].Contains(e.Location)) row = i;
bool close = _close.Contains(e.Location);
bool more = !_more.IsEmpty && _more.Contains(e.Location);
if (row != _hoverRow || close != _hoverClose || more != _hoverMore)
{
_hoverRow = row;
_hoverClose = close;
_hoverMore = more;
Render();
}
Cursor = row >= 0 || close || more ? Cursors.Hand : Cursors.Default;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
_hoverRow = -1;
_hoverClose = _hoverMore = false;
Render();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button != MouseButtons.Left) return;
if (_close.Contains(e.Location)) { CloseClicked?.Invoke(); return; }
if (!_more.IsEmpty && _more.Contains(e.Location)) { MoreClicked?.Invoke(); return; }
for (int i = 0; i < _rows.Count; i++)
if (_rows[i].Contains(e.Location)) { OpenClicked?.Invoke(_items[i]); return; }
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_blink.Dispose();
DisposeFonts();
}
base.Dispose(disposing);
}
// ── Win32: 픽셀 단위 투명 창 ──
private void Push(Bitmap bmp)
{
IntPtr screenDc = GetDC(IntPtr.Zero);
IntPtr memDc = CreateCompatibleDC(screenDc);
IntPtr hBmp = IntPtr.Zero, old = IntPtr.Zero;
try
{
hBmp = bmp.GetHbitmap(Color.FromArgb(0));
old = SelectObject(memDc, hBmp);
var size = new SIZE { cx = bmp.Width, cy = bmp.Height };
var src = new POINT();
var dst = new POINT { x = Left, y = Top };
var blend = new BLENDFUNCTION { BlendOp = 0, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = 1 };
UpdateLayeredWindow(Handle, screenDc, ref dst, ref size, memDc, ref src, 0, ref blend, ULW_ALPHA);
}
finally
{
if (old != IntPtr.Zero) SelectObject(memDc, old);
if (hBmp != IntPtr.Zero) DeleteObject(hBmp);
DeleteDC(memDc);
ReleaseDC(IntPtr.Zero, screenDc);
}
}
private const int WS_EX_LAYERED = 0x80000, WS_EX_TOOLWINDOW = 0x80, WS_EX_TOPMOST = 0x8, WS_EX_NOACTIVATE = 0x08000000;
private const int ULW_ALPHA = 2;
private const uint SWP_NOSIZE = 0x1, SWP_NOMOVE = 0x2, SWP_NOACTIVATE = 0x10, SWP_SHOWWINDOW = 0x40;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
[StructLayout(LayoutKind.Sequential)]
private struct POINT { public int x, y; }
[StructLayout(LayoutKind.Sequential)]
private struct SIZE { public int cx, cy; }
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct BLENDFUNCTION { public byte BlendOp, BlendFlags, SourceConstantAlpha, AlphaFormat; }
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize,
IntPtr hdcSrc, ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr after, int x, int y, int cx, int cy, uint flags);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
private static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
}
}