我是靠谱客的博主 迷路歌曲,这篇文章主要介绍第1章-MFC程序运行流程,现在分享给大家,希望可以做个参考。

流程
  1. WinMain函数编写
  2. 设计窗口类(WNDCLASS)
  3. 注册窗口类(RegisterClass)
  4. 创建窗口(CreateWindow)
  5. 显示并更新
  6. 消息循环
  7. 窗口过程函数(wndclass->lpfnWndProc)
代码
#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int WINAPI WinMain(//the entry point of programme
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	WNDCLASS wndcls; //1.the feature of window
	wndcls.cbClsExtra=0;
	wndcls.cbWndExtra=0;
	wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//the black brush wipe the backgorund of window
	wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
	wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndcls.hInstance=hInstance;
	wndcls.lpfnWndProc=WinSunProc;//the function of windows procedure
	wndcls.lpszClassName="MFC Procedure";
	wndcls.lpszMenuName=NULL;
	wndcls.style=CS_HREDRAW | CS_VREDRAW;
	
	RegisterClass(&wndcls);//2.register windclass

	HWND hwnd;
	hwnd=CreateWindow("MFC Procedure","新窗口创建",WS_OVERLAPPEDWINDOW,
		0,0,600,400,NULL,NULL,hInstance,NULL);

	ShowWindow(hwnd,SW_SHOWNORMAL);
	UpdateWindow(hwnd);//direct send WM_PAINT;

	MSG msg;
	//while(bRet=GetMessage(&msg,hWnd,0,0))!=0)
	//{
		//if(bRest==-1){//handle the error}
		//else{}
	//}
	while(GetMessage(&msg,NULL,0,0))//avoid endless loop
	{
		TranslateMessage(&msg);//translate the message of key_buton to ASCII
		DispatchMessage(&msg); //repass message to system,then OP call functional for repaly
	}
	return 0;
}

LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	switch(uMsg)
	{
	case WM_CHAR:
		char szChar[20];
		sprintf(szChar,"char is %d",wParam);
		MessageBox(hwnd,szChar,"weixin",0);
		break;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd,"mouse clicked","weixin",0);
		HDC hdc;
		hdc=GetDC(hwnd);
		TextOut(hdc,0,50,"计算机编程语言培训",strlen("计算机编程语言培训"));
		ReleaseDC(hwnd,hdc);
		break;
	case WM_PAINT:
		HDC hDC;
		PAINTSTRUCT ps;
		hDC=BeginPaint(hwnd,&ps);
		TextOut(hDC,0,0,"新星人类",strlen("新星人类"));
		EndPaint(hwnd,&ps);
		break;
	case WM_CLOSE:
		if(IDYES==MessageBox(hwnd,"是否真的结束?","MFC",MB_YESNO))
		{
			DestroyWindow(hwnd);//send WM_DESTROY
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);//send WM_QUIT and Return
		break;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
	return 0;
}

最后

以上就是迷路歌曲最近收集整理的关于第1章-MFC程序运行流程的全部内容,更多相关第1章-MFC程序运行流程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部