65.9K
CodeProject 正在变化。 阅读更多。
Home

Pocket Fuzzy Quotient Calculator

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.95/5 (21投票s)

2004年4月24日

CPOL

2分钟阅读

viewsIcon

50703

downloadIcon

467

袖珍模糊商计算器是一个 .NET Compact Framework 示例应用程序,它根据你的心情计算你的 FQ。

Sample Image - FuzzyQuotient.gif

引言

袖珍模糊商计算器是一个 .NET Compact Framework 示例应用程序,允许你根据一天中的心情输入你对各种主题的看法。然后它会计算你的总体 FQ,或模糊商。该程序灵感来自生物节律、幸运饼干和星座运势。

在此过程中,感兴趣的编码主题包括

  • 如何为 PocketPC 创建应用程序
  • 使用 XML 数据集配置和生成用户界面控件
  • 派生一个新的控件来显示颜色渐变

使用代码

该应用程序是使用 Visual Studio 2003 中的“文件”>“新建项目”>“智能设备应用程序”创建的。这为我们提供了主窗体和主菜单。模糊商主显示需要显示主题控件,这些控件可以滚动以包含任意数量的主题。为了实现这一点,创建了一个基于 Panel 的类,名为 AutoScrollPanel,并将其添加到主窗体中。这个类基本上是带有滚动条的两个嵌套面板。它的工作原理如下。

namespace FuzzyQuotient
{
    public class AutoScrollPanel : Panel
    {
        public Panel Contents
        {
            get { return contents; }
        }
        Panel contents;
        VScrollBar vScroll;

        public AutoScrollPanel()
        {
            // create a scroll bar
            this.vScroll = new VScrollBar();
            this.vScroll.Parent = this;
            this.vScroll.Visible = true;
            this.vScroll.Minimum = 0;
            this.vScroll.SmallChange = 20;
            this.vScroll.ValueChanged +=
                new EventHandler (this.scrollbar_ValueChanged);

            // create the contents panel that holds the controls to scroll
            this.contents = new Panel();
            this.contents.Parent = this;
            this.contents.Width =
                this.ClientSize.Width - this.vScroll.Size.Width;
        }
...

接下来,需要从 XML 文件加载主题配置数据,以便创建添加到滚动面板中的主题控件。为配置 XML 选择以下模式

<?xml version="1.0" encoding="utf-8" ?>
<Topics>
    <Topic>
        <Name>Mood</Name>
        <Values>
            <Value>
                <Score>1</Score>
                <Text>Sad</Text>
            </Value>
            <Value>
                <Score>10</Score>
                <Text>Happy</Text>
            </Value>
        </Values>
    </Topic>
    <Topic>
        <Name>Weather</Name>
        <Values>
            <Value>
                <Score>1</Score>
                <Text>Lousy</Text>
            </Value>
            <Value>
                <Score>10</Score>
                <Text>Really, Really Nice!</Text>
            </Value>
        </Values>
    </Topic>
</Topics>

然后将此 XML 读取到 DataSet 类中。“Topics”表被扫描,并根据数据构建控件行。每个主题的颜色范围需要显示为渐变,但不幸的是,Graphics 类在紧凑框架上并不直接支持渐变画笔。因此,我决定编写自己的 GradientControl,并使用自定义 OnPaint 消息来处理它。

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace FuzzyQuotient
{
    public class GradientControl
    : Control     { public
    Color
        Start = Color.Blue; public Color
        End = Color.Red; private const
        int STEPS = 20; public GradientControl()

                {
               
        }

        protected override void OnPaint (PaintEventArgs e)
        {
            Pen blackPen = new Pen (System.Drawing.Color.Black);

            // step through each of the color components
            int step = Width / STEPS;
            float startR = (float) Start.R;
            float stepR = ((float) End.R - (float) Start.R) / (float) STEPS;
            float startG = (float) Start.G;
            float stepG = ((float) End.G - (float) Start.G) / (float) STEPS;
            float startB = (float) Start.B;
            float stepB = ((float) End.B - (float) Start.B) / (float) STEPS;
            
            // draw each color band one at a time
            for (int i = 0; i < Width; i += step)
            {
                e.Graphics.FillRectangle(new SolidBrush(
                    Color.FromArgb((int)startR, (int)startG, (int)startB)),
                    i, 0, step, Height);
                if ((stepR > 0 && (startR < End.R)) ||
                    (stepR < 0 && (startR > End.R)))
                    startR += stepR;
                if ((stepG > 0 && (startG < End.G)) ||
                    (stepG < 0 && (startG > End.G)))
                    startG += stepG;
                if ((stepB > 0 && (startB < End.B)) ||
                    (stepB < 0 && (startB > End.B)))
                    startB += stepB;
            }

            //draw the border
            e.Graphics.DrawRectangle (blackPen, 0, 0,
                this.ClientRectangle.Width - 1,
                this.ClientRectangle.Height - 1);
        }
    }
}

就这样!

FQ 计算按钮现在只是取平均分,但你可以看到很容易将其进一步扩展并做一些有趣的事情。

未来的增强

  • 添加一个配置对话框来设置要跟踪的主题
  • 将分数数据保存到 XML 数据集
  • 按日期跟踪你的分数
  • 使用真实的模糊逻辑计算
  • 将输入的数据与桌面应用程序同步
  • 将结果发布到 Web 应用程序
  • 查看其他用户输入的分数,按:朋友、邮政编码、州、国家/地区分组
  • 绘制美国地图,显示每个州平均分数的颜色,并按每个主题进行动画显示
© . All rights reserved.