Last active
February 28, 2026 01:29
-
-
Save bg1bgst333/c2302bf66a629b9a1c0da738fb09a137 to your computer and use it in GitHub Desktop.
CApplication::OnIdle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ヘッダのインクルード | |
| // 独自のヘッダ | |
| #include "Application.h" // CApplication | |
| // コンストラクタCApplication | |
| CApplication::CApplication() { | |
| // メンバの初期化 | |
| m_pMainWnd = NULL; // m_pMainWndをNULLで初期化. | |
| } | |
| // メッセージループ処理関数Run. | |
| int CApplication::Run() { | |
| // 構造体の宣言 | |
| MSG msg; // MSG型構造体msg. | |
| // メッセージループの処理 | |
| while (GetMessage(&msg, NULL, 0, 0) > 0) { // GetMessageでウィンドウメッセージを取得し, msgに格納.(0以下ならここを抜ける.) | |
| // メッセージの変換と送出. | |
| TranslateMessage(&msg); // TranslateMessageで仮想キーメッセージを文字メッセージに変換. | |
| DispatchMessage(&msg); // DispatchMessageでメッセージをウィンドウプロシージャWindowProcに送出. | |
| } | |
| // ExitInstanceの値を返す. | |
| return ExitInstance(); // returnでExitInstanceの値を返す. | |
| } | |
| // 終了処理関数ExitInstance. | |
| int CApplication::ExitInstance() { | |
| // メインウィンドウの破棄 | |
| if (m_pMainWnd != NULL) { // m_pMainWndがNULLでない時. | |
| // 破棄する. | |
| delete m_pMainWnd; // deleteでm_pMainWndを破棄. | |
| m_pMainWnd = NULL; // m_pMainWndにNULLをセット. | |
| } | |
| // 今回は常に0を返す. | |
| return 0; // returnで0を返す. | |
| } | |
| // アイドル処理関数OnIdle. | |
| BOOL CApplication::OnIdle(LONG lCount) { | |
| // 今回は常にFALSEを返す. | |
| return FALSE; // returnでFALSEを返す. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 二重インクルード防止 | |
| #ifndef __APPLICATION_H__ | |
| #define __APPLICATION_H__ | |
| // ヘッダのインクルード | |
| // 独自のヘッダ | |
| #include "Window.h" // CWindow | |
| // アプリケーションクラスCApplicationの定義 | |
| class CApplication { | |
| // publicメンバ | |
| public: | |
| // publicメンバ変数 | |
| CWindow* m_pMainWnd; // メインウィンドウポインタm_pMainWnd. | |
| // publicメンバ関数 | |
| // コンストラクタ | |
| CApplication(); // コンストラクタCApplication | |
| virtual BOOL InitInstance(HINSTANCE hInstance, LPTSTR lpCmdLine, int nShowCmd) = 0; // インスタンス初期化関数InitInstance.(純粋仮想関数) | |
| virtual int Run(); // メッセージループ処理関数Run. | |
| virtual int ExitInstance(); // 終了処理関数ExitInstance. | |
| virtual BOOL OnIdle(LONG lCount); // アイドル処理関数OnIdle. | |
| }; | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ヘッダのインクルード | |
| // 独自のヘッダ | |
| #include "resource.h" | |
| #include "MainApplication.h" // CMainApplication | |
| #include "MainWindow.h" // CMainWindow | |
| // インスタンス初期化関数InitInstance. | |
| BOOL CMainApplication::InitInstance(HINSTANCE hInstance, LPTSTR lpCmdLine, int nShowCmd) { | |
| // ウィンドウクラスの登録. | |
| CMainWindow::RegisterClass(hInstance); | |
| // CMainWindowオブジェクトの作成. | |
| CMainWindow* pMainWnd = new CMainWindow(); // CMainWindowオブジェクトを作成し, pMainWndに格納. | |
| m_pMainWnd = pMainWnd; // pMainWndをm_pMainWndにもセット. | |
| // ウィンドウの作成. | |
| if (!pMainWnd->CreateEx(0, _T("CApplication"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance)) { // m_pMainWnd->CreateExでウィンドウ作成し, 失敗した場合. | |
| // エラー処理 | |
| return FALSE; // returnでFALSEを返して異常終了. | |
| } | |
| // ウィンドウの表示. | |
| m_pMainWnd->ShowWindow(SW_SHOW); // m_pMainWnd->ShowWindowで表示. | |
| // TRUEを返す. | |
| return TRUE; // returnでTRUEを返す. | |
| } | |
| // メッセージループ処理関数Run. | |
| int CMainApplication::Run() { | |
| // 変数の初期化. | |
| MSG msg = { 0 }; // MSG型メッセージ構造体msgを{0}で初期化. | |
| int lCount = 0; // ウィンドウメッセージが来なかった時の回数を保持するint型変数lCountの初期値を0とする. | |
| // PeekMessageによるメインループ. | |
| while (TRUE) { // 常に真(TRUE)なので無限ループ. | |
| // ウィンドウメッセージが来ているかを確認する. | |
| if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { // PeekMessageでウィンドウメッセージが来ているかを確認し, 真なら来ている.(PM_NOREMOVEなのでメッセージキューからこのメッセージを削除しない.次のGetMessageがそのメッセージを処理する.) | |
| // 0にリセット. | |
| lCount = 0; // lCountを0にリセット. | |
| // 来ていたらそのメッセージを取得. | |
| if (GetMessage(&msg, NULL, 0, 0) > 0) { // GetMessageでPeekMessageで確認したメッセージを取得. | |
| // ウィンドウメッセージの送出 | |
| TranslateMessage(&msg); // TranslateMessageで仮想キーメッセージを文字メッセージへ変換. | |
| DispatchMessage(&msg); // DispatchMessageで受け取ったメッセージをウィンドウプロシージャ(この場合は独自に定義したWindowProc)に送出. | |
| } | |
| else { // 正常終了(0), またはエラーによる異常終了(-1). | |
| // メインループを抜ける. | |
| break; // breakでメインループを抜ける. | |
| } | |
| } | |
| else { // 偽ならウィンドウメッセージが来ていないとき. | |
| // アイドル処理 | |
| if (OnIdle(lCount)) { // OnIdleにlCountを渡して, この中でアイドル処理をする. | |
| lCount++; // TRUEで返ってくるたびにlCountを増やす. | |
| } | |
| } | |
| } | |
| // ExitInstanceの値を返す. | |
| return ExitInstance(); // returnでExitInstanceを返す. | |
| } | |
| // 終了処理関数ExitInstance. | |
| int CMainApplication::ExitInstance() { | |
| // 親クラスのExitInstanceを呼ぶ. | |
| return CApplication::ExitInstance(); | |
| } | |
| // アイドル処理関数OnIdle. | |
| BOOL CMainApplication::OnIdle(LONG lCount) { | |
| // 画面の更新. | |
| if (m_pMainWnd != NULL) { // m_pMainWndがNULLでない時. | |
| if (m_pMainWnd->m_hWnd != NULL) { // m_pMainWnd->m_hWndがNULLでない時. | |
| HDC hDC = GetDC(m_pMainWnd->m_hWnd); | |
| TextOut(hDC, 50, 50, _T("ABCDE"), lstrlen(_T("ABCDE"))); | |
| ReleaseDC(m_pMainWnd->m_hWnd, hDC); | |
| } | |
| } | |
| // とりあえずTRUE. | |
| return TRUE; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 二重インクルード防止 | |
| #ifndef __MAIN_APPLICATION_H__ | |
| #define __MAIN_APPLICATION_H__ | |
| // ヘッダのインクルード | |
| // 独自のヘッダ | |
| #include "Application.h" // CApplication | |
| // メインアプリケーションクラスCMainApplication | |
| class CMainApplication : public CApplication { | |
| // publicメンバ | |
| public: | |
| // publicメンバ関数 | |
| virtual BOOL InitInstance(HINSTANCE hInstance, LPTSTR lpCmdLine, int nShowCmd); // インスタンス初期化関数InitInstance. | |
| virtual int Run(); // メッセージループ処理関数Run. | |
| virtual int ExitInstance(); // 終了処理関数ExitInstance. | |
| virtual BOOL OnIdle(LONG lCount); // アイドル処理関数OnIdle. | |
| }; | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ヘッダのインクルード | |
| // 既定のヘッダ | |
| #include <windows.h> // 標準WindowsAPI | |
| // 独自のヘッダ | |
| #include "MainWindow.h" // CMainWindow | |
| #include "resource.h" | |
| // ウィンドウクラス登録関数RegisterClass. | |
| BOOL CMainWindow::RegisterClass(HINSTANCE hInstance) { | |
| // ウィンドウクラス名は"CMainWindow". | |
| return CWindow::RegisterClass(hInstance, _T("CMainWindow")); // CWindow::RegisterClassでウィンドウクラス名"CMainWindow"を登録. | |
| } | |
| // ウィンドウクラス登録関数RegisterClass.(メニュー名指定バージョン) | |
| BOOL CMainWindow::RegisterClass(HINSTANCE hInstance, LPCTSTR lpctszMenuName) { | |
| // メニュー名はlpctszMenuName. | |
| return CWindow::RegisterClass(hInstance, _T("CMainWindow"), lpctszMenuName); // CWindow::RegisterClassで, ウィンドウクラス名"CMainWindow", メニュー名lpctszMenuNameを登録. | |
| } | |
| // コンストラクタCMainWindow() | |
| CMainWindow::CMainWindow() { | |
| // メンバの初期化. | |
| m_hInstance = NULL; | |
| m_pMainMenu = NULL; | |
| } | |
| // デストラクタ~CMainWindow() | |
| CMainWindow::~CMainWindow() { | |
| // メンバの終了処理. | |
| Destroy(); // Destroyで子ウィンドウの破棄. | |
| } | |
| // ウィンドウ作成関数Create.(ウィンドウクラス名省略バージョン.) | |
| BOOL CMainWindow::Create(LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance) { | |
| // ウィンドウクラス名は"CMainWindow". | |
| return CWindow::Create(_T("CMainWindow"), lpctszWindowName, dwStyle, x, y, iWidth, iHeight, hWndParent, hMenu, hInstance); // CWindow::Createにウィンドウクラス名"CMainWindow"を指定. | |
| } | |
| // ウィンドウ作成関数CreateEx. | |
| BOOL CMainWindow::CreateEx(DWORD dwExStyle, LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance) { | |
| // ウィンドウクラス名は"CMainWindow". | |
| return CWindow::CreateEx(dwExStyle, _T("CMainWindow"), lpctszWindowName, dwStyle, x, y, iWidth, iHeight, hWndParent, hMenu, hInstance); // CWindow::CreateExにウィンドウクラス名"CMainWindow"を指定. | |
| } | |
| // ウィンドウ破棄関数Destroy | |
| BOOL CMainWindow::Destroy() { | |
| // 変数の初期化. | |
| BOOL bRet = FALSE; // bRetをFALSEで初期化. | |
| // DestroyChildrenを分けたので, 自身のウィンドウ破棄は問題ない. | |
| // まず子ウィンドウの破棄. | |
| DestroyChildren(); | |
| // 自身のウィンドウ破棄. | |
| bRet = CWindow::Destroy(); // 戻り値をbRetに格納. | |
| // bRetを返す. | |
| return bRet; | |
| } | |
| // 子ウィンドウ破棄関数DestroyChildren | |
| BOOL CMainWindow::DestroyChildren() { | |
| // 変数の初期化. | |
| BOOL bRet = FALSE; // bRetをFALSEで初期化. | |
| // 破棄したらTRUEを返す. | |
| if (bRet) { // TRUEなら. | |
| return TRUE; // TRUEを返す. | |
| } | |
| // 破棄しなければ, CWindowのDestroyChildrenを返す. | |
| return CWindow::DestroyChildren(); // CWindow::DestroyChildrenを返す. | |
| } | |
| // ウィンドウの作成が開始された時. | |
| int CMainWindow::OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) { | |
| // 親クラスのOnCreateを呼ぶ. | |
| m_hInstance = lpCreateStruct->hInstance; | |
| int iRet = CWindow::OnCreate(hwnd, lpCreateStruct); // CWindow::OnCreateを呼び, 戻り値をiRetに格納. | |
| m_pMainMenu = GetMenu(); // CWindow::GetMenuでm_pMainMenu取得. | |
| if (m_pMainMenu == NULL) { // メニューハンドルが無い場合は, m_pMainMenuがNULLになる. | |
| m_pMainMenu = new CMenu(); | |
| BOOL bRet = m_pMainMenu->LoadMenu(lpCreateStruct->hInstance, IDM_MAINMENU); | |
| if (bRet) { | |
| SetMenu(m_pMainMenu); | |
| } | |
| } | |
| return iRet; // iRetを返す. | |
| } | |
| // ウィンドウが破棄された時. | |
| void CMainWindow::OnDestroy() { | |
| // CWindowのOnDestroyを呼ぶ. | |
| CWindow::OnDestroy(); // CWindow::OnDestroyを呼ぶ. | |
| // メニューの終了処理. | |
| CMenu::DeleteMenuHandleMap(); | |
| m_pMainMenu = NULL; | |
| } | |
| // ウィンドウのサイズが変更された時. | |
| void CMainWindow::OnSize(UINT nType, int cx, int cy) { | |
| } | |
| // ウィンドウが閉じられる時. | |
| int CMainWindow::OnClose() { | |
| // メッセージボックスで"Close CMainWindow OK?"と表示. | |
| int iRet = MessageBox(m_hWnd, _T("Close CMainWindow OK?"), _T("CApplication"), MB_OKCANCEL); // MessageBoxで"Close CMainWindow OK?"と表示し, 戻り値をiRetに格納. | |
| if (iRet != IDOK) { // OK以外.(Cancelなど.) | |
| return -1; // -1を返す. | |
| } | |
| // このウィンドウの破棄.(OnCloseの後, ウィンドウの破棄処理が勝手に行われるので, Destroyは不要なのでコメントアウト.) | |
| //Destroy(); // Destroyでこのウィンドウの破棄処理. | |
| // OKなので閉じる. | |
| return CWindow::OnClose(); // 親クラスのOnCloseを呼ぶ.(親クラスのOnCloseは常に閉じる処理になっている.) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 二重インクルード防止 | |
| #ifndef __MAIN_WINDOW_H__ | |
| #define __MAIN_WINDOW_H__ | |
| // ヘッダのインクルード | |
| // 独自のヘッダ | |
| #include "Window.h" // CWindow | |
| #include "Menu.h" // CMenu | |
| // メインウィンドウクラスCMainWindow | |
| class CMainWindow : public CWindow { | |
| // publicメンバ | |
| public: | |
| // publicメンバ変数 | |
| HINSTANCE m_hInstance; // インスタンスハンドルm_hInstance. | |
| CMenu* m_pMainMenu; // CMenuオブジェクトポインタm_pMainMenu. | |
| // staticメンバ関数 | |
| static BOOL RegisterClass(HINSTANCE hInstance); // ウィンドウクラス登録関数RegisterClass. | |
| static BOOL RegisterClass(HINSTANCE hInstance, LPCTSTR lpctszMenuName); // ウィンドウクラス登録関数RegisterClass.(メニュー名指定バージョン) | |
| // publicメンバ関数 | |
| // コンストラクタ・デストラクタ | |
| CMainWindow(); // コンストラクタCMainWindow() | |
| virtual ~CMainWindow(); // デストラクタ~CMainWindow() | |
| virtual BOOL Create(LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance); // ウィンドウ作成関数Create.(ウィンドウクラス名省略バージョン.) | |
| virtual BOOL CreateEx(DWORD dwExStyle, LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance); // ウィンドウ作成関数CreateEx. | |
| virtual BOOL Destroy(); // ウィンドウ破棄関数Destroy | |
| virtual BOOL DestroyChildren(); // 子ウィンドウ破棄関数DestroyChildren | |
| virtual int OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct); // ウィンドウの作成が開始された時. | |
| virtual void OnDestroy(); // ウィンドウが破棄された時. | |
| virtual void OnSize(UINT nType, int cx, int cy); // ウィンドウのサイズが変更された時. | |
| virtual int OnClose(); // ウィンドウが閉じられる時. | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment