C#で付箋紙を作る(第三回目) XMLファイルからパラメーターを読み込んで反映する
2017/07/17
前回の記事はこちら。
前回は付箋紙の本体を形作りました。背景色やフォントは固定のままでしたので、今回はそれらが自由に設定できるように、各パラメーターをXMLファイルに設定し、起動時にそれを読み込んで反映させるのが今回のゴールです。
今回の完成形
起動すると付箋紙が現れるのは前回と同様ですが、今回はXMLファイルから各パラメーターを読み込んで反映させています。
読み込む内容は、画面上の位置、付箋紙のサイズ、背景色、フォント、表示するテキストです。これを起動時に読み込んで反映させるわけです。
[ad#top-1]
XMLファイル
こちらがXMLファイルです。各パラメーターごとに数値を設定しています。今回はデータは1個のみ。最終的には「Sticky」要素が複数あり、その数だけ付箋紙が起動するようにしますが、今回は1個のデータを読み込むだけとします。
背景色のデータは悩みましたが、red, green, blueをそれぞれ持つことにしました。
<?xml version="1.0" encoding="utf-8"?>
<Stickys xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Sticky>
<text>テスト付箋紙</text>
<width>300</width>
<height>150</height>
<top>200</top>
<left>200</left>
<red>255</red>
<green>200</green>
<blue>200</blue>
<font-family>Meiryo UI</font-family>
<font-size>14</font-size>
</Sticky>
</Stickys>
ソースコード
こちらが、C#のソースコードです。
getData()というメソッドがXMLからデータを読み込むところです。ソースの最後にXMLの構造に対応したデータクラスがあり、これに基づいてパースしています。
前回の機能はそのままですが新規作成機能だけは無効化しています。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
class Sticky{
static void Main(){
Application.Run(new StickyNote());
}
}
partial class StickyNote : Form{
private Point mousePoint; //マウスのクリック位置を記憶
private Label label;
private TextBox textBox;
private StickyList list;
public StickyNote(){
this.StartPosition = FormStartPosition.Manual;
this.Text = "付箋紙";
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
this.MouseDown += new MouseEventHandler(MouseDowned);
this.MouseMove += new MouseEventHandler(MouseMoved);
this.DoubleClick += new EventHandler(DoubleClicked);
this.getData();
this.Width = list.DataList[0].Width;
this.Height = list.DataList[0].Height;
this.Location = new Point(list.DataList[0].Top, list.DataList[0].Left);
this.setComponents();
}
private void DoubleClicked(object sender, EventArgs e){
if(this.FormBorderStyle == FormBorderStyle.None){
this.FormBorderStyle = FormBorderStyle.Sizable;
this.ControlBox = false;
this.Controls.Remove(label);
this.Controls.Add(textBox);
textBox.Text = label.Text;
}else{
this.FormBorderStyle = FormBorderStyle.None;
this.Controls.Remove(textBox);
this.Controls.Add(label);
label.Text = textBox.Text;
}
}
private void popClicked(Object sender, ToolStripItemClickedEventArgs e) {
if(e.ClickedItem.Name=="closeBtn") this.Close();
if(e.ClickedItem.Name=="newBtn") {
//今回は新規作成機能は無効化
//System.Diagnostics.Process.Start("sticky.exe", "");
}
}
private void getData(){
FileStream file = new FileStream("sticky.xml", FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(StickyList));
list = (StickyList)serializer.Deserialize(file);
file.Close();
}
private void setComponents(){
Font font = new Font(list.DataList[0].FontFamily, list.DataList[0].FontSize);
label = new Label(){
Dock = DockStyle.Fill,
Font = font,
BorderStyle = BorderStyle.FixedSingle,
Parent=this,
//以下パラメーター設定
Text = list.DataList[0].Text,
BackColor = Color.FromArgb(list.DataList[0].Red, list.DataList[0].Green, list.DataList[0].Blue),
};
label.DoubleClick += new EventHandler(DoubleClicked);
label.MouseDown += new MouseEventHandler(MouseDowned);
label.MouseMove += new MouseEventHandler(MouseMoved);
textBox = new TextBox(){
Dock = DockStyle.Fill,
Font = font,
Multiline = true,
WordWrap = false,
//以下パラメーター設定
Text = list.DataList[0].Text,
BackColor = Color.FromArgb(list.DataList[0].Red, list.DataList[0].Green, list.DataList[0].Blue),
};
textBox.DoubleClick += new EventHandler(DoubleClicked);
ToolStripMenuItem newItem = new ToolStripMenuItem(){
Text = "新規付箋紙",
Name = "newBtn",
};
ToolStripMenuItem closeItem = new ToolStripMenuItem(){
Text = "この付箋紙を捨てる",
Name = "closeBtn",
};
ContextMenuStrip pop = new ContextMenuStrip();
pop.Items.AddRange(new ToolStripItem[] {
newItem, new ToolStripSeparator(), closeItem
});
pop.ItemClicked += new ToolStripItemClickedEventHandler(popClicked);
this.ContextMenuStrip = pop;
}
//マウスのボタンが押されたとき
private void MouseDowned(object sender, System.Windows.Forms.MouseEventArgs e){
if ((e.Button & MouseButtons.Left) == MouseButtons.Left){
//位置を記憶する
mousePoint = new Point(e.X, e.Y);
}
}
//マウスが動いたとき
private void MouseMoved(object sender, System.Windows.Forms.MouseEventArgs e){
if ((e.Button & MouseButtons.Left) == MouseButtons.Left){
this.Location = new Point(
this.Location.X + e.X - mousePoint.X,
this.Location.Y + e.Y - mousePoint.Y);
}
}
}
[XmlRoot("Sticky")]
public class StickyData{
[XmlElement("text")]
public string Text { get; set; }
[XmlElement("width")]
public int Width { get; set; }
[XmlElement("height")]
public int Height { get; set; }
[XmlElement("top")]
public int Top { get; set; }
[XmlElement("left")]
public int Left { get; set; }
[XmlElement("red")]
public int Red { get; set; }
[XmlElement("green")]
public int Green { get; set; }
[XmlElement("blue")]
public int Blue { get; set; }
[XmlElement("font-family")]
public string FontFamily { get; set; }
[XmlElement("font-size")]
public int FontSize { get; set; }
}
[XmlRoot("Stickys")]
public class StickyList{
[XmlElement("Sticky")]
public List<StickyData> DataList { get; set; }
}
関連記事
[ad#ad-1]
スポンサーリンク




