1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C++で、設定した日付を通知する機能を作ってみる第一講

Posted at

         役に立たずとも

開発経緯

 学校の課題提出が近づくごとに思う「提出まであと何日だ?」。
windows内のアプリで設定ができる。
 しかし個人的には、PCが起動したタイミングで通知が欲しいのだ。

必要な機能

 まず機能の洗い出し。
 1. 日付の設定・記録
 2. パソコンの起動確認
 3. 通知を行う
今回は2および3の機能への挑戦になる。

開発

 開発環境
Visual Studio Community 2022 ver.17.7.5
windows10 Home

 開発言語
C
C++?(CとC++の違いがまだよくわからない)

現在の日付から目的の日付までの日数計算

 現在の日付の確保を行うためには[time.h]をincludeをする。
コードがこちら

time.cpp
#include <iostream>
#include <conio.h>
#include <time.h>

using namespace std;

int main ()
{
  //変数宣言
  int CurrentMonth = 0;  //現在の月
  int CurrentDay = 0;  //現在の日
  
  //現在の日付の取得
  CurrentMonth = tm_mon;  //月の取得
  CurrentDay = tm_mday;  //日の取得

 cout << CurrentMonth << "月" << CurrentDay << "日";

_getch ();
return 0;
}

見事にエラー。
CurrentMonth = tm_mon; //月の取得
CurrentDay = tm_mday; //日の取得

この部分が定義されていないとのこと。
調べてみるとローカルタイム(どこの時間か)を設定していないからっぽい。
のでこれより前の分に
 time_t t = time(NULL);

 struct tm *local = localtime(&t);
これを追加したうえで前に
 local->
を追加すると、

またエラー。
エラー内容は

C4996 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

初めて見るエラーだぞ。
雑翻訳すると
「'localtime'←こいつ安全じゃねえぞ!'localtime_s'←こっちを使っとけ!」
とのこと。
安全じゃないってどういう…?

調べてみると何かvisial studioだと使えないっぽい?
仕方がないので従うとしよう。
変更後のプログラムがこちら
※長くなるので必要のないものを消しています

time.cpp
int main()
{
	//変数宣言
	int CurrentMonth = 0;  //現在の月
	int CurrentDay = 0;  //現在の日

	time_t t = time(NULL);

	struct tm local;
	errno_t err = localtime_s(&local, &t);

	CurrentMonth = local.tm_tm_mon + 1;
	CurrentDay = local.tm_mday;
}

結果…

1月20日

行った!!!!

とりあえず日付の取得はできた。
 お次は目的の日付を入力して記録を行う。
ここは授業でやっていたことの応用。

time.cpp
int main()
{
   //変数宣言
   //日付関係
   int CurrentMonth = 0;  //現在の月
   int CurrentDay = 0;  //現在の日

   int ObjectiveMonth = 0;  //目的の月
   int ObjectiveDay = 0;  //目的の日

   time_t t = time(NULL);  //ローカルタイムの初期化?

   struct tm local;
   errno_t err = localtime_s(&local, &t);

   //ファイル関係
   ifstream inputFile;

   inputFile.open("Days.txt", ios::in);  //ファイルオープン

   if (inputFile.fail())
   {
   	cout << "ファイルオープン失敗";
   }
   else
   {
   	//現在の日付の所得
   	CurrentMonth = local.tm_mon + 1;  //現在の月
   	CurrentDay = local.tm_mday;  //現在の日

   	//目的の日付の入力
   	inputFile >> ObjectiveMonth;
   	inputFile >> ObjectiveDay;

   	cout << "現在の日付:" << CurrentMonth << "月" << CurrentDay << "日" << endl;
   	cout << "目的の日付:" << ObjectiveMonth << "月" << ObjectiveDay << "日" << endl;
   }

   _getch();
   return 0;
}
Days.txt
2
1

結果

現在の日付:1月20日
目的の日付:2月1日

まあ楽勝ですよ。

ちなみにここまで一時間半かかってます。
あれ?思っているよりも時間が掛かった?
次行くぞい

パソコンの起動を確認する

 多分今までのは序章的な感じ。
序章に時間かけすぎな気がするが。

 調べてみるとレジストリやらなんやらって話が出てきて思っているよりもこれは難しいのでは?
思い付きでやってはダメなものでは?

って考えたがプログラムで書くよりもスタートアップに登録すれば簡単に行くことに気が付いた。
今回はこちらを採用。
次は触ってやる。

1. まず使うプログラムのexeの場所を開く。
2. exeのショートカットを作る。
3. ウィンドウズキーとRキーを押す。
4. shell:startup と書き、実行する。
5. 開いた場所にショートカットをコピーする。

完成!
思っているよりも簡単だった…。

再起動して確認したがうまくいった。

次に向けて

 とりあえず最低限のことはできた。
次に立ちはだかるは直接通知を行うことをしなければならない。
SDK関係に足を踏み入れなければならない感じで時間が掛かってしまうのでいったん終了。

初めての投稿になるため、あまりうまくまとめられなかった場面が多くあった。
プログラムが汚いやもっとこうした方がいいがあったらコメントくださるとうれしいです

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?