关闭

利用Windows绘制界面并且编写窗口过程函数

标签: API界面编程WINDOWS
526人阅读 评论(0) 收藏 举报
分类:

这篇blog主要是将的是如何实现窗口过程编程。

即通过窗口过程函数的编写来实现其如何处理消息的方式。


例如:

#include<windows.h>
#include <stdio.h>
/*LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
	return	DefWindowProc( hWnd,  uMsg,  wParam, lParam);
}
*/
LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
	switch(uMsg){
	case WM_CHAR:   //按下键盘按键会产生以下函数的调用
		char szchar[20];
		sprintf_s (szchar,20,"char is %d",wParam);
		MessageBoxA(hWnd, szchar, "Keyboard",MB_OK);
		break;
	case WM_LBUTTONDOWN:  //按下鼠标后会产生一下函数的调用
		MessageBoxA(hWnd, "Mouse clicked", "Mouse",MB_OK);
		HDC hdc;
		hdc = GetDC(hWnd);
		TextOutA(hdc,0,0,"You clicked the left button",strlen("You clicked the left button"));
		ReleaseDC(hWnd,hdc);
		break;
	case WM_PAINT:      //移动窗口后会产生一下函数的调用
		HDC hDC;
		PAINTSTRUCT ps;
		hDC = BeginPaint(hWnd,&ps);
		TextOutA(hDC, 0, 0, "http://www.baidu.com",strlen("http://www.baidu.com"));
		EndPaint(hWnd,&ps);
		break;
	case WM_CLOSE:    //点击X按钮关闭程序,同时产生WM_DESTROY来引发WM_DESTROY函数的执行
		if(IDYES == MessageBoxA(hWnd,"rellay over?", "message", MB_YESNO)){
			DestroyWindow(hWnd);
			PostQuitMessage(0);
		}
		break;
	case WM_DESTROY:
		MessageBoxA(hWnd,"Destroy","Destroy",MB_OK);
		PostQuitMessage(0);
		break;
	default:  //默认返回类型,如果忘记加return则 生成的wnidows窗口不会出现
		return DefWindowProc( hWnd,  uMsg,  wParam, lParam);
	}
	return 0;
}
int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, 
	int nCmdShow){
		HWND hWnd;
		MSG uMsg;
		WNDCLASS wc;
		wc.style = CS_HREDRAW | CS_VREDRAW;
		wc.hCursor = NULL;
		wc.hIcon = NULL;
		wc.lpfnWndProc = (WNDPROC)WinProc;
		wc.cbClsExtra = 0;
		wc.cbWndExtra = 0;
		wc.hInstance = hInstance;
		wc.hbrBackground = (HBRUSH)COLOR_GRAYTEXT+ 1;	
		wc.lpszMenuName = NULL;
		wc.lpszClassName = TEXT("Main");
		RegisterClass(&wc);
		hWnd = CreateWindowA("Main", "WINDOWS",WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hInstance, NULL);
		ShowWindow(hWnd, SW_SHOW);	
		UpdateWindow(hWnd);
		while(GetMessage(&uMsg, NULL, 0, 0)){
			TranslateMessage(&uMsg);
			DispatchMessage(&uMsg);		
		}
		return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:340754次
    • 积分:6602
    • 等级:
    • 排名:第3583名
    • 原创:331篇
    • 转载:14篇
    • 译文:0篇
    • 评论:116条
    最新评论