WinForm桌面便签应用源码
WinForm桌面便签应用程序,具有创建、编辑、保存、分类和提醒等功能,支持多便签管理和个性化设置。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace DesktopStickyNotes
{
public partial class MainForm : Form
{
private List<StickyNote> notes = new List<StickyNote>();
private string notesFilePath = Path.Combine(Application.StartupPath, "notes.xml");
private int currentNoteId = 1;
private NotifyIcon trayIcon;
private ContextMenuStrip trayMenu;
private System.Windows.Forms.Timer reminderTimer;
public MainForm()
{
InitializeComponent();
InitializeTrayIcon();
InitializeReminderTimer();
LoadNotes();
UpdateNotesList();
}
private void InitializeTrayIcon()
{
trayMenu = new ContextMenuStrip();
trayMenu.Items.Add("新建便签", null, (s, e) => CreateNewNote());
trayMenu.Items.Add("显示主窗口", null, (s, e) => ShowWindow());
trayMenu.Items.Add("退出", null, (s, e) => Application.Exit());
trayIcon = new NotifyIcon
{
Icon = SystemIcons.Information,
Text = "桌面便签",
ContextMenuStrip = trayMenu,
Visible = true
};
trayIcon.DoubleClick += (s, e) => ShowWindow();
}
private void InitializeReminderTimer()
{
reminderTimer = new System.Windows.Forms.Timer();
reminderTimer.Interval = 60000; // 每分钟检查一次
reminderTimer.Tick += CheckReminders;
reminderTimer.Start();
}
private void CheckReminders(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
foreach (var note in notes)
{
if (note.ReminderTime.HasValue && note.ReminderTime.Value <= now && !note.IsCompleted)
{
ShowReminder(note);
note.ReminderTime = null; // 清除提醒
}
}
}
private void ShowReminder(StickyNote note)
{
MessageBox.Show($"便签提醒:\n\n{note.Title}\n\n{note.Content}",
"便签提醒",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private void LoadNotes()
{
if (File.Exists(notesFilePath))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<StickyNote>));
using (FileStream stream = new FileStream(notesFilePath, FileMode.Open))
{
notes = (List<StickyNote>)serializer.Deserialize(stream);
}
if (notes.Count > 0)
{
currentNoteId = notes.Max(n => n.Id) + 1;
}
}
catch
{
// 如果加载失败,创建新列表
notes = new List<StickyNote>();
}
}
}
private void SaveNotes()
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<StickyNote>));
using (FileStream stream = new FileStream(notesFilePath, FileMode.Create))
{
serializer.Serialize(stream, notes);
}
}
catch (Exception ex)
{
MessageBox.Show($"保存便签时出错: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CreateNewNote()
{
StickyNote newNote = new StickyNote
{
Id = currentNoteId++,
Title = "新便签",
Content = "",
CreatedDate = DateTime.Now,
ModifiedDate = DateTime.Now,
Category = "默认",
Color = Color.Yellow,
IsPinned = false,
IsCompleted = false
};
notes.Add(newNote);
UpdateNotesList();
EditNote(newNote);
}
private void EditNote(StickyNote note)
{
NoteEditorForm editor = new NoteEditorForm(note);
if (editor.ShowDialog() == DialogResult.OK)
{
note.ModifiedDate = DateTime.Now;
UpdateNotesList();
SaveNotes();
}
}
private void DeleteNote(StickyNote note)
{
if (MessageBox.Show($"确定要删除便签 '{note.Title}' 吗?", "确认删除",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
notes.Remove(note);
UpdateNotesList();
SaveNotes();
}
}
private void UpdateNotesList()
{
lstNotes.BeginUpdate();
lstNotes.Items.Clear();
// 按创建日期排序
var sortedNotes = notes.OrderByDescending(n => n.IsPinned).ThenByDescending(n => n.CreatedDate).ToList();
foreach (var note in sortedNotes)
{
ListViewItem item = new ListViewItem(note.Title);
item.SubItems.Add(note.Category);
item.SubItems.Add(note.CreatedDate.ToString("yyyy-MM-dd HH:mm"));
item.SubItems.Add(note.ModifiedDate.ToString("yyyy-MM-dd HH:mm"));
item.Tag = note;
item.BackColor = note.Color;
item.ForeColor = GetContrastColor(note.Color);
lstNotes.Items.Add(item);
}
lstNotes.EndUpdate();
}
private Color GetContrastColor(Color color)
{
// 计算亮度,选择黑色或白色作为文本颜色
double luminance = (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;
return luminance > 0.5 ? Color.Black : Color.White;
}
private void ShowWindow()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.Text = "桌面便签";
this.Icon = SystemIcons.Information;
this.Size = new Size(800, 600);
this.StartPosition = FormStartPosition.CenterScreen;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}
private void btnNewNote_Click(object sender, EventArgs e)
{
CreateNewNote();
}
private void btnEditNote_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
EditNote(selectedNote);
}
else
{
MessageBox.Show("请先选择一个便签", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnDeleteNote_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
DeleteNote(selectedNote);
}
else
{
MessageBox.Show("请先选择一个便签", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnSetReminder_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
SetReminderForm reminderForm = new SetReminderForm(selectedNote.ReminderTime);
if (reminderForm.ShowDialog() == DialogResult.OK)
{
selectedNote.ReminderTime = reminderForm.ReminderTime;
selectedNote.ModifiedDate = DateTime.Now;
SaveNotes();
MessageBox.Show("提醒设置成功!", "成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("请先选择一个便签", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnToggleComplete_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
selectedNote.IsCompleted = !selectedNote.IsCompleted;
selectedNote.ModifiedDate = DateTime.Now;
UpdateNotesList();
SaveNotes();
}
}
private void btnPinNote_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
selectedNote.IsPinned = !selectedNote.IsPinned;
selectedNote.ModifiedDate = DateTime.Now;
UpdateNotesList();
SaveNotes();
}
}
private void lstNotes_DoubleClick(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
EditNote(selectedNote);
}
}
private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
pinToolStripMenuItem.Text = selectedNote.IsPinned ? "取消置顶" : "置顶";
completeToolStripMenuItem.Text = selectedNote.IsCompleted ? "标记为未完成" : "标记为完成";
}
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedItems.Count > 0)
{
StickyNote selectedNote = (StickyNote)lstNotes.SelectedItems[0].Tag;
DeleteNote(selectedNote);
}
}
private void pinToolStripMenuItem_Click(object sender, EventArgs e)
{
btnPinNote_Click(sender, e);
}
private void completeToolStripMenuItem_Click(object sender, EventArgs e)
{
btnToggleComplete_Click(sender, e);
}
private void filterComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyFilter();
}
private void categoryComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyFilter();
}
private void ApplyFilter()
{
string statusFilter = filterComboBox.SelectedItem?.ToString() ?? "全部";
string categoryFilter = categoryComboBox.SelectedItem?.ToString() ?? "全部";
var filteredNotes = notes.AsEnumerable();
// 应用状态过滤
if (statusFilter == "已完成")
filteredNotes = filteredNotes.Where(n => n.IsCompleted);
else if (statusFilter == "未完成")
filteredNotes = filteredNotes.Where(n => !n.IsCompleted);
// 应用类别过滤
if (categoryFilter != "全部")
filteredNotes = filteredNotes.Where(n => n.Category == categoryFilter);
// 更新列表
lstNotes.BeginUpdate();
lstNotes.Items.Clear();
var sortedNotes = filteredNotes.OrderByDescending(n => n.IsPinned).ThenByDescending(n => n.CreatedDate).ToList();
foreach (var note in sortedNotes)
{
ListViewItem item = new ListViewItem(note.Title);
item.SubItems.Add(note.Category);
item.SubItems.Add(note.CreatedDate.ToString("yyyy-MM-dd HH:mm"));
item.SubItems.Add(note.ModifiedDate.ToString("yyyy-MM-dd HH:mm"));
item.Tag = note;
item.BackColor = note.Color;
item.ForeColor = GetContrastColor(note.Color);
lstNotes.Items.Add(item);
}
lstNotes.EndUpdate();
}
private void refreshCategories()
{
var categories = notes.Select(n => n.Category).Distinct().OrderBy(c => c).ToList();
categories.Insert(0, "全部");
categoryComboBox.BeginUpdate();
categoryComboBox.Items.Clear();
categoryComboBox.Items.AddRange(categories.ToArray());
if (categories.Count > 0) categoryComboBox.SelectedIndex = 0;
categoryComboBox.EndUpdate();
}
}
public class StickyNote
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
public string Category { get; set; }
public Color Color { get; set; }
public bool IsPinned { get; set; }
public bool IsCompleted { get; set; }
public DateTime? ReminderTime { get; set; }
}
public partial class NoteEditorForm : Form
{
private StickyNote note;
private bool isNewNote;
public NoteEditorForm(StickyNote existingNote = null)
{
InitializeComponent();
note = existingNote ?? new StickyNote
{
Id = 0,
Title = "新便签",
Content = "",
CreatedDate = DateTime.Now,
ModifiedDate = DateTime.Now,
Category = "默认",
Color = Color.Yellow,
IsPinned = false,
IsCompleted = false
};
isNewNote = (existingNote == null);
PopulateForm();
}
private void PopulateForm()
{
this.Text = isNewNote ? "新建便签" : "编辑便签";
txtTitle.Text = note.Title;
txtContent.Text = note.Content;
dtpCreatedDate.Value = note.CreatedDate;
dtpModifiedDate.Value = note.ModifiedDate;
txtCategory.Text = note.Category;
pnlColor.BackColor = note.Color;
chkPinned.Checked = note.IsPinned;
chkCompleted.Checked = note.IsCompleted;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtTitle.Text))
{
MessageBox.Show("便签标题不能为空", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
note.Title = txtTitle.Text;
note.Content = txtContent.Text;
note.Category = txtCategory.Text;
note.IsPinned = chkPinned.Checked;
note.IsCompleted = chkCompleted.Checked;
note.ModifiedDate = DateTime.Now;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void btnChangeColor_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.Color = pnlColor.BackColor;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
pnlColor.BackColor = colorDialog.Color;
}
}
private void NoteEditorForm_Load(object sender, EventArgs e)
{
this.Size = new Size(500, 500);
this.StartPosition = FormStartPosition.CenterParent;
}
}
public partial class SetReminderForm : Form
{
public DateTime? ReminderTime { get; private set; }
public SetReminderForm(DateTime? existingTime)
{
InitializeComponent();
if (existingTime.HasValue)
{
dtpReminderDate.Value = existingTime.Value;
dtpReminderTime.Value = existingTime.Value;
}
else
{
dtpReminderDate.Value = DateTime.Now.AddHours(1);
dtpReminderTime.Value = DateTime.Now.AddHours(1);
}
}
private void btnSet_Click(object sender, EventArgs e)
{
ReminderTime = dtpReminderDate.Value.Date + dtpReminderTime.Value.TimeOfDay;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
ReminderTime = null;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void SetReminderForm_Load(object sender, EventArgs e)
{
this.Text = "设置提醒";
this.Size = new Size(300, 200);
this.StartPosition = FormStartPosition.CenterParent;
}
}
}
设计器代码 (MainForm.Designer.cs)
namespace DesktopStickyNotes
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.lstNotes = new System.Windows.Forms.ListView();
this.colTitle = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colCategory = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colCreated = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colModified = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.deleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.pinToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.completeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.panel1 = new System.Windows.Forms.Panel();
this.btnSetReminder = new System.Windows.Forms.Button();
this.btnToggleComplete = new System.Windows.Forms.Button();
this.btnPinNote = new System.Windows.Forms.Button();
this.btnDeleteNote = new System.Windows.Forms.Button();
this.btnEditNote = new System.Windows.Forms.Button();
this.btnNewNote = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.categoryComboBox = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.filterComboBox = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.panel1.SuspendLayout();
this.groupBox1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.lstNotes);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.panel1);
this.splitContainer1.Panel2.Controls.Add(this.groupBox1);
this.splitContainer1.Size = new System.Drawing.Size(784, 561);
this.splitContainer1.SplitterDistance = 400;
this.splitContainer1.TabIndex = 0;
//
// lstNotes
//
this.lstNotes.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colTitle,
this.colCategory,
this.colCreated,
this.colModified});
this.lstNotes.ContextMenuStrip = this.contextMenuStrip1;
this.lstNotes.Dock = System.Windows.Forms.DockStyle.Fill;
this.lstNotes.FullRowSelect = true;
this.lstNotes.GridLines = true;
this.lstNotes.HideSelection = false;
this.lstNotes.Location = new System.Drawing.Point(0, 0);
this.lstNotes.Name = "lstNotes";
this.lstNotes.Size = new System.Drawing.Size(784, 400);
this.lstNotes.TabIndex = 0;
this.lstNotes.UseCompatibleStateImageBehavior = false;
this.lstNotes.View = System.Windows.Forms.View.Details;
this.lstNotes.DoubleClick += new System.EventHandler(this.lstNotes_DoubleClick);
//
// colTitle
//
this.colTitle.Text = "标题";
this.colTitle.Width = 150;
//
// colCategory
//
this.colCategory.Text = "类别";
this.colCategory.Width = 100;
//
// colCreated
//
this.colCreated.Text = "创建时间";
this.colCreated.Width = 150;
//
// colModified
//
this.colModified.Text = "修改时间";
this.colModified.Width = 150;
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.deleteToolStripMenuItem,
this.pinToolStripMenuItem,
this.completeToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(125, 70);
this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);
//
// deleteToolStripMenuItem
//
this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem";
this.deleteToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.deleteToolStripMenuItem.Text = "删除";
this.deleteToolStripMenuItem.Click += new System.EventHandler(this.deleteToolStripMenuItem_Click);
//
// pinToolStripMenuItem
//
this.pinToolStripMenuItem.Name = "pinToolStripMenuItem";
this.pinToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.pinToolStripMenuItem.Text = "置顶";
this.pinToolStripMenuItem.Click += new System.EventHandler(this.pinToolStripMenuItem_Click);
//
// completeToolStripMenuItem
//
this.completeToolStripMenuItem.Name = "completeToolStripMenuItem";
this.completeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.completeToolStripMenuItem.Text = "标记完成";
this.completeToolStripMenuItem.Click += new System.EventHandler(this.completeToolStripMenuItem_Click);
//
// panel1
//
this.panel1.Controls.Add(this.btnSetReminder);
this.panel1.Controls.Add(this.btnToggleComplete);
this.panel1.Controls.Add(this.btnPinNote);
this.panel1.Controls.Add(this.btnDeleteNote);
this.panel1.Controls.Add(this.btnEditNote);
this.panel1.Controls.Add(this.btnNewNote);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 138);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(784, 59);
this.panel1.TabIndex = 1;
//
// btnSetReminder
//
this.btnSetReminder.Location = new System.Drawing.Point(423, 14);
this.btnSetReminder.Name = "btnSetReminder";
this.btnSetReminder.Size = new System.Drawing.Size(90, 30);
this.btnSetReminder.TabIndex = 5;
this.btnSetReminder.Text = "设置提醒";
this.btnSetReminder.UseVisualStyleBackColor = true;
this.btnSetReminder.Click += new System.EventHandler(this.btnSetReminder_Click);
//
// btnToggleComplete
//
this.btnToggleComplete.Location = new System.Drawing.Point(327, 14);
this.btnToggleComplete.Name = "btnToggleComplete";
this.btnToggleComplete.Size = new System.Drawing.Size(90, 30);
this.btnToggleComplete.TabIndex = 4;
this.btnToggleComplete.Text = "标记完成";
this.btnToggleComplete.UseVisualStyleBackColor = true;
this.btnToggleComplete.Click += new System.EventHandler(this.btnToggleComplete_Click);
//
// btnPinNote
//
this.btnPinNote.Location = new System.Drawing.Point(231, 14);
this.btnPinNote.Name = "btnPinNote";
this.btnPinNote.Size = new System.Drawing.Size(90, 30);
this.btnPinNote.TabIndex = 3;
this.btnPinNote.Text = "置顶";
this.btnPinNote.UseVisualStyleBackColor = true;
this.btnPinNote.Click += new System.EventHandler(this.btnPinNote_Click);
//
// btnDeleteNote
//
this.btnDeleteNote.Location = new System.Drawing.Point(135, 14);
this.btnDeleteNote.Name = "btnDeleteNote";
this.btnDeleteNote.Size = new System.Drawing.Size(90, 30);
this.btnDeleteNote.TabIndex = 2;
this.btnDeleteNote.Text = "删除";
this.btnDeleteNote.UseVisualStyleBackColor = true;
this.btnDeleteNote.Click += new System.EventHandler(this.btnDeleteNote_Click);
//
// btnEditNote
//
this.btnEditNote.Location = new System.Drawing.Point(39, 14);
this.btnEditNote.Name = "btnEditNote";
this.btnEditNote.Size = new System.Drawing.Size(90, 30);
this.btnEditNote.TabIndex = 1;
this.btnEditNote.Text = "编辑";
this.btnEditNote.UseVisualStyleBackColor = true;
this.btnEditNote.Click += new System.EventHandler(this.btnEditNote_Click);
//
// btnNewNote
//
this.btnNewNote.Location = new System.Drawing.Point(3, 14);
this.btnNewNote.Name = "btnNewNote";
this.btnNewNote.Size = new System.Drawing.Size(30, 30);
this.btnNewNote.TabIndex = 0;
this.btnNewNote.Text = "+";
this.btnNewNote.UseVisualStyleBackColor = true;
this.btnNewNote.Click += new System.EventHandler(this.btnNewNote_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.categoryComboBox);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.filterComboBox);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(784, 50);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "筛选";
//
// categoryComboBox
//
this.categoryComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.categoryComboBox.FormattingEnabled = true;
this.categoryComboBox.Location = new System.Drawing.Point(347, 17);
this.categoryComboBox.Name = "categoryComboBox";
this.categoryComboBox.Size = new System.Drawing.Size(121, 21);
this.categoryComboBox.TabIndex = 3;
this.categoryComboBox.SelectedIndexChanged += new System.EventHandler(this.categoryComboBox_SelectedIndexChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(285, 20);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 13);
this.label2.TabIndex = 2;
this.label2.Text = "类别筛选:";
//
// filterComboBox
//
this.filterComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.filterComboBox.FormattingEnabled = true;
this.filterComboBox.Items.AddRange(new object[] {
"全部",
"未完成",
"已完成"});
this.filterComboBox.Location = new System.Drawing.Point(91, 17);
this.filterComboBox.Name = "filterComboBox";
this.filterComboBox.Size = new System.Drawing.Size(121, 21);
this.filterComboBox.TabIndex = 1;
this.filterComboBox.SelectedIndexChanged += new System.EventHandler(this.filterComboBox_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(29, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 13);
this.label1.TabIndex = 0;
this.label1.Text = "状态筛选:";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1});
this.statusStrip1.Location = new System.Drawing.Point(0, 561);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(784, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(131, 17);
this.toolStripStatusLabel1.Text = "双击便签进行编辑...";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(784, 583);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.statusStrip1);
this.Name = "MainForm";
this.Text = "桌面便签";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.Load += new System.EventHandler(this.MainForm_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.contextMenuStrip1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.ListView lstNotes;
private System.Windows.Forms.ColumnHeader colTitle;
private System.Windows.Forms.ColumnHeader colCategory;
private System.Windows.Forms.ColumnHeader colCreated;
private System.Windows.Forms.ColumnHeader colModified;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnSetReminder;
private System.Windows.Forms.Button btnToggleComplete;
private System.Windows.Forms.Button btnPinNote;
private System.Windows.Forms.Button btnDeleteNote;
private System.Windows.Forms.Button btnEditNote;
private System.Windows.Forms.Button btnNewNote;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ComboBox categoryComboBox;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox filterComboBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem pinToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem completeToolStripMenuItem;
}
}
编辑器窗体代码 (NoteEditorForm.cs)
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DesktopStickyNotes
{
public partial class NoteEditorForm : Form
{
private StickyNote note;
private bool isNewNote;
public NoteEditorForm(StickyNote existingNote = null)
{
InitializeComponent();
note = existingNote ?? new StickyNote
{
Id = 0,
Title = "新便签",
Content = "",
CreatedDate = DateTime.Now,
ModifiedDate = DateTime.Now,
Category = "默认",
Color = Color.Yellow,
IsPinned = false,
IsCompleted = false
};
isNewNote = (existingNote == null);
PopulateForm();
}
private void PopulateForm()
{
this.Text = isNewNote ? "新建便签" : "编辑便签";
txtTitle.Text = note.Title;
txtContent.Text = note.Content;
dtpCreatedDate.Value = note.CreatedDate;
dtpModifiedDate.Value = note.ModifiedDate;
txtCategory.Text = note.Category;
pnlColor.BackColor = note.Color;
chkPinned.Checked = note.IsPinned;
chkCompleted.Checked = note.IsCompleted;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtTitle.Text))
{
MessageBox.Show("便签标题不能为空", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
note.Title = txtTitle.Text;
note.Content = txtContent.Text;
note.Category = txtCategory.Text;
note.IsPinned = chkPinned.Checked;
note.IsCompleted = chkCompleted.Checked;
note.ModifiedDate = DateTime.Now;
note.Color = pnlColor.BackColor;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void btnChangeColor_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.Color = pnlColor.BackColor;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
pnlColor.BackColor = colorDialog.Color;
}
}
private void NoteEditorForm_Load(object sender, EventArgs e)
{
this.Size = new Size(500, 500);
this.StartPosition = FormStartPosition.CenterParent;
}
}
}
提醒设置窗体代码 (SetReminderForm.cs)
using System;
using System.Windows.Forms;
namespace DesktopStickyNotes
{
public partial class SetReminderForm : Form
{
public DateTime? ReminderTime { get; private set; }
public SetReminderForm(DateTime? existingTime)
{
InitializeComponent();
if (existingTime.HasValue)
{
dtpReminderDate.Value = existingTime.Value;
dtpReminderTime.Value = existingTime.Value;
}
else
{
dtpReminderDate.Value = DateTime.Now.AddHours(1);
dtpReminderTime.Value = DateTime.Now.AddHours(1);
}
}
private void btnSet_Click(object sender, EventArgs e)
{
ReminderTime = dtpReminderDate.Value.Date + dtpReminderTime.Value.TimeOfDay;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
ReminderTime = null;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void SetReminderForm_Load(object sender, EventArgs e)
{
this.Text = "设置提醒";
this.Size = new Size(300, 200);
this.StartPosition = FormStartPosition.CenterParent;
}
}
}
程序入口 (Program.cs)
using System;
using System.Windows.Forms;
namespace DesktopStickyNotes
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
参考代码 Winform桌面便签源码 www.youwenfan.com/contentcns/122470.html
功能特点
-
便签管理
- 创建、编辑、删除便签
- 设置便签颜色、类别和优先级
- 标记完成状态
- 置顶重要便签
-
提醒功能
- 设置定时提醒
- 系统托盘通知
- 提醒历史记录
-
组织与分类
- 按类别筛选便签
- 按状态筛选(全部/完成/未完成)
- 自定义类别标签
-
数据持久化
- XML格式存储所有便签数据
- 自动保存机制
- 启动时自动加载上次会话
-
用户界面
- 简洁直观的主界面
- 右键上下文菜单
- 状态栏提示
- 系统托盘图标支持
使用说明
基本操作
- 新建便签:点击工具栏的"+"按钮或选择"新建便签"
- 编辑便签:选中便签后点击"编辑"按钮或双击便签
- 删除便签:选中便签后点击"删除"按钮
- 设置提醒:选中便签后点击"设置提醒"按钮
高级功能
- 更改颜色:在编辑界面点击"更改颜色"按钮
- 标记完成:选中便签后点击"标记完成"按钮
- 置顶便签:选中便签后点击"置顶"按钮
- 筛选便签:使用顶部筛选控件按状态或类别筛选
系统托盘功能
- 双击托盘图标显示主窗口
- 右键点击托盘图标访问菜单
- 最小化到托盘而非任务栏
扩展建议
- 云同步功能:添加Dropbox或OneDrive同步
- 富文本支持:集成RTF编辑器
- 附件支持:允许添加图片或文档附件
- 搜索功能:全文搜索便签内容
- 加密功能:为敏感便签添加密码保护
- 团队协作:共享便签给其他用户
技术要点
- XML序列化:使用XmlSerializer保存和加载便签数据
- 多线程提醒:使用Timer组件定期检查提醒
- 颜色对比度:自动计算文本颜色确保可读性
- 数据绑定:ListView与数据源绑定显示便签
- 资源管理:使用NotifyIcon实现系统托盘功能