Changed WidgetForm

This commit is contained in:
Victor 2013-10-07 19:07:28 +03:00
parent d16f5cbd63
commit eb27840141
2 changed files with 286 additions and 12 deletions

View File

@ -1,5 +1,7 @@
namespace BatteryWidget {
partial class WidgetForm {
partial class WidgetForm
{
/// <summary>
/// Required designer variable.
/// </summary>
@ -9,8 +11,10 @@
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
@ -22,13 +26,108 @@
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
private void InitializeComponent()
{
this.mainTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.titleLabel = new System.Windows.Forms.Label();
this.exitLabel = new System.Windows.Forms.Label();
this.contentPanel = new System.Windows.Forms.Panel();
this.imageLabel = new System.Windows.Forms.Label();
this.mainTableLayoutPanel.SuspendLayout();
this.contentPanel.SuspendLayout();
this.SuspendLayout();
//
// mainTableLayoutPanel
//
this.mainTableLayoutPanel.ColumnCount = 2;
this.mainTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.mainTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 14F));
this.mainTableLayoutPanel.Controls.Add(this.titleLabel, 0, 0);
this.mainTableLayoutPanel.Controls.Add(this.exitLabel, 1, 0);
this.mainTableLayoutPanel.Controls.Add(this.contentPanel, 0, 1);
this.mainTableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.mainTableLayoutPanel.Location = new System.Drawing.Point(0, 0);
this.mainTableLayoutPanel.Name = "mainTableLayoutPanel";
this.mainTableLayoutPanel.RowCount = 2;
this.mainTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 12F));
this.mainTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.mainTableLayoutPanel.Size = new System.Drawing.Size(120, 69);
this.mainTableLayoutPanel.TabIndex = 0;
//
// titleLabel
//
this.titleLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.titleLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.titleLabel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.titleLabel.Location = new System.Drawing.Point(0, 0);
this.titleLabel.Margin = new System.Windows.Forms.Padding(0);
this.titleLabel.Name = "titleLabel";
this.titleLabel.Size = new System.Drawing.Size(106, 12);
this.titleLabel.TabIndex = 0;
this.titleLabel.Text = "Android Battery Widget";
this.titleLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// exitLabel
//
this.exitLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(196)))), ((int)(((byte)(2)))), ((int)(((byte)(2)))));
this.exitLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.exitLabel.Image = global::BatteryWidget.Properties.Resources.close;
this.exitLabel.Location = new System.Drawing.Point(107, 0);
this.exitLabel.Margin = new System.Windows.Forms.Padding(1, 0, 1, 0);
this.exitLabel.Name = "exitLabel";
this.exitLabel.Size = new System.Drawing.Size(12, 12);
this.exitLabel.TabIndex = 1;
//
// contentPanel
//
this.contentPanel.Controls.Add(this.imageLabel);
this.contentPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.contentPanel.Location = new System.Drawing.Point(0, 12);
this.contentPanel.Margin = new System.Windows.Forms.Padding(0);
this.contentPanel.Name = "contentPanel";
this.contentPanel.Size = new System.Drawing.Size(106, 57);
this.contentPanel.TabIndex = 2;
//
// imageLabel
//
this.imageLabel.BackColor = System.Drawing.Color.DarkGreen;
this.imageLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.imageLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.imageLabel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.imageLabel.Image = global::BatteryWidget.Properties.Resources.green;
this.imageLabel.Location = new System.Drawing.Point(0, 0);
this.imageLabel.Margin = new System.Windows.Forms.Padding(0);
this.imageLabel.Name = "imageLabel";
this.imageLabel.Size = new System.Drawing.Size(106, 57);
this.imageLabel.TabIndex = 0;
this.imageLabel.Text = "99%";
this.imageLabel.TextAlign = System.Drawing.ContentAlignment.BottomRight;
//
// WidgetForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
this.BackColor = System.Drawing.Color.Fuchsia;
this.ClientSize = new System.Drawing.Size(120, 69);
this.Controls.Add(this.mainTableLayoutPanel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "WidgetForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Android Battery Widget";
this.mainTableLayoutPanel.ResumeLayout(false);
this.contentPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel mainTableLayoutPanel;
private System.Windows.Forms.Label titleLabel;
private System.Windows.Forms.Label exitLabel;
private System.Windows.Forms.Panel contentPanel;
private System.Windows.Forms.Label imageLabel;
}
}

View File

@ -1,15 +1,190 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BatteryWidget.Properties;
namespace BatteryWidget {
public partial class WidgetForm : Form {
private const int HIDE_CONTROLS_INTERVAL = 1000;
private const int UPDATE_INFO_INTERVAL = 60 * 1000;
/** Contol moving window */
private bool windowDragStatus;
private Point clickPoint;
private Timer updateInfoTimer;
private Timer hideControlsTimer;
private Image grayImage;
private BatteryGraph graph;
#region Initialize
public WidgetForm() {
InitializeComponent();
imageLabel.Image = ImageLoader.getImage(ImageLoader.ImageName.Green);
grayImage = ImageLoader.getImage(ImageLoader.ImageName.Gray);
graph = new BatteryGraph();
initTimers();
initWindow();
initMouseEventHandler();
updateInfo();
}
private void initTimers() {
// Timer for hide window's title
hideControlsTimer = new Timer();
hideControlsTimer.Interval = HIDE_CONTROLS_INTERVAL;
hideControlsTimer.Tick += (s1, e1) => {
hideControlsTimer.Stop();
titleLabel.Hide();
exitLabel.Hide();
};
// Timer for update info
updateInfoTimer = new Timer();
updateInfoTimer.Interval = UPDATE_INFO_INTERVAL;
updateInfoTimer.Tick += (s, e) => updateInfo();
updateInfoTimer.Start();
}
private void initWindow() {
this.TransparencyKey = this.BackColor;
this.ShowInTaskbar = false;
// Hide controls
titleLabel.Hide();
exitLabel.Hide();
// Restore window position
this.Location = Settings.Default.windowPosition;
}
private void initMouseEventHandler() {
// Moving window by title
titleLabel.MouseDown += (s, e) => {
if (e.Button == MouseButtons.Left) {
// Moving mode
windowDragStatus = true;
clickPoint = new Point(e.X, e.Y);
} else {
windowDragStatus = false;
}
};
titleLabel.MouseMove += (s, e) => {
if (windowDragStatus) {
Point pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
pointMoveTo.Offset(-clickPoint.X, -clickPoint.Y);
this.Location = pointMoveTo;
}
};
titleLabel.MouseUp += (s, e) => {
windowDragStatus = false;
Settings.Default.windowPosition = this.Location;
Settings.Default.Save();
};
// Exit button
exitLabel.MouseClick += (s, e) => this.Close();
// Show/hide title and exit button
imageLabel.MouseEnter += (s, e) => {
// Show controls
titleLabel.Show();
exitLabel.Show();
hideControlsTimer.Stop();
};
imageLabel.MouseLeave += (s, e) => {
// Hide controls
hideControlsTimer.Start();
};
imageLabel.MouseClick += (s, e) => {
// Change view by click: graph/image
graph.revertShowState();
updateInfo();
};
}
#endregion
private void updateInfo() {
int percent = Battery.getBatteryPercentage();
graph.addPercent(percent);
// Display graph
if (graph.graphIsShowing()) {
Bitmap bmp = new Bitmap(Size.Width, Size.Height);
Graphics g = Graphics.FromImage(bmp);
graph.drawGraph(g);
g.Dispose();
imageLabel.Text = "";
imageLabel.Image = bmp;
return;
}
imageLabel.Text = percent + "%";
imageLabel.Image = makeImage(percent);
}
#region Images
private Image makeImage(int percent) {
if (percent <= 8) {
return warningStateImage();
}
updateInfoTimer.Interval = UPDATE_INFO_INTERVAL;
if (Battery.isBatteryPowerOn()) {
return powerStateImage();
}
return normalStateImage(percent);
}
private Image powerStateImage() {
imageLabel.BackColor = Color.Black;
return ImageLoader.getImage(ImageLoader.ImageName.Power);
}
private Image normalStateImage(int percent) {
imageLabel.BackColor = Color.DarkGreen;
Bitmap bmp = new Bitmap(selectImage(percent));
int y = (100 - percent) * bmp.Height / 100;
Rectangle destRect = new Rectangle(0, 0, bmp.Width, y);
Graphics g = Graphics.FromImage(bmp);
g.DrawImageUnscaledAndClipped(grayImage, destRect);
g.Dispose();
return bmp;
}
private Image warningStateImage() {
updateInfoTimer.Interval = 800;
if (imageLabel.Image.Equals(grayImage)) {
imageLabel.BackColor = Color.DarkGreen;
return ImageLoader.getImage(ImageLoader.ImageName.Red);
}
imageLabel.BackColor = Color.Red;
return grayImage;
}
private Image selectImage(int percent) {
if (percent < 30)
return ImageLoader.getImage(ImageLoader.ImageName.Red);
else if (percent < 70)
return ImageLoader.getImage(ImageLoader.ImageName.Yellow);
return ImageLoader.getImage(ImageLoader.ImageName.Green);
}
#endregion
}
}
}