粘貼箱: OmegaChart Fibonacciリトレースメント Util.cs 追加修正ポイントには//☆Fibonacci

格式
Plain text
Post date
2017-12-27 19:58
Publication Period
Unlimited
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using System.Web;
  6. using System.Drawing;
  7. using System.Net;
  8. using System.Diagnostics;
  9. using System.Text;
  10. using SevenZip;
  11. using Travis.Storage;
  12. using Travis.Util;
  13. using Travis.Http;
  14. using Zanetti.UI;
  15. using Zanetti.Data;
  16. namespace Zanetti
  17. {
  18. internal enum PrimitiveIndicator
  19. {
  20. Date,
  21. Open,
  22. High,
  23. Low,
  24. Close,
  25. Volume,
  26. CreditLong,
  27. CreditShort,
  28. LAST
  29. }
  30. [EnumDesc(typeof(ChartFormat))]
  31. internal enum ChartFormat
  32. {
  33. [EnumValue(Description = "日足")] Daily,
  34. [EnumValue(Description = "週足")] Weekly,
  35. [EnumValue(Description = "月足")] Monthly,
  36. [EnumValue(Description = "年足")] Yearly
  37. #if DOJIMA
  38. , [EnumValue(Description = "半日足")] HalfDaily
  39. #endif
  40. }
  41. internal enum FormatModifier
  42. {
  43. Nop,
  44. Mul100,
  45. Percent
  46. }
  47. internal enum CompanyInfoSite
  48. {
  49. Yahoo,
  50. Infoseek,
  51. Nikkei,
  52. Livedoor
  53. }
  54. /// <summary>
  55. /// Util の概要の説明です。
  56. /// </summary>
  57. internal class Util
  58. {
  59. //ユーザ定義銘柄のコード最小値
  60. public const int CustomBrandMinValue = 400;
  61. public static void ReportCriticalError(Exception ex)
  62. {
  63. SilentReportCriticalError(ex);
  64. //メッセージボックスで通知
  65. string dir = Env.GetAppDir();
  66. MessageBox.Show(String.Format("内部エラーが発生しました。" + dir + "error.logファイルにエラー位置が記録されました。\n{0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  67. }
  68. public static void SilentReportCriticalError(Exception ex)
  69. {
  70. Debug.WriteLine(ex.Message);
  71. Debug.WriteLine(ex.StackTrace);
  72. //エラーファイルに追記
  73. string dir = Env.GetAppDir();
  74. StreamWriter sw = new StreamWriter(dir + "error.log", true);
  75. sw.WriteLine(DateTime.Now.ToString() + " : " + ex.Message);
  76. sw.WriteLine(ex.StackTrace);
  77. sw.Close();
  78. }
  79. public static MemoryStream HttpDownload(string url)
  80. {
  81. /* 2005/2/12より、ケンミレはクッキーを要求するようになった。WinHTTP経由だとこれに対応できない。
  82. * スクリプトによるプロキシ設定はあきらめざるをえないが仕方ないか。
  83. if(HTTPConnection.IsWinHTTPAvailable && Env.Options.ProxyConfig.UseIESetting) {
  84. return new HTTPConnection(url).Open();
  85. }
  86. */
  87. HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);
  88. //rq.CookieContainer = _cookieContainer;
  89. rq.ProtocolVersion = new Version(1, 1);
  90. rq.Method = "GET";
  91. if (!Env.Options.ProxyConfig.UseIESetting)
  92. rq.Proxy = new WebProxy(Env.Options.ProxyConfig.Address, Env.Options.ProxyConfig.Port);
  93. HttpWebResponse rs = (HttpWebResponse)rq.GetResponse();
  94. //効率はわるいがWinHTTPが使えない環境用なのでまあいいだろう
  95. MemoryStream strm = new MemoryStream(0x18000);
  96. CopyStream(rs.GetResponseStream(), strm);
  97. strm.Close();
  98. return new MemoryStream(strm.ToArray());
  99. }
  100. public static Stream ExtractData(string url)
  101. {
  102. var execdir = Path.GetDirectoryName(Application.ExecutablePath) ?? "";
  103. SevenZipBase.SetLibraryPath(Path.Combine(execdir, IntPtr.Size == 4 ? "7z.dll" : "7z64.dll"));
  104. var tmp = Path.Combine(execdir, Path.GetFileName(url));
  105. var result = new MemoryStream();
  106. using (var ms = Util.HttpDownload(url))
  107. using (var file = File.Create(tmp))
  108. {
  109. ms.WriteTo(file);
  110. using (var extractor = new SevenZipExtractor(file))
  111. {
  112. if (extractor.ArchiveFileData.Count != 1)
  113. throw new ApplicationException("データファイルの展開に失敗しました。");
  114. extractor.ExtractFile(0, result);
  115. }
  116. }
  117. File.Delete(tmp);
  118. result.Seek(0, SeekOrigin.Begin);
  119. return result;
  120. }
  121. public static void CopyStream(Stream input, Stream output)
  122. {
  123. byte[] buffer = new byte[0x20000];
  124. int n = input.Read(buffer, 0, buffer.Length);
  125. while (n > 0)
  126. {
  127. output.Write(buffer, 0, n);
  128. n = input.Read(buffer, 0, buffer.Length);
  129. }
  130. }
  131. public static void StreamToFile(Stream input, string filename)
  132. {
  133. using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
  134. CopyStream(input, fs);
  135. }
  136. //COLORREFに対応した整数を返す
  137. public static uint ToCOLORREF(Color c)
  138. {
  139. uint t = (uint)c.ToArgb();
  140. //COLORREFは0x00BBGGRR、ToArgbは0x00RRGGBB
  141. uint r = (t & 0x00FF0000) >> 16;
  142. uint b = (t & 0x000000FF) << 16;
  143. t &= 0x0000FF00;
  144. return t | r | b;
  145. }
  146. public static Color ToLightColor(Color c)
  147. {
  148. return Color.FromArgb((255 + c.R) / 2, (255 + c.G) / 2, (255 + c.B) / 2);
  149. }
  150. public static Color ToDarkColor(Color c)
  151. {
  152. return Color.FromArgb(c.R / 2, c.G / 2, c.B / 2);
  153. }
  154. //c1*v + c2*(1-v)
  155. public static Color MergeColor(Color c1, Color c2, double v)
  156. {
  157. double r1 = (double)c1.R;
  158. double g1 = (double)c1.G;
  159. double b1 = (double)c1.B;
  160. double r2 = (double)c2.R;
  161. double g2 = (double)c2.G;
  162. double b2 = (double)c2.B;
  163. return Color.FromArgb((int)(r1 * v + r2 * (1 - v)), (int)(g1 * v + g2 * (1 - v)), (int)(b1 * v + b2 * (1 - v)));
  164. }
  165. public static string FormatColor(Color col)
  166. {
  167. if (col.IsNamedColor)
  168. return col.Name;
  169. else
  170. return "#" + col.Name;
  171. }
  172. public static Color ParseColor(string src, Color def)
  173. {
  174. if (src.StartsWith("#"))
  175. {
  176. try
  177. {
  178. return Color.FromArgb(Int32.Parse(src.Substring(1), System.Globalization.NumberStyles.HexNumber));
  179. }
  180. catch (Exception)
  181. {
  182. return def;
  183. }
  184. }
  185. else
  186. {
  187. Color c = Color.FromName(src);
  188. return c;
  189. }
  190. }
  191. public static string GetDailyDataFileName(int code)
  192. {
  193. if (code < 1000)
  194. return Env.GetAppDir() + "data\\0" + code;
  195. else
  196. return Env.GetAppDir() + "data\\" + code;
  197. }
  198. public static bool IsDailyBased(ChartFormat fmt)
  199. {
  200. #if DOJIMA
  201. return fmt==ChartFormat.Daily || fmt==ChartFormat.HalfDaily;
  202. #else
  203. return fmt == ChartFormat.Daily;
  204. #endif
  205. }
  206. private static string[] _dayOfWeek = { "日", "月", "火", "水", "木", "金", "土" };
  207. //内部形式の日付のフォーマット
  208. public static string FormatFullDate(int date)
  209. {
  210. DateTime d = new DateTime(date / 10000, (date % 10000) / 100, (date % 100));
  211. return String.Format("{0}年{1:d2}月{2:d2}日({3})", d.Year, d.Month, d.Day, _dayOfWeek[(int)d.DayOfWeek]);
  212. }
  213. public static string FormatFullDate(int date, ChartFormat format)
  214. {
  215. DateTime d = new DateTime(date / 10000, (date % 10000) / 100, (date % 100));
  216. if (IsDailyBased(format))
  217. return String.Format("{0}年{1:d2}月{2:d2}日({3})", d.Year, d.Month, d.Day, _dayOfWeek[(int)d.DayOfWeek]);
  218. else if (format == ChartFormat.Weekly)
  219. {
  220. d = d.AddDays(5); //金曜日の位置で決めるのが自然
  221. return String.Format("{0}年{1:d2}月{2}週", d.Year, d.Month, (d.Day - 1) / 7 + 1);
  222. }
  223. else if (format == ChartFormat.Yearly)
  224. {
  225. return String.Format("{0}年", d.Year);
  226. }
  227. else // Monthly
  228. return String.Format("{0}年{1:d2}月", d.Year, d.Month);
  229. }
  230. public static string FormatShortDate(int date)
  231. {
  232. DateTime d = new DateTime(date / 10000, (date % 10000) / 100, (date % 100));
  233. return String.Format("{0}/{1:d2}/{2:d2}", d.Year, d.Month, d.Day);
  234. }
  235. public static DayOfWeek GetDayOfWeek(int date)
  236. {
  237. DateTime d = new DateTime(date / 10000, (date % 10000) / 100, (date % 100));
  238. return d.DayOfWeek;
  239. }
  240. //len文字の右詰文字
  241. public static string FormatFixedLenValue(double value, int len, string format, FormatModifier mod)
  242. {
  243. if (Double.IsNaN(value))
  244. return new string(' ', len - 1) + '-';
  245. else
  246. {
  247. if (mod == FormatModifier.Mul100 || mod == FormatModifier.Percent) value *= 100;
  248. string t = Math.Abs(value) < 100000 ? value.ToString(format) : value.ToString("F0"); //あまりでかい数に小数点つけても仕方ない
  249. if (mod == FormatModifier.Percent) t += "%";
  250. if (t.Length < len) t = new string(' ', len - t.Length) + t;
  251. return t;
  252. }
  253. }
  254. //一般的な数値フォーマット
  255. public static string FormatValue(double value, string fs, FormatModifier mod)
  256. {
  257. switch (mod)
  258. {
  259. case FormatModifier.Nop:
  260. return value.ToString(fs);
  261. case FormatModifier.Mul100:
  262. return (value * 100).ToString(fs);
  263. case FormatModifier.Percent:
  264. return (value * 100).ToString(fs) + "%";
  265. default:
  266. return "format error";
  267. }
  268. }
  269. //データが利用可能と思われる最終日付を取得
  270. public static DateTime GuessLatestTradeDate()
  271. {
  272. DateTime dt = DateTime.UtcNow.AddHours(9); //日本時間を常に取得
  273. return GuessLatestTradeDate(dt);
  274. }
  275. //last以前で最後の日付を取得
  276. public static DateTime GuessLatestTradeDate(DateTime last)
  277. {
  278. while (!IsMarketOpenDate(last)) last = last.AddDays(-1);
  279. return new DateTime(last.Year, last.Month, last.Day, 0, 0, 0);
  280. }
  281. public static int DateToInt(DateTime dt)
  282. {
  283. return dt.Year * 10000 + dt.Month * 100 + dt.Day;
  284. }
  285. public static int DateToInt(int year, int month, int day)
  286. {
  287. return year * 10000 + month * 100 + day;
  288. }
  289. public static DateTime IntToDate(int dt)
  290. {
  291. return new DateTime(dt / 10000, (dt % 10000) / 100, (dt % 100));
  292. }
  293. /// <summary>
  294. /// 指定された日付に市場が開いているかを返す。
  295. /// </summary>
  296. /// <param name="d">日付を指定する。</param>
  297. /// <returns></returns>
  298. public static bool IsMarketOpenDate(DateTime d)
  299. {
  300. // 土日
  301. if (d.DayOfWeek == DayOfWeek.Saturday ||
  302. d.DayOfWeek == DayOfWeek.Sunday)
  303. return false;
  304. // 年末年始
  305. if ((d.Month == 12 && d.Day == 31) ||
  306. (d.Month == 1 && d.Day <= 3))
  307. return false;
  308. return !IsHoliday(d);
  309. }
  310. static bool IsHoliday(DateTime d)
  311. {
  312. // 動かない休日
  313. if ((d.Month == 1 && d.Day == 1) ||
  314. (d.Month == 2 && d.Day == 11) ||
  315. (d.Month == 4 && d.Day == 29) ||
  316. (d.Month == 5 && (d.Day >= 3 && d.Day <= 5)) ||
  317. (d.Month == 11 && (d.Day == 3 || d.Day == 23)) ||
  318. (d.Month == 12 && d.Day == 23))
  319. return true;
  320. if (d.Year >= 2016 && d.Month == 8 && d.Day == 11) // 山の日
  321. return true;
  322. // 春分と秋分(1980~2099年に対応)
  323. if (d.Month == 3 &&
  324. d.Day == (int)(20.8431 + 0.242194 * (d.Year - 1980) - (d.Year - 1980) / 4))
  325. return true;
  326. if (d.Month == 9 &&
  327. d.Day == (int)(23.2488 + 0.242194 * (d.Year - 1980) - (d.Year - 1980) / 4))
  328. return true;
  329. // ハッピーマンデー
  330. if (d.Year < 2000)
  331. {
  332. if ((d.Month == 1 && d.Day == 15) ||
  333. (d.Month == 10 && d.Day == 10))
  334. return true;
  335. }
  336. else
  337. {
  338. if ((d.Month == 1 || d.Month == 10) &&
  339. (d.Day >= 8 && d.Day <= 14) && d.DayOfWeek == DayOfWeek.Monday)
  340. return true;
  341. }
  342. if (d.Year < 2003)
  343. {
  344. if ((d.Month == 7 && d.Day == 20) ||
  345. (d.Month == 9 && d.Day == 15))
  346. return true;
  347. }
  348. else
  349. {
  350. if ((d.Month == 7 || d.Month == 9) &&
  351. (d.Day >= 15 && d.Day <= 21) && d.DayOfWeek == DayOfWeek.Monday)
  352. return true;
  353. // シルバーウィーク
  354. if (d.Month == 9 && (d.Day == 21 || d.Day == 22) && d.DayOfWeek == DayOfWeek.Tuesday)
  355. return IsHoliday(d.AddDays(1));
  356. }
  357. // 5月4日が国民の祝日になったので、振替休日が二日以上繰り越す。
  358. if (d.Year > 2007 && d.Month == 5 && d.Day == 6)
  359. return d.DayOfWeek >= DayOfWeek.Monday &&
  360. d.DayOfWeek <= DayOfWeek.Wednesday;
  361. // 振り替え休日
  362. if (d.DayOfWeek == DayOfWeek.Monday)
  363. return IsHoliday(d.AddDays(-1));
  364. return false;
  365. }
  366. public static double Yobine(MarketType mt, double v)
  367. {
  368. //呼値を計算
  369. double r;
  370. if (mt == MarketType.B)
  371. return 1;
  372. /* 旧JASDAQ版
  373. if(v <= 1000)
  374. r = 1;
  375. else if(v < 10000)
  376. r = 10;
  377. else if(v < 100000)
  378. r = 100;
  379. else if(v < 1000000)
  380. r = 1000;
  381. else if(v < 10000000)
  382. r = 10000;
  383. else
  384. r = 50000;
  385. }
  386. else {
  387. */
  388. if (v <= 2000)
  389. r = 1;
  390. else if (v <= 3000)
  391. r = 5;
  392. else if (v <= 30000)
  393. r = 10;
  394. else if (v <= 50000)
  395. r = 50;
  396. else if (v <= 100000)
  397. r = 100;
  398. else if (v <= 1000000)
  399. r = 1000;
  400. else if (v <= 20000000)
  401. r = 10000;
  402. else if (v <= 30000000)
  403. r = 50000;
  404. else
  405. r = 100000;
  406. return r;
  407. }
  408. //呼値の整数倍になるように丸める
  409. public static double RoundToYobine(MarketType mt, double v)
  410. {
  411. double y = Yobine(mt, v);
  412. if (y == 1) return v; //ショートカット
  413. double n = Math.Round(v / y);
  414. return n * y;
  415. }
  416. public static void Warning(IWin32Window parent, string msg)
  417. {
  418. MessageBox.Show(parent, msg, Env.Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  419. }
  420. public static void Warning(string msg)
  421. {
  422. MessageBox.Show(msg, Env.Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  423. }
  424. public static void Information(IWin32Window parent, string msg)
  425. {
  426. MessageBox.Show(parent, msg, Env.Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
  427. }
  428. public static DialogResult AskUserYesNo(IWin32Window parent, string msg)
  429. {
  430. return MessageBox.Show(parent, msg, Env.Constants.AppTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  431. }
  432. public static void AddMenuBar(Menu parent)
  433. {
  434. ZMenuItem mi = new ZMenuItem();
  435. mi.Text = "-";
  436. mi.Index = parent.MenuItems.Count;
  437. parent.MenuItems.Add(mi);
  438. }
  439. public static void AddMenuItem(Menu parent, string text, EventHandler handler)
  440. {
  441. ZMenuItem mi = new ZMenuItem();
  442. mi.Text = text;
  443. mi.Index = parent.MenuItems.Count;
  444. mi.Click += handler;
  445. parent.MenuItems.Add(mi);
  446. }
  447. public static double ParseDouble(string value, double def)
  448. {
  449. try
  450. {
  451. if (value == null || value.Length == 0)
  452. return def;
  453. else
  454. return Double.Parse(value);
  455. }
  456. catch (Exception)
  457. {
  458. return def;
  459. }
  460. }
  461. public static double[] ParseDoubles(string value)
  462. {
  463. string[] t = value.Split(',');
  464. double[] r = new double[t.Length];
  465. for (int i = 0; i < t.Length; i++)
  466. r[i] = Double.Parse(t[i]);
  467. return r;
  468. }
  469. public static int ParseInt(string value)
  470. {
  471. try
  472. {
  473. return Int32.Parse(value);
  474. }
  475. catch (Exception)
  476. {
  477. Debug.WriteLine("Int32 parse error " + value);
  478. throw;
  479. }
  480. }
  481. public static int ParseInt(string value, int def)
  482. {
  483. try
  484. {
  485. if (value == null || value.Length == 0)
  486. return def;
  487. else
  488. return Int32.Parse(value);
  489. }
  490. catch (Exception)
  491. {
  492. Debug.WriteLine("Int32 parse error " + value);
  493. return def;
  494. }
  495. }
  496. public static int ParseHexInt(string value, int def)
  497. {
  498. try
  499. {
  500. if (value == null || value.Length == 0)
  501. return def;
  502. else
  503. return Int32.Parse(value, System.Globalization.NumberStyles.HexNumber); //先頭に0xがついていてはだめ
  504. }
  505. catch (Exception)
  506. {
  507. Debug.WriteLine("Int32 parse error " + value);
  508. return def;
  509. }
  510. }
  511. public static float ParseFloat(string value, float def)
  512. {
  513. try
  514. {
  515. if (value == null || value.Length == 0)
  516. return def;
  517. else
  518. return Single.Parse(value);
  519. }
  520. catch (Exception)
  521. {
  522. return def;
  523. }
  524. }
  525. public static bool ParseBool(string value)
  526. {
  527. return value == "True";
  528. }
  529. public static bool ParseBool(string value, bool def)
  530. {
  531. if (value == "True" || value == "true")
  532. return true;
  533. else if (value == "False" || value == "false")
  534. return false;
  535. else
  536. return def;
  537. }
  538. public static HeightConfig ParseHeightConfig(string value, HeightConfig def)
  539. {
  540. try
  541. {
  542. if (value == null || value.Length == 0) return def; //shortcut
  543. return (HeightConfig)Enum.Parse(typeof(HeightConfig), value);
  544. }
  545. catch (Exception)
  546. {
  547. return def;
  548. }
  549. }
  550. public static int IndexOf(object[] values, object value)
  551. {
  552. for (int i = 0; i < values.Length; i++)
  553. if (values[i] == value) return i;
  554. return -1;
  555. }
  556. public static string LoadMandatoryAttr(StorageNode node, string name)
  557. {
  558. string r = node[name];
  559. if (r == null || r.Length == 0)
  560. throw new FormatException(String.Format("{0}に必須アトリビュート{1}がないか、空です。", node.Name, name));
  561. return r;
  562. }
  563. public static string LoadMandatoryText(StorageNode node, string name)
  564. {
  565. StorageNode r = node.FindChildNode(name);
  566. if (r == null)
  567. throw new FormatException(String.Format("{0}に必須要素{1}がありません。", node.Name, name));
  568. else if (r.TextValue == null)
  569. throw new FormatException(String.Format("{0}の必須要素{1}が空です。", node.Name, name));
  570. return r.TextValue;
  571. }
  572. public static string LoadOptionalText(StorageNode node, string name, string def)
  573. {
  574. StorageNode r = node.FindChildNode(name);
  575. if (r == null)
  576. return def;
  577. else if (r.TextValue == null)
  578. return def;
  579. else
  580. return r.TextValue;
  581. }
  582. public static int SafeArgLength(Array a)
  583. {
  584. return a == null ? 0 : a.Length;
  585. }
  586. public static Keys ParseKey(string s)
  587. {
  588. if (s.Length == 0)
  589. return Keys.None;
  590. /*
  591. !!もしここの処理が遅ければ自前で書きなおせる
  592. else if(s.Length==1) {
  593. char ch = s[0];
  594. if('0'<=ch && ch<='9')
  595. return Keys.D0 + (ch - '0');
  596. }
  597. */
  598. return (Keys)Enum.Parse(typeof(Keys), s);
  599. }
  600. public static string FormatShortcut(Keys key)
  601. {
  602. if (key == Keys.None) return "";
  603. Keys modifiers = key & Keys.Modifiers;
  604. StringBuilder b = new StringBuilder();
  605. if ((modifiers & Keys.Control) != Keys.None)
  606. {
  607. b.Append("Ctrl");
  608. }
  609. if ((modifiers & Keys.Shift) != Keys.None)
  610. {
  611. if (b.Length > 0) b.Append('+');
  612. b.Append("Shift");
  613. }
  614. if ((modifiers & Keys.Alt) != Keys.None)
  615. {
  616. if (b.Length > 0) b.Append('+');
  617. b.Append("Alt");
  618. }
  619. if (b.Length > 0)
  620. b.Append('+');
  621. b.Append(KeyString(key & Keys.KeyCode));
  622. return b.ToString();
  623. }
  624. public static string KeyString(Keys key)
  625. {
  626. int ik = (int)key;
  627. if ((int)Keys.D0 <= ik && ik <= (int)Keys.D9)
  628. return new string((char)('0' + (ik - (int)Keys.D0)), 1);
  629. else
  630. {
  631. switch (key)
  632. {
  633. case Keys.None:
  634. return "";
  635. case Keys.Prior:
  636. return "PageUp";
  637. case Keys.Next:
  638. return "PageDown";
  639. //Oemほにゃららがうざったい
  640. case Keys.OemBackslash:
  641. return "Backslash";
  642. case Keys.OemCloseBrackets:
  643. return "CloseBrackets";
  644. case Keys.Oemcomma:
  645. return "Comma";
  646. case Keys.OemMinus:
  647. return "Minus";
  648. case Keys.OemOpenBrackets:
  649. return "OpenBrackets";
  650. case Keys.OemPeriod:
  651. return "Period";
  652. case Keys.OemPipe:
  653. return "Pipe";
  654. case Keys.Oemplus:
  655. return "Plus";
  656. case Keys.OemQuestion:
  657. return "Question";
  658. case Keys.OemQuotes:
  659. return "Quotes";
  660. case Keys.OemSemicolon:
  661. return "Semicolon";
  662. case Keys.Oemtilde:
  663. return "Tilde";
  664. default:
  665. return key.ToString();
  666. }
  667. }
  668. }
  669. public static void Swap(ref int x, ref int y)
  670. {
  671. int t = x;
  672. x = y;
  673. y = t;
  674. }
  675. }
  676. internal abstract class Trans
  677. {
  678. protected double _a;
  679. protected double _b;
  680. public Trans(double a, double b)
  681. {
  682. _a = a;
  683. _b = b;
  684. }
  685. public double A
  686. {
  687. get
  688. {
  689. return _a;
  690. }
  691. }
  692. public double B
  693. {
  694. get
  695. {
  696. return _b;
  697. }
  698. }
  699. public static Trans Solve(double x1, double y1, double x2, double y2, bool logscale, bool inverseupdown)
  700. {
  701. if (logscale)
  702. {
  703. if (inverseupdown)
  704. {
  705. return LogTrans.SolveUpsideDown(x1, y1, x2, y2);
  706. }
  707. else
  708. {
  709. return LogTrans.Solve(x1, y1, x2, y2);
  710. }
  711. }
  712. else
  713. {
  714. if (inverseupdown)
  715. {
  716. return LinearTrans.SolveUpsideDown(x1, y1, x2, y2);
  717. }
  718. else
  719. {
  720. return LinearTrans.Solve(x1, y1, x2, y2);
  721. }
  722. }
  723. }
  724. public abstract double TransValue(double x);
  725. public abstract double Inverse(double y);
  726. public abstract double Inverse(float y);//☆Fibonacci
  727. }
  728. //y = ax + b の変換をする
  729. internal class LinearTrans : Trans
  730. {
  731. public LinearTrans(double a, double b) : base(a, b)
  732. {
  733. }
  734. public override double TransValue(double x)
  735. {
  736. return _a * x + _b;
  737. }
  738. public override double Inverse(double y)
  739. {
  740. return (y - _b) / _a;
  741. }
  742. public override double Inverse(float y)//☆Fibonacci
  743. {
  744. return (y - _b) / _a;
  745. }
  746. //0除算や精度はあまり気にせず連立1次方程式を解く
  747. public static LinearTrans Solve(double x1, double y1, double x2, double y2)
  748. {
  749. double a = (y1 - y2) / (x1 - x2);
  750. return new LinearTrans(a, y1 - a * x1);
  751. }
  752. public static LinearTrans SolveUpsideDown(double x1, double y1, double x2, double y2)
  753. {
  754. double a = -(y1 - y2) / (x1 - x2);
  755. return new LinearTrans(a, y1 - a * x2);
  756. }
  757. }
  758. internal class LogTrans : Trans
  759. {
  760. public LogTrans(double a, double b) : base(a, b)
  761. {
  762. }
  763. public override double TransValue(double x)
  764. {
  765. // ボリバンなどがはみ出てもトリミング
  766. if (x < 1)
  767. x = 1;
  768. return _a * Math.Log10(x) + _b;
  769. }
  770. public override double Inverse(double y)
  771. {
  772. return Math.Pow(10, (y - _b) / _a);
  773. }
  774. public override double Inverse(float y)//☆Fibonacci
  775. {
  776. return Math.Pow(10, (y - _b) / _a);
  777. }
  778. public static LogTrans Solve(double x1, double y1, double x2, double y2)
  779. {
  780. // 負の部分はトリミング
  781. if (x2 < 1)
  782. {
  783. y2 = LinearTrans.Solve(x1, y1, x2, y2).TransValue(1);
  784. x2 = 1;
  785. }
  786. double a = (y1 - y2) / Math.Log10(x1 / x2);
  787. double b = y1 - a * Math.Log10(x1);
  788. return new LogTrans(a, b);
  789. }
  790. public static LogTrans SolveUpsideDown(double x1, double y1, double x2, double y2)
  791. {
  792. // 負の部分はトリミング
  793. if (x2 < 1)
  794. {
  795. y2 = LinearTrans.Solve(x1, y1, x2, y2).TransValue(1);
  796. x2 = 1;
  797. }
  798. double a = -(y1 - y2) / Math.Log10(x1 / x2);
  799. double b = y1 - a * Math.Log10(x2);
  800. return new LogTrans(a, b);
  801. }
  802. }
  803. //時間のかかる動作のときに途中経過を知らせるための定数 SendMessageを使う
  804. internal class AsyncConst
  805. {
  806. public const int WM_ASYNCPROCESS = Win32.WM_USER + 1;
  807. public const int LPARAM_PROGRESS_SUCCESSFUL = 1;
  808. public const int LPARAM_PROGRESS_FAILURE = 2;
  809. public const int LPARAM_FINISHED = 3;
  810. public const int LPARAM_ERROR = 4;
  811. }
  812. //Look & Feelを変更するための仕組み
  813. internal class ThemeUtil
  814. {
  815. public enum Theme
  816. {
  817. Unspecified,
  818. Luna
  819. }
  820. private static Theme _theme;
  821. public static Theme CurrentTheme
  822. {
  823. get
  824. {
  825. return _theme;
  826. }
  827. }
  828. [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
  829. private static extern int GetCurrentThemeName(char[] filename, int filenamelen, char[] colorbuff, int colornamelen, char[] sizebuff, int sizebufflen);
  830. private static void SpecifyThemeUnderWinXP()
  831. {
  832. try
  833. {
  834. char[] fn = new char[256];
  835. char[] cb = new char[256];
  836. char[] sz = new char[256];
  837. int r = GetCurrentThemeName(fn, 256, cb, 256, sz, 256);
  838. if (r == 0)
  839. {
  840. string theme_name = new string(fn);
  841. if (theme_name.IndexOf("Luna") != -1)
  842. _theme = Theme.Luna;
  843. }
  844. //Debug.WriteLine(String.Format("FN={0} Color={1} Size={2}", new string(fn), new string(cb), new string(sz)));
  845. }
  846. catch (Exception)
  847. {
  848. }
  849. }
  850. public static void Init()
  851. {
  852. _theme = Theme.Unspecified;
  853. OperatingSystem os = System.Environment.OSVersion;
  854. if (os.Platform == PlatformID.Win32NT && os.Version.CompareTo(new Version(5, 1)) >= 0)
  855. SpecifyThemeUnderWinXP();
  856. }
  857. public static Color TabPaneBackColor
  858. {
  859. get
  860. {
  861. if (_theme == Theme.Luna)
  862. return Color.FromKnownColor(KnownColor.ControlLightLight);
  863. else
  864. return Color.FromKnownColor(KnownColor.Control);
  865. }
  866. }
  867. }
  868. }
下載 Printable view

網址

Embed with JavaScript

Embed with iframe

Raw text