⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 C#股票行情查看。使用了Webservice技术
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace StockMarket
{
    public partial class frmMain : Form
    {
        Comm_Func func = new Comm_Func();
        frmHint fh = new frmHint();
        bool bHide = false;

        public frmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 与昨收盘相比,是涨还是跌
        /// </summary>
        /// <param name="strNow">昨收盘</param>
        /// <param name="strSource">待比较价格</param>
        /// <returns>返回相应颜色</returns>
        Color GetColor(string strNow, string strSource)
        {
            float _fNow = float.Parse(strNow);
            float _fSource = float.Parse(strSource);
            if (_fNow > _fSource)
                return func.PubColorDown;
            else if (_fNow == _fSource)
                return func.PubColorNormal;
            else
                return func.PubColorUp;
        }

        // 获取除以一万后的两位小数点值
        string GetWan(string strSource)
        {
            if (strSource == "") return "";
            float _fResult = float.Parse(strSource);
            _fResult = _fResult / 10000;
            return _fResult.ToString(".##");
        }

        //载入股票数据
        void GetInfo(string strCode)
        {
            string[] _ArrInfo = func.getStockInfoByCode(strCode); //获取信息数组
            Color _Color;

            if (_ArrInfo[3] != "")
            {
                _Color = GetColor(_ArrInfo[4], _ArrInfo[3]);

                gbInfo.Text = _ArrInfo[1] + "(" + _ArrInfo[0] + ") ";
                labNewPrice.Text = _ArrInfo[3];
                labUpDown.Text = _ArrInfo[6] + "(" + _ArrInfo[9] + ")";
                labNewPrice.ForeColor = labUpDown.ForeColor = _Color;

                labPreDay.Text = _ArrInfo[4];
                labTodayOpen.Text = _ArrInfo[5];
                //最低价
                labMinPrice.Text = _ArrInfo[7];
                _Color = GetColor(_ArrInfo[4], _ArrInfo[7]);
                labMinPrice.ForeColor = _Color;
                //最高价
                labMaxPrice.Text = _ArrInfo[8];
                _Color = GetColor(_ArrInfo[4], _ArrInfo[8]);
                labMaxPrice.ForeColor = _Color;
                //成交数据
                labBecome.Text = GetWan(_ArrInfo[10]) + "万手";
                labBecomePrice.Text = GetWan(_ArrInfo[11]) + "亿元";
            }

            pic.Load(func.getStockImageByteByCode(strCode));
        }

        //初始化
        private void frmMain_Load(object sender, EventArgs e)
        {
            func.initalService();
            GetInfo("SH000001");
            this.Visible = false;
            notifyIcon.Text = this.Text = "股票分析小工具";
        }

        //输入股票号
        private void txtCode_KeyUp(object sender, KeyEventArgs e)
        {
            txtCode.Text = txtCode.Text.Trim();
            if (txtCode.Text.Length >= 6)
            {
                GetInfo(txtCode.Text);
            }
        }

        //点击上证
        private void label3_Click(object sender, EventArgs e)
        {
            txtCode.Text = "sh000001";
            txtCode_KeyUp(sender, null);
        }

        //管理买入股票
        private void 管理买入股票MToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmManager fm = new frmManager();
            fm.Show();
        }

        //返回单个股票行情,与成本价对比
        string GetStockCodeBySelf(string strCode, float fCost, float fNum)
        {
            string strResult = "(" + strCode + ")";
            string[] _ArrInfo = func.getStockInfoByCode(strCode); //获取信息数组
            if (_ArrInfo[3] != "")
            {
                strResult += " 最新价:" + _ArrInfo[3] + " 涨跌:" + _ArrInfo[6] + "(" + _ArrInfo[9] + ")"
                    + " 成本价:" + fCost.ToString("0.##") + " 数量:" + fNum.ToString();
                float fCostSum = fCost * fNum;
                float fNowSum = float.Parse(_ArrInfo[3]) * fNum;
                float fUpDownSum = fNowSum - fCostSum;
                if (fUpDownSum < 0)
                    strResult += " 跌:" + fUpDownSum.ToString("0.##");
                else if (fUpDownSum > 0)
                    strResult += " 涨:" + fUpDownSum.ToString("0.##");
                else
                    strResult += " 维持成本";
            }
            return strResult;
        }

        //组合
        void DoHint()
        {
            if (File.Exists(func.GetStockFileName()))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(func.GetStockFileName());
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sb.Append(GetStockCodeBySelf(ds.Tables[0].Rows[i]["股票代码"].ToString(), float.Parse(ds.Tables[0].Rows[i]["成本价"].ToString()), float.Parse(ds.Tables[0].Rows[i]["购买数量"].ToString())));
                    sb.Append("\r\n");
                }
                fh.txtInfo.Text = sb.ToString();
                sb = null;
            }
        }

        //最小化
        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                bHide = true;
            }
        }

        void ShowHideWindow()
        {
            if (!fh.Visible)
            {
                DoHint();
                fh.Show();
            }
            else
            {
                fh.Hide();
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!fh.Visible)
                {
                    DoHint();
                    fh.Show();
                }
                else
                {
                    fh.Hide();
                }
            }
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 显示主窗体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bHide)
            {
                this.Show();
                bHide = false;
            }
            else
            {
                this.Hide();
                bHide = true;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -