您的位置:首页 > 其它

MFC学习笔记.day01续-调试MessageBox,注册窗口,创建窗口,显示,更新,消息循环,回调函数

2015-07-23 10:15 495 查看

MFC调试

在MFC 中程序繁杂,很容易出错。有时间断点不一定能直观的显示程序执行的情况。所以,我们可以再函数中任意一行添加

MessageBox(_T("xxxxxxxxxxxxxxxxx"));


或者:

MassageBox(_T("程序执行到此处"));


两个函数可以直接生成指定风格的消息对话框,而不需要我们在每次使用的时候都要去创建对话框资源和生成对话框类等。这两个函数就是CWnd类的成员函数MessageBox()和全局函数AfxMessageBox()。

int MessageBox(
HWND hWnd,
//Handle to the owner window of the message box to be created.
//If this parameter is NULL, the message box has no owner window.

LPCTSTR lpText,
//Pointer to a null-terminated string that contains the message to be displayed

LPCTSTR lpszCaption = NULL,
//Pointer to a null-terminated string that contains the dialog box title. If this parameter is NULL, the default title Error is used.

UINT nType = MB_OK

);


nType的取值可以是下面两个表中任取一个值,也可以是各取一个值的任意组合。即可以指定一个对话框类型,也可以指定一个对话框图标,还可以两者都设定。

nType 取值 参数说明

MB_ABORTRETRY 有“终止”、“重试”和“忽略”按钮

MB_OK 有“确定”按钮

MB_OKCANCEL 有“确定”和“取消”按钮

MB_RETRYCANCEL 有“重试”和“取消”按钮

MB_YESNO 有“是”和“否”按钮

MB_YESNOCANCEL 有“是”、“否”和“取消”按钮

对话框类型表

nType 取值 显示图标

MB_ICONEXCLAMTION

MB_ICONWARNING

MB_ICONASTERISK

MB_ICONINFORMATION

MB_ICONQUESTION

MB_ICONHAND

MB_ICONSTOP

MB_ICONERROR

#####Example
Visual C++  Copy Code
void CMainFrame::OnDisplayErrorMessage()
{
// This displays a message box with the title "Error"
// and the message "Help, Something went wrong."
// The error icon is displayed in the message box, along with
// an OK button.
MessageBox(_T("Help, Something went wrong."), _T("Error"),
MB_ICONERROR | MB_OK);
}


注册窗口类:RegisterClass

ATOM RegisterClass(
CONST WNDCLASS *lpWndClass  // class data  窗口类结构体的指针
);


创建窗口——CreateWindow

① 定义一个句
HWND  hwnd


② 创建窗口

Hwnd=CreateWindow()


HWND CreateWindow(
LPCTSTR lpClassName, // registered class name 一定要是已经注册的类名
LPCTSTR lpWindowName,// window name 窗口的名字
DWORD dwStyle,       // window style 窗口的类型
int x,               // horizontal position of window 窗口显示的时候的左上角的x的坐标值
int y,               // vertical position of window 窗口显示的时候的左上角的y的坐标值
int nWidth,          // window width  窗口的大小的宽度
int nHeight,         // window height 窗口大小的高度
HWND hWndParent,     // handle to parent or owner window 指示的副窗口的句柄
HMENU hMenu,         // menu handle or child identifier菜单的句柄
HINSTANCE hInstance, // handle to application instance 当前应用程序的实例的句柄
LPVOID lpParam        // window-creation data   窗口创建的附加参数
);


③ 显示窗口 ——ShowWindow

BOOL ShowWindow(

HWND hWnd, // handle to window 要显示的窗口的指针,显示的是哪一个窗口

int nCmdShow // show state 要显示的窗口的状态

);

④ 更新窗口 ——UpdataWindow

⑤ 消息循环

MSG msg;

While (GetMessage(&msg,NULL,0,0))这里用GetMessage从我们的消息的队列中取出一条消息

当执行PostQuitmessage时会产生Wm_quit消息,此时消息循环跳出,结束消息循环

{

TranslateMessage(&msg); 将Wm-Keydown 和 keyup 的消息转化为WM-CHAR消息,并且将转化后的新消息投递到我们的消息队列当中

DispatchMessage(&msg);将收到的消息传到我们的系统,系统来调用我们窗口的回调函数,交由回调函数来处理

}

备注: DispatchMessage是将消息分发给操作系统,然后操作系统再去调用回调函数,由回调函数来进行处理。

回调函数:

LRESULT CALLBACK WindowProc(
HWND hwnd,     // handle to window  窗口的句柄
UINT uMsg,     // message identifier      消息的标识
WPARAM wParam, // first message parameter  第一个消息的附加参数
LPARAM lParam   // second message parameter 第二个消息的附加参数
);


说明:这个函数的形式不能变,但是可以改变函数名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: