Created
June 24, 2024 01:30
-
-
Save bg1bgst333/ea743235768971853cddcb54dc8a58fa to your computer and use it in GitHub Desktop.
CMenu::DeleteMenu
This file contains 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 <stdio.h> // C標準入出力 | |
// 独自のヘッダ | |
#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_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"を指定. | |
} | |
// ウィンドウ破棄関数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を呼ぶ. | |
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->CreateMenu(); | |
if (bRet) { | |
// サブメニュー1の作成. | |
CMenu* pSubMenu1 = new CMenu(); | |
pSubMenu1->CreatePopupMenu(); | |
pSubMenu1->AppendMenu(MF_STRING, ID_ITEM_1_1, _T("Item1-1(&A)")); | |
pSubMenu1->AppendMenu(MF_STRING, ID_ITEM_1_2, _T("Item1-2(&B)")); | |
pSubMenu1->AppendMenu(MF_STRING, ID_ITEM_1_3, _T("Item1-3(&C)")); | |
pSubMenu1->AppendMenu(MF_STRING, ID_ITEM_1_4, _T("Item1-4(&D)")); | |
// メインメニューに追加. | |
m_pMainMenu->AppendMenu(MF_POPUP, (UINT_PTR)pSubMenu1->m_hMenu, _T("Item1")); | |
// サブメニュー2の作成. | |
CMenu* pSubMenu2 = new CMenu(); | |
pSubMenu2->CreatePopupMenu(); | |
pSubMenu2->AppendMenu(MF_STRING, ID_ITEM_2_1, _T("Item2-1(&E)")); | |
pSubMenu2->AppendMenu(MF_STRING, ID_ITEM_2_2, _T("Item2-2(&F)")); | |
pSubMenu2->AppendMenu(MF_STRING, ID_ITEM_2_3, _T("Item2-3(&G)")); | |
pSubMenu2->AppendMenu(MF_STRING, ID_ITEM_2_4, _T("Item2-4(&H)")); | |
// メインメニューに追加. | |
m_pMainMenu->AppendMenu(MF_POPUP, (UINT_PTR)pSubMenu2->m_hMenu, _T("Item2")); | |
// サブメニュー3の作成. | |
CMenu* pSubMenu3 = new CMenu(); | |
pSubMenu3->CreatePopupMenu(); | |
pSubMenu3->AppendMenu(MF_STRING, ID_ITEM_3_1, _T("Item3-1(&I)")); | |
pSubMenu3->AppendMenu(MF_STRING, ID_ITEM_3_2, _T("Item3-2(&J)")); | |
pSubMenu3->AppendMenu(MF_STRING, ID_ITEM_3_3, _T("Item3-3(&K)")); | |
pSubMenu3->AppendMenu(MF_STRING, ID_ITEM_3_4, _T("Item3-4(&L)")); | |
// メインメニューに追加. | |
m_pMainMenu->AppendMenu(MF_POPUP, (UINT_PTR)pSubMenu3->m_hMenu, _T("Item3")); | |
SetMenu(m_pMainMenu); // CWindow::SetMenuでm_pMainMenuをセット. | |
// メニューハンドラの追加. | |
AddCommandHandler(ID_ITEM_1_1, 0, (int(CWindow::*)(WPARAM, LPARAM)) & CMainWindow::OnItem1_1); // AddCommandHandlerでID_ITEM_1_1に対するハンドラCMainWindow::OnItem1_1を登録. | |
AddCommandHandler(ID_ITEM_2_1, 0, (int(CWindow::*)(WPARAM, LPARAM)) & CMainWindow::OnItem2_1); // AddCommandHandlerでID_ITEM_2_1に対するハンドラCMainWindow::OnItem2_1を登録. | |
} | |
} | |
return iRet; // iRetを返す. | |
} | |
// ウィンドウが破棄された時. | |
void CMainWindow::OnDestroy() { | |
// メニューハンドラの削除. | |
DeleteCommandHandler(ID_ITEM_1_1, 0); // DeleteCommandHandlerでID_ITEM_1_1のハンドラを削除. | |
DeleteCommandHandler(ID_ITEM_2_1, 0); // DeleteCommandHandlerでID_ITEM_2_1のハンドラを削除. | |
// メニューの終了処理. | |
CMenu::DeleteMenuHandleMap(); | |
m_pMainMenu = NULL; | |
// CWindowのOnDestroyを呼ぶ. | |
CWindow::OnDestroy(); // CWindow::OnDestroyを呼ぶ. | |
} | |
// ウィンドウのサイズが変更された時. | |
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("CMenu"), 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は常に閉じる処理になっている.) | |
} | |
// Item1-1が選択された時. | |
int CMainWindow::OnItem1_1(WPARAM wParam, LPARAM lParam) { | |
// Item1-1を削除. | |
CMenu* pSubMenu0 = m_pMainMenu->GetSubMenu(0); | |
pSubMenu0->DeleteMenu(ID_ITEM_1_1, MF_BYCOMMAND); | |
// 0を返す. | |
return 0; // 処理したので0. | |
} | |
// Item2-1が選択された時. | |
int CMainWindow::OnItem2_1(WPARAM wParam, LPARAM lParam) { | |
// Item2を削除. | |
m_pMainMenu->DeleteMenu(1, MF_BYPOSITION); | |
// 0を返す. | |
return 0; // 処理したので0. | |
} |
This file contains 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メンバ変数 | |
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 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(); // ウィンドウが閉じられる時. | |
int OnItem1_1(WPARAM wParam, LPARAM lParam); // Item1-1が選択された時. | |
int OnItem2_1(WPARAM wParam, LPARAM lParam); // Item2-1が選択された時. | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment