复习windows程序运行机制

通过编写一个简单的win32对话框程序,回顾了一下windows程序的运行机制。

 

#include <Windows.h>
#include <stdio.h>

//操作系统将每个事件包装成消息放入消息队列中,应用程序从消息队列中获取消息并做相应处理

//消息回调函数声明
LRESULT CALLBACK Winmypro(          
						  HWND hwnd,
						  UINT uMsg,
						  WPARAM wParam,
						  LPARAM lParam
						  );

//入口函数
int WINAPI WinMain(          
				   HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow
				   )
{
	//设计窗口类
	WNDCLASS wnd;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wnd.hCursor = LoadCursor(NULL,IDC_CROSS);
	wnd.hIcon = LoadIcon(NULL,IDI_ERROR);
	wnd.hInstance = hInstance;
	wnd.lpfnWndProc = Winmypro;
	wnd.lpszClassName = "zhang";
	wnd.lpszMenuName = NULL;
	wnd.style = CS_HREDRAW | CS_VREDRAW;

	//注册窗口类
	RegisterClass(&wnd);

	//创建窗口
	HWND hWnd;
	hWnd = CreateWindow("zhang","Windows程序运行机制",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);

	//显示窗口
	ShowWindow(hWnd,SW_NORMAL);

	//更新窗口
	UpdateWindow(hWnd);

	//从消息队列获取消息
	MSG msg;
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

//回调函数实现
LRESULT CALLBACK Winmypro(          
						  HWND hwnd,
						  UINT uMsg,
						  WPARAM wParam,
						  LPARAM lParam
						  )
{
	switch(uMsg)
	{
	case WM_CHAR:
		char szchar[20];
		sprintf(szchar,"char is %d",wParam);
		MessageBox(hwnd,szchar,"注意",MB_OK);
		break;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd,"mouse click","文字",MB_OK);
		HDC hDC;
		hDC = GetDC(hwnd);
		TextOut(hDC,0,50,"Windows运行机制",strlen("Windows运行机制"));
		ReleaseDC(hwnd,hDC);
		break;
	case WM_PAINT://每当窗口发生重绘
		HDC hDC1;
		PAINTSTRUCT ps;
		hDC1 = BeginPaint(hwnd,&ps);
		TextOut(hDC1,0,0,"Windows运行机制_重绘了",strlen("Windows运行机制_重绘了"));
		EndPaint(hwnd,&ps);
		break;
	case WM_CLOSE://窗口关闭时
		if(IDYES == MessageBox(hwnd,"确认结束?","提示",MB_YESNO))
		{
			DestroyWindow(hwnd);//销毁窗口,并产生WM_DESTROY消息
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);//向系统发送关闭程序消息
		break;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
	return 0;
}

//相关函数:
//消息结构体
typedef struct tagMSG { 
HWND hwnd; 	   //Handle to the window whose window procedure receives the message. 资源的标识
UINT message;  //Unsigned int,Specifies the message identifier.消息的标识,消息用宏表示
WPARAM wParam; //Specifies additional information about the message.附加消息
LPARAM lParam; //Specifies additional information about the message.
DWORD time;    //Double WORD,32-bit unsigned int.Specifies the time at which the message was posted. 
POINT pt;      //The POINT structure defines the x- and y- coordinates of a point. 
}MSG;

//其中
typedef struct tagPOINT { 
  LONG x; 
  LONG y; 
} POINT, *PPOINT;

HICON LoadIcon(
  HINSTANCE hInstance, // handle to application instance
  LPCTSTR lpIconName   // name string or resource identifier
);

//注册:
ATOM RegisterClass(
  CONST WNDCLASS *lpWndClass  // class data
);

//创建:
HWND CreateWindow(
  LPCTSTR lpClassName,  // registered class name.注册的窗口类名
  LPCTSTR lpWindowName, // window name.窗口名字
  DWORD dwStyle,        // window style.窗口类型
  int x,                // horizontal position of window窗口水平坐标(窗口左上角)
  int y,                // vertical position of window窗口垂直坐标
  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.  WM_CREATE的附加参数
);

//显示并更新:
BOOL ShowWindow(
  HWND hWnd,     // handle to window
  int nCmdShow   // show state
);

BOOL UpdateWindow(
  HWND hWnd   // handle to window
);

BOOL GetMessage(//从消息消息队列中取消息并返回,收到WM_QUIT返回0
  LPMSG lpMsg,         // message information
  HWND hWnd,           // handle to window
  UINT wMsgFilterMin,  // first message.设定消息最小值
  UINT wMsgFilterMax   // last message.设定消息最大值
}

BOOL TranslateMessage(  CONST MSG *lpMsg   // message information);
//翻译消息。

LRESULT DispatchMessage(
  CONST MSG *lpmsg   // message information
);

//回调函数
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int MessageBox(
  HWND hWnd,          // handle to owner window
  LPCTSTR lpText,     // text in message box
  LPCTSTR lpCaption,  // message box title
  UINT uType          // message box style
);

//DC设备上下文
HDC GetDC(
  HWND hWnd   // handle to window
);

//calls the default window procedure to provide default processing for any windo
//w messages that an application does not process. This function ensures that 
//every message is processed. DefWindowProc is called with the same parameters 
//received by the window procedure. 
//默认处理
LRESULT DefWindowProc(
  HWND hWnd,      // handle to window
  UINT Msg,       // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

BOOL TextOut(
  HDC hdc,           // handle to DC
  int nXStart,       // x-coordinate of starting position
  int nYStart,       // y-coordinate of starting position
  LPCTSTR lpString,  // character string
  int cbString       // number of characters
);

WM_PAINT//重绘消息

HDC BeginPaint(//只能在WM_PAINT下使用
  HWND hwnd,            // handle to window
  LPPAINTSTRUCT lpPaint // paint information
);

//销毁窗口
BOOL DestroyWindow(
  HWND hWnd   // handle to window to destroy
);

//indicates to the system that a thread has made a request to terminate (quit). 
//发送WM_QUIT到消息队列
VOID PostQuitMessage(
  int nExitCode   // exit code
);

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值