Mfc-messages-events
MFC-メッセージとイベント
アプリケーションはさまざまなオブジェクトで構成されています。 ほとんどの場合、複数のアプリケーションがコンピューターで実行されており、オペレーティングシステムは常にいくつかの割り当てを実行するように求められます。 予測できないほど多くの要求が提示される可能性があるため、オペレーティングシステムはオブジェクトに任せて、必要なもの、必要な場合、および期待される動作または結果を指定します。
概要
- Microsoft Windowsオペレーティングシステムは、1つのオブジェクトを処理する必要がある要求の種類と、別のオブジェクトが必要とする割り当ての種類を予測できません。
- これらすべての割り当てと要求を管理するために、オブジェクトはメッセージを送信します。
- 各オブジェクトには、どのメッセージをいつ送信するかを決定する責任があります。
- メッセージを送信するには、コントロールがイベントを作成する必要があります。
- この2つを区別するために、メッセージの名前は通常、ウィンドウメッセージを表すWM_で始まります。
- 通常、イベントの名前はOnで始まり、アクションを示します。
- イベントは、メッセージを送信するアクションです。
メッセージの地図
Windowsはメッセージ指向のオペレーティングシステムであるため、Windows環境のプログラミングの大部分はメッセージ処理に関係しています。 キーストロークやマウスクリックなどのイベントが発生するたびに、メッセージがアプリケーションに送信され、アプリケーションでイベントを処理する必要があります。
- コンパイラーがメッセージを管理するには、それらをクラス定義に含める必要があります。
- 次のコードに示すように、クラス定義の最後に DECLARE_MESSAGE_MAP マクロを提供する必要があります。
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
DECLARE_MESSAGE_MAP()
};
- 実際のメッセージは、DECLARE_MESSAGE_MAP行のすぐ上にリストする必要があります。
- メッセージを実装するには、プログラムが使用しているメッセージのテーブルを作成する必要があります。
- この表では、2つの区切りマクロを使用しています。
- BEGIN_MESSAGE_MAP で始まり、 END_MESSAGE_MAP マクロで終わります。
- BEGIN_MESSAGE_MAPマクロは、次のコードに示すように、クラスの名前とクラスの派生元のMFCクラスの2つの引数を取ります。
#include <afxwin.h>
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame() {
//Create the window's frame
Create(NULL, L"MFC Messages Demo", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CMessagesApp : public CWinApp {
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
END_MESSAGE_MAP()
BOOL CMessagesApp::InitInstance(){
m_pMainWnd = new CMainFrame;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMessagesApp theApp;
新しいWin32プロジェクトを作成して、簡単な例を見てみましょう。
- ステップ1 *-MFCプロジェクトを作成するには、プロジェクトを右クリックして[プロパティ]を選択します。
- ステップ2 *-左側のセクションで、[構成プロパティ]→[全般]をクリックします。
- ステップ3 *-[プロジェクトのデフォルト]セクションで[共有DLLでMFCを使用]オプションを選択し、[OK]をクリックします。
- ステップ4 *-新しいソースファイルを追加する必要があります。
- ステップ5 *-プロジェクトを右クリックして、[追加]→[新しいアイテム]を選択します。
- ステップ6 *-[テンプレート]セクションで、[C ++ファイル(.cpp)]をクリックします。
- ステップ7 *-[追加]をクリックして続行します。
ステップ8 *-次に、。cppファイルに次のコードを追加します。
#include <afxwin.h>
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame() {
//Create the window's frame
Create(NULL, L"MFC Messages Demo", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CMessagesApp : public CWinApp {
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
END_MESSAGE_MAP()
BOOL CMessagesApp::InitInstance() {
m_pMainWnd = new CMainFrame;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMessagesApp theApp;
Windowsメッセージ
ウィンドウの作成、ウィンドウの表示など、Windowsメッセージにはさまざまな種類があります。 一般的に使用されるWindowsメッセージの一部を次に示します。
以下に、さまざまな種類のウィンドウメッセージを示します。
Message | Map entry | Description |
---|---|---|
WM_ACTIVATE | ON_WM_ACTIVATE() | The framework calls this member function when a CWnd object is being activated or deactivated. |
WM_ACTIVATEA PP | ON_WM_ACTIVATEAPP() | The framework calls this member function to all top-level windows of the task being activated and for all top-level windows of the task being deactivated. |
WM_APPCOMM AND | ON_WM_APPCOMMAND() | The framework calls this member function when the user generates an application command event. |
WM_CANCELMODE | WM_CANCELMODE() | The framework calls this member function to inform CWnd to cancel any internal mode. |
WM_CHILDACTIVATE | ON_WM_CHILDACTIVATE() | If the CWnd object is a multiple document interface (MDI) child window, OnChildActivate is called by the framework when the user clicks the window’s title bar or when the window is activated, moved, or sized. |
WM_CLIPBOAR DUPDATE | ON_WM_CLIPBOARDUPDATE() | The framework calls this member function when the contents of the clipboard have changed. |
WM_CLOSE | ON_WM_CLOSE() | The framework calls this member function as a signal that the CWnd or an application is to terminate. |
WM_CONTEXTMENU | ON_WM_CONTEXTMENU() | Called by the framework when the user has clicked the right mouse button (rightclicked) in the window. |
WM_COPYDATA | ON_WM_COPYDATA() | This member function is called by the framework to copy data from one application to another. |
WM_CREATE | ON_WM_CREATE() | The framework calls this member function when an application requests that the Windows window be created by calling the Create or CreateEx member function. |
WM_CTLCOLOR | ON_WM_CTLCOLOR() | The framework calls this member function when a child control is about to be drawn. |
WM_DELETEITEM | ON_WM_DELETEITEM() | The framework calls this member function to inform the owner of an owner-draw list box or combo box that the list box or combo box is destroyed or that items have been removed. |
WM_DESTROY | ON_WM_DESTROY() | he framework calls this member function to inform the CWnd object that it is being destroyed. |
WM_DRAWITEM | ON_WM_DRAWITEM() | The framework calls this member function for the owner of an owner-draw button control, combo-box control, list-box control, or menu when a visual aspect of the control or menu has changed. |
WM_DROPFILES | ON_WM_DROPFILES() | The framework calls this member function when the user releases the left mouse button over a window that has registered itself as the recipient of dropped files. |
WM_ENABLE | ON_WM_ENABLE() | The framework calls this member function when an application changes the enabled state of the CWnd object. Syntax. |
WM_HELPINFO | ON_WM_HELPINFO() | Handles F1 Help within the application (using the current context). |
WM_HOTKEY | ON_WM_HOTKEY() | The framework calls this member function when the user presses a system-wide hot key. |
WM_HSCROLL | ON_WM_HSCROLL() | The framework calls this member function when the user clicks a window’s horizontal scroll bar. |
WM_KEYDOWN | ON_WM_KEYDOWN() | The framework calls this member function when a nonsystem key is pressed. |
WM_KEYUP | ON_WM_KEYUP() | The framework calls this member function when a nonsystem key is released. |
WM_KILLFOCUS | ON_WM_KILLFOCUS() | The framework calls this member function immediately before losing the input focus. |
WM_LBUTTONDBLCLK | ON_WM_LBUTTONDBLCLK() | The framework calls this member function when the user double-clicks the left mouse button. |
WM_LBUTTONDOWN | ON_WM_LBUTTONDOWN() | The framework calls this member function when the user presses the left mouse button. |
WM_LBUTTONUP | ON_WM_LBUTTONUP() | The framework calls this member function when the user releases the left mouse button. |
WM_MBUTTONDBLCLK | ON_WM_MBUTTONDBLCLK() | The framework calls this member function when the user double-clicks the middle mouse button. |
WM_MBUTTONDOWN | ON_WM_MBUTTONDOWN() | The framework calls this member function when the user presses the middle mouse button. |
WM_MBUTTONUP | ON_WM_MBUTTONUP() | The framework calls this member function when the user releases the middle mouse button. |
WM_MENUSELECT | ON_WM_MENUSELECT() | If the CWnd object is associated with a menu, OnMenuSelect is called by the framework when the user selects a menu item. |
WM_MOUSEACTIVATE | ON_WM_MOUSEACTIVATE() | The framework calls this member function when the cursor is in an inactive window and the user presses a mouse button. |
WM_MOUSEHOVER | ON_WM_MOUSEHOVER() | The framework calls this member function when the cursor hovers over the client area of the window for the period of time specified in a prior call to TrackMouseEvent. |
WM_MOUSEHWHEEL | ON_WM_MOUSEHWHEEL() | The framework calls this member when the current window is composed by the Desktop Window Manager (DWM), and that window is maximized. |
WM_MOUSELEAVE | ON_WM_MOUSELEAVE() | The framework calls this member function when the cursor leaves the client area of the window specified in a prior call to TrackMouseEvent. |
WM_MOUSEMOVE | ON_WM_MOUSEMOVE() | The framework calls this member function when the mouse cursor moves. |
WM_MOVE | ON_WM_MOVE() | The framework calls this member function after the CWnd object has been moved. |
WM_PAINT | ON_WM_PAINT() | The framework calls this member function when Windows or an application makes a request to repaint a portion of an application’s window. |
WM_SETFOCUS() | ON_WM_SETFOCUS( ) | The framework calls this member function after gaining the input focus. |
WM_SIZE( ) | ON_WM_SIZE( ) | The framework calls this member function after the window’s size has changed. |
WM_TIMER | ON_WM_TIMER() | The framework calls this member function after each interval specified in the SetTimer member function used to install a timer. |
WM_VSCROLL | ON_WM_VSCROLL() | The framework calls this member function when the user clicks the window’s vertical scroll bar. |
WM_WINDOWPOSCHANGED | ON_WM_WINDOWPOSCHANGED() | The framework calls this member function when the size, position, or Z-order has changed as a result of a call to the SetWindowPos member function or another window-management function. |
ウィンドウ作成の簡単な例を見てみましょう。
*WM_CREATE* -ウィンドウと呼ばれるオブジェクトが作成されると、オブジェクトを作成するフレームは *ON_WM_CREATE* として識別されるメッセージを送信します。
- ステップ1 *-ON_WM_CREATEを作成するには、afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);を追加します。以下に示すように、DECLARE_MESSAGE_MAP()の前。
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};
- ステップ2 *-BEGIN_MESSAGE_MAP(CMainFrame、CFrameWnd)の後でEND_MESSAGE_MAP()の前にON_WM_CREATE()を追加します
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
- ステップ3 *-これがOnCreate()の実装です
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
//Call the base class to create the window
if (CFrameWnd::OnCreate(lpCreateStruct) == 0) {
//If the window was successfully created, let the user know
MessageBox(L"The window has been created!!!");
//Since the window was successfully created, return 0
return 0;
}
//Otherwise, return -1
return -1;
}
ステップ4 *-これで、。cppファイルは次のコードのようになります。
#include <afxwin.h>
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame() {
//Create the window's frame
Create(NULL, L"MFC Messages Demo", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CMessagesApp : public CWinApp {
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
//Call the base class to create the window
if (CFrameWnd::OnCreate(lpCreateStruct) == 0) {
//If the window was successfully created, let the user know
MessageBox(L"The window has been created!!!");
//Since the window was successfully created, return 0
return 0;
}
//Otherwise, return -1
return -1;
}
BOOL CMessagesApp::InitInstance() {
m_pMainWnd = new CMainFrame;
m_pMainWnd -> ShowWindow(SW_SHOW);
m_pMainWnd -> UpdateWindow();
return TRUE;
}
CMessagesApp theApp;
- ステップ5 *-上記のコードをコンパイルして実行すると、次の出力が表示されます。
- ステップ6 *-[OK]をクリックすると、メインウィンドウが表示されます。
コマンドメッセージ
グラフィカルアプリケーションの主な機能の1つは、ユーザーがマシンと対話できるようにするWindowsコントロールとリソースを提示することです。 学習するコントロールの例は、ボタン、リストボックス、コンボボックスなどです。
前のレッスンで紹介したリソースの1つのタイプはメニューです。 このようなコントロールとリソースは、ユーザーがクリックすると独自のメッセージを開始できます。 Windowsコントロールまたはリソースから発せられるメッセージは、コマンドメッセージと呼ばれます。
コマンドメッセージの簡単な例を見てみましょう。
アプリケーションに新しいドキュメントを作成する機能を提供するために、CWinAppクラスはOnFileNew()メソッドを提供します。
afx_msg void OnFileNew();
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_COMMAND(ID_FILE_NEW, CMainFrame::OnFileNew)
END_MESSAGE_MAP()
ここにメソッド定義があります-
void CMainFrame::OnFileNew() {
//Create New file
}
キーボードメッセージ
- キーボード*は、コンピューターに接続されているハードウェアオブジェクトです。 デフォルトでは、認識可能な記号、文字、その他の文字をコントロールに入力するために使用されます。 キーボードの各キーには、記号、文字、またはそれらの組み合わせが表示され、キーの用途を示すことができます。 ユーザーは通常、プログラムに信号を送信するキーを押します。
各キーには、オペレーティングシステムが認識できるコードがあります。 このコードは*仮想キーコード*と呼ばれます。
____仮想キーコードは次のとおりです。
Sr.No. | Constant/value & Description |
---|---|
1 |
VK_LBUTTON 左マウスボタン |
2 |
VK_RBUTTON 右マウスボタン |
3 |
VK_CANCEL コントロールブレーク処理 |
4 |
VK_MBUTTON 中マウスボタン(3ボタンマウス) |
5 |
VK_BACK BACKSPACEキー |
6 |
VK_RETURN キーを入力してください |
7 |
VK_TAB Tabキー |
8 |
VK_CLEAR クリアキー |
9 |
VK_SHIFT シフトキー |
10 |
VK_CONTROL Ctrlキー |
11 |
VK_MENU ALTキー |
12 |
VK_PAUSE 一時停止キー |
13 |
VK_CAPITAL Caps Lockキー |
14 |
VK_ESCAPE ESCキー |
15 |
VK_SPACE スペースキー |
16 |
VK_PRIOR PAGE UPキー |
17 |
VK_NEXT Page Downキー |
18 |
VK_END ENDキー |
19 |
VK_HOME ホームキー |
20 |
VK_LEFT 左矢印キー |
21 |
VK_UP 上矢印キー |
22 |
VK_RIGHT 右矢印キー |
23 |
VK_DOWN 下矢印キー |
24 |
VK_SELECT SELECTキー |
25 |
VK_PRINT 印刷キー |
26 |
VK_EXECUTE 実行キー |
27 |
VK_SNAPSHOT 印刷画面キー |
28 |
VK_INSERT INSキー |
29 |
VK_DELETE DELキー |
30 |
VK_NUMPAD0 テンキー0キー |
31 |
VK_NUMPAD1 テンキー1キー |
32 |
VK_NUMPAD2 テンキー2キー |
33 |
VK_NUMPAD3 テンキー3キー |
34 |
VK_NUMPAD4 テンキー4キー |
35 |
VK_NUMPAD5 数字キーパッド5キー |
36 |
VK_NUMPAD6 テンキー6キー |
37 |
VK_NUMPAD7 テンキー7キー |
38 |
VK_NUMPAD8 テンキー8キー |
39 |
VK_NUMPAD9 テンキー9キー |
40 |
VK_MULTIPLY 乗算キー |
41 |
VK_ADD キーを追加 |
42 |
VK_SEPARATOR 区切りキー |
43 |
VK_SUBTRACT 減算キー |
44 |
VK_DECIMAL 10進数のキー |
45 |
VK_DIVIDE 分割キー |
46 |
VK_F1 F1キー |
47 |
VK_F2 F2キー |
48 |
VK_F3 F3キー |
49 |
VK_F4 F4キー |
50 |
VK_F5 F5キー |
52 |
VK_F6 F6キー |
53 |
VK_F7 F7キー |
54 |
VK_F8 F8キー |
55 |
VK_F9 F9キー |
56 |
VK_F10 F10キー |
57 |
VK_F11 F11キー |
58 |
VK_F12 F12キー |
59 |
VK_NUMLOCK NUM LOCKキー |
60 |
VK_SCROLL スクロールロックキー |
61 |
VK_LSHIFT 左Shiftキー |
62 |
VK_RSHIFT 右Shiftキー |
63 |
VK_LCONTROL 左コントロールキー |
64 |
VK_RCONTROL 右コントロールキー |
キーを押すと、https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-keydown [WM_KEYDOWN]またはhttps://docs.microsoft.com/en-us/windows/desktop/が発生しますinputdev/wm-syskeydown [WM_SYSKEYDOWN]メッセージがスレッドメッセージに配置されます。 これは次のように定義することができます-
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
簡単な例を見てみましょう。
- ステップ1 *-これがメッセージです。
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
- ステップ2 *-OnKeyDown()の実装です。
void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
switch (nChar) {
case VK_RETURN:
MessageBox(L"You pressed Enter");
break;
case VK_F1:
MessageBox(L"Help is not available at the moment");
break;
case VK_DELETE:
MessageBox(L"Can't Delete This");
break;
default:
MessageBox(L"Whatever");
}
}
- ステップ3 *-上記のコードをコンパイルして実行すると、次の出力が表示されます。
- ステップ4 *-Enterキーを押すと、次のメッセージが表示されます。
マウスメッセージ
マウスは、コンピューターに接続されている別のオブジェクトで、ユーザーがマシンと対話できるようにします。
- マウスの左ボタンが押された場合、ON_WM_LBUTTONDOWNメッセージが送信されます。 このメッセージの構文は次のとおりです-
- afx_msg void OnLButtonDown(UINT nFlags、CPointポイント)
- マウスの右ボタンが押された場合、ON_WM_RBUTTONDOWNメッセージが送信されます。 その構文は-
- afx_msg void OnRButtonDown(UINT nFlags、CPointポイント)
- 同様に、左マウスを離すと、ON_WM_LBUTTONUPメッセージが送信されます。 その構文は-
- afx_msg void OnLButtonUp(UINT nFlags、CPointポイント)
- 右マウスボタンを離すと、ON_WM_TBUTTONUPメッセージが送信されます。 その構文は-
- afx_msg void OnRButtonUp(UINT nFlags、CPointポイント)
簡単な例を見てみましょう。
- ステップ1 *-次のコードに示すように、CMainFrameクラス定義に次の2つの関数を追加します。
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
- ステップ2 *-次の2つのメッセージマップを追加します。
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONUP()
END_MESSAGE_MAP()
- ステップ3 *-これが関数の定義です。
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point) {
CString MsgCoord;
MsgCoord.Format(L"Left Button at P(%d, %d)", point.x, point.y);
MessageBox(MsgCoord);
}
void CMainFrame::OnRButtonUp(UINT nFlags, CPoint point) {
MessageBox(L"Right Mouse Button Up");
}
- ステップ4 *-このアプリケーションを実行すると、次の出力が表示されます。
- ステップ5 *-[OK]をクリックすると、次のメッセージが表示されます。
- ステップ6 *-このウィンドウを右クリックします。 これで、マウスの右ボタンを放すと、次のメッセージが表示されます。