window.c

来自「ADS环境下的类似linux内核的操作系统」· C语言 代码 · 共 222 行

C
222
字号
/* MShowTec - www.mshowtec.com
** msGUI window.c ver1.0
** 20051221 lmjx create limiao@mshowtec.com
** 
*/

#define MSGUI_WINDOW_C

#include "gdi.h"
#include "window.h"

WIN g_wnd[MAXWINDOW] = {0};
HWND topVisibleWindow = 0;
HWND topWindow = 0;

HWND GetParent(HWND hwnd)
{
	return g_wnd[hwnd].pid;
}

HWND GetChild(HWND hwnd)
{
	return g_wnd[hwnd].child;
}

RECT GetRect(HWND hwnd)
{
	return g_wnd[hwnd].win_rect;	
}

int GetAllChild(HWND hwnd)
{
	int num = 0;	
	HWND cid;
	
	cid = GetChild(hwnd);
	while(cid){
		num++;
		cid = GetChild(cid);	
	}
	return num;
}

void InvalidateWindow(HWND id)
{
	g_wnd[id].invalid = 1;
	while(g_wnd[id].child)
	{
		id = g_wnd[id].child;
		InvalidateWindow(id);
	}
}

HWND CreateWindow(HWND pid,ENTRY func,PAINTCALLBACK paint,RECT rect, HRESRC pic, unsigned char* str)
{
	WIN * this;
	int id;
	RECT prect;
	int psx,psy,pex,pey,csx,csy,cex,cey;

	if(GetChild(pid))
	{
		DestroyWindow(GetChild(pid));
	}

	for(id=1;id<MAXWINDOW;id++)
	{
		if(g_wnd[id].entry == 0)
			break;
	}

	if(id >= MAXWINDOW)
	{
		return 0xff;
	}
	
	this = &g_wnd[id];

	memset(this, 0, sizeof(WIN));
	
	prect = GetRect(pid);
	psx = prect.startX;
	psy = prect.startY;
	pex = prect.startX + prect.width - 1;
	pey = prect.startY + prect.height - 1;
	
	csx = rect.startX;
	csy = rect.startY;
	cex = rect.startX + rect.width - 1;
	cey = rect.startY + rect.height - 1;
	
	if((psx > csx)||(psy > csy)||(pex < cex)||(pey < cey)){
				rect = prect;
	}

	this->pid				= pid;
	this->id				= id;
	this->entry			= func;
	this->paint_callback = paint;
	this->win_rect		= rect;
	this->pic				= pic;

	if(str)
	{
		memcpy(this->title,str,strlen(str));
	}

	this->invalid			= 1;
	
	g_wnd[pid].child = id;
	
	topWindow = id;
	
	SendWindowMessage(id,WM_CREATE,0,0);
	
	return id;

}

void DestroyWindow(HWND id)
{
	if(id == 0)//the desktop window can not be destroied
		return;
	if(id)
	{
		WIN* this = g_wnd+id;
		WIN* parent = g_wnd+this->pid;
		
		parent->child = 0;
		InvalidateWindow(this->pid);

		if(this->child)
		{
			DestroyWindow(this->child);
		}

		memset(g_wnd+id, 0, sizeof(WIN));
		
		topWindow = parent->id;
		
	}
	
}

int DrawWindow(HWND id)
{
	WIN* this = g_wnd + id;
	POINT point;
	
	if(this->invalid)
	{
		topVisibleWindow = id;
		
		if(this->pic)
			gdi_draw_picture(this->win_rect,this->pic);
			
		if(this->title[0]){
			point.x = this->win_rect.startX+3;
			point.y = this->win_rect.startY;
			gdi_text_out(this->win_rect,this->title,0);
		}
		
		if(this->child)
		{
			(g_wnd + this->child)->invalid = 1;
		}
		if(this->paint_callback)
			this->paint_callback(id);
			
		this->invalid = 0;
		
		return 1;
	}
	return 0;
}

int ShowWindow(HWND id)
{
	WIN* this= g_wnd + id;

	if(this->child)
	{
		int update = 0;
		
		do
		{
			update += DrawWindow(id);
			id = g_wnd[id].child;
		}while(id);

		return update;
	}
	return DrawWindow(id);
}

int ShowWindows()
{
	return ShowWindow(0);
}

int SendWindowMessage(HWND id, EVENT event, WPARAM wparam, LPARAM lparam)
{
	ENTRY entry = g_wnd[id].entry;

	if(entry == 0)
	{
		return 0;
	}
	return entry(id, event, wparam, lparam);
}

int TransferWindowMessage(HWND id, EVENT event, WPARAM wparam, LPARAM lparam)
{
	HWND cid;
	int ret = -1;
	
	cid = GetChild(id);
	if(cid)
		ret = SendWindowMessage(cid,event,wparam,lparam);
	
	return ret;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?