主にプログラミングに関して。Python, .NET Framework(C#), JavaScript, その他いくらか。
記事にあるサンプルやコードは要検証。使用に際しては責任を負いかねます

スポンサーサイト

                
tags:
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

Boo: Windows.Formsでシャットダウンタイマを作る

                
Booの実用性を考える。二回目。今回はWPFアプリケーションに続いて、Formアプリケーションを作ってみる。HelloWorld!はWPFでやったので、今回はもう少し使い道のあるものを。

PCで曲をかけながら床につくことがあって、バッチファイルでシャットダウンタイマーをかけていた。これをGUIで時間指定できるタイマーにおきかえる。IDEとしてSharpDevelopを使う。

SharpDevelopで新規ソリューションの作成を選び、Windows Applicationを選ぶ。あとはスクリプトを書くだけ。SharpDevelopにはデザイナも入っているので、マウスを使いながらサクサクGUIが作れる。

対応は.NET 4.0の動作する環境。VMWareに入れたWin8で動作を確認した。

130416_1.jpg



*.boo
namespace BooTimer

import System
import System.Collections
import System.Drawing
import System.Windows.Forms
import System.Diagnostics

partial class MainForm:
private t as TimeSpan
private counting = false

public def constructor():
// The InitializeComponent() call is required for Windows Forms designer support.
InitializeComponent()
self.Text = "ConutDown and ShutDown"

private def Tick(sender as object, e as System.EventArgs):
self.t -= TimeSpan(0, 0, 1)
self.label2.Text = self.t.ToString() + ' to Shutdown'
if self.t.TotalSeconds <= 0.0:
Process.Start("shutdown","/s /t 0")

private def Label1Click(sender as object, e as System.EventArgs):
pass

private def TextBox1TextChanged(sender as object, e as System.EventArgs):
pass

private def Button1Click(sender as object, e as System.EventArgs):
try:
h = int.Parse(self.textBox1.Text)
m = int.Parse(self.textBox2.Text)
except:
return
print h, m
self.t = TimeSpan(h, m, 0)
self.timer1.Tick += self.Tick as System.EventHandler
self.timer1.Interval = 1000
self.timer1.Start()
self.counting = true

private def TextBoxKeyPress(sender as object, e as KeyPressEventArgs):
if not char.IsDigit(e.KeyChar):
e.Handled = true

[STAThread]
public def Main(argv as (string)) as void:
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
Application.Run(MainForm())


*.Designer.boo
namespace BooTimer

partial class MainForm(System.Windows.Forms.Form):
private components as System.ComponentModel.IContainer = null

protected override def Dispose(disposing as bool) as void:
if disposing:
if components is not null:
components.Dispose()
super(disposing)

// This method is required for Windows Forms designer support.
// Do not change the method contents inside the source code editor. The Forms designer might
// not be able to load this method if it was changed manually.
private def InitializeComponent():
self.components = System.ComponentModel.Container()
self.timer1 = System.Windows.Forms.Timer(self.components)
self.textBox1 = System.Windows.Forms.TextBox()
self.textBox2 = System.Windows.Forms.TextBox()
self.label1 = System.Windows.Forms.Label()
self.button1 = System.Windows.Forms.Button()
self.label2 = System.Windows.Forms.Label()
self.SuspendLayout()
#
# textBox1
#
self.textBox1.Location = System.Drawing.Point(12, 23)
self.textBox1.MaxLength = 2
self.textBox1.Name = "textBox1"
self.textBox1.Size = System.Drawing.Size(35, 19)
self.textBox1.TabIndex = 0
self.textBox1.Text = "1"
self.textBox1.TextChanged += self.TextBox1TextChanged as System.EventHandler
self.textBox1.KeyPress += self.TextBoxKeyPress as System.Windows.Forms.KeyPressEventHandler
#
# textBox2
#
self.textBox2.Location = System.Drawing.Point(69, 23)
self.textBox2.MaxLength = 2
self.textBox2.Name = "textBox2"
self.textBox2.Size = System.Drawing.Size(35, 19)
self.textBox2.TabIndex = 1
self.textBox2.Text = "00"
self.textBox2.KeyPress += self.TextBoxKeyPress as System.Windows.Forms.KeyPressEventHandler
#
# label1
#
self.label1.Font = System.Drawing.Font("MS UI Gothic", 9, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, cast(System.Byte,128))
self.label1.Location = System.Drawing.Point(53, 26)
self.label1.Name = "label1"
self.label1.Size = System.Drawing.Size(10, 17)
self.label1.TabIndex = 2
self.label1.Text = ":"
self.label1.Click += self.Label1Click as System.EventHandler
#
# button1
#
self.button1.Location = System.Drawing.Point(123, 21)
self.button1.Name = "button1"
self.button1.Size = System.Drawing.Size(75, 23)
self.button1.TabIndex = 3
self.button1.Text = "CountDown"
self.button1.UseVisualStyleBackColor = true
self.button1.Click += self.Button1Click as System.EventHandler
#
# label2
#
self.label2.Location = System.Drawing.Point(13, 74)
self.label2.Name = "label2"
self.label2.Size = System.Drawing.Size(185, 23)
self.label2.TabIndex = 4
#
# MainForm
#
self.AutoScaleDimensions = System.Drawing.SizeF(6, 12)
self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
self.ClientSize = System.Drawing.Size(284, 116)
self.Controls.Add(self.label2)
self.Controls.Add(self.button1)
self.Controls.Add(self.label1)
self.Controls.Add(self.textBox2)
self.Controls.Add(self.textBox1)
self.Name = "MainForm"
self.Text = "MainForm"
self.ResumeLayout(false)
self.PerformLayout()
private label2 as System.Windows.Forms.Label
private button1 as System.Windows.Forms.Button
private label1 as System.Windows.Forms.Label
private textBox2 as System.Windows.Forms.TextBox
private textBox1 as System.Windows.Forms.TextBox
private timer1 as System.Windows.Forms.Timer
            

コメントの投稿

非公開コメント

プロフィール

Matoba

Author:Matoba

最新記事
リンク
作ったものなど
月別アーカイブ
カテゴリ
タグリスト

検索フォーム
Amazon
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。