⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win.c

📁 深圳市微逻辑电子有限公司 巨果&#8226 Kingmos&reg 系统核心
💻 C
📖 第 1 页 / 共 5 页
字号:
//			WS_SYSMENU-系统菜单
//			WS_THICKFRAME-可缩放边框
//			WS_MINIMIZEBOX-最小尺寸盒
//			WS_MAXIMIZEBOX-最大尺寸盒
//			WS_GROUP-组标志
//			WS_TABSTOP-可用TAB键接受键盘焦点
//			WS_OVERLAPPEDWINDOW-组合风格(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX |WS_MAXIMIZEBOX )
//			WS_POPUPWINDOW-组合风格( WS_POPUP | WS_BORDER | WS_SYSMENU )
//			WS_CHILDWINDOW-组合风格( WS_CHILD )
//			WS_VISIBLE-初始可视
//			WS_DISABLED-初始不可能
//			WS_MINIMIZE-初始最小化
//			WS_MAXIMIZE-初始最大化
//			WS_EX_TOPMOST-顶层窗口
//	IN x-左上角X坐标
//	IN y-左上角Y坐标
//	IN dx-窗口宽度
//	IN dy-窗口高度
//	IN hParent-父窗口
//	IN hMenu-菜单
//	IN hInstance-实例
//	IN lpCreateParam-创建数据

// 返回值:
//	成功:返回窗口句柄
//	否则:返回NULL
// 功能描述:
//	创建窗口
// 引用: 
//	系统API
// ************************************************

#define DEBUG_WIN_CREATEEX 0
HWND WINAPI Win_CreateEx(                 
                    DWORD dwExStyle,
                    LPCBYTE lpClassName,
		            LPCBYTE lpText,
                    DWORD dwMainStyle,
		            int x, int y, int dx, int dy,
		            HWND hParent,
		            HMENU hMenu,
		            HINSTANCE hInstance,
		            LPVOID lpCreateParams )
{
    _LPWINDATA lpws, lpwsParent = NULL;
    HANDLE hmem;
    CREATESTRUCT cs, *lpcs;
    BOOL bVisible;
	_LPREGCLASS lpClass;
	//检查参数
	if( lpClassName == NULL )
	{
		SetLastError( ERROR_INVALID_PARAMETER );
		return NULL;
	}

	if( dx < 0 || dy < 0 )
	{
		dx = 0; dy = 0;
		DEBUGMSG( DEBUG_WIN_CREATEEX, ( "CreateWindow Warn: window width(%d) or height(%d) < 0,class(%s),title(%s).\r\n", dx, dy, lpClassName ? (char*)lpClassName : "", lpText ? (char*)lpText : "" ) );
	}

    DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b1.\r\n" ) );

	bVisible = (dwMainStyle & WS_VISIBLE) ? 1 : 0;
    dwMainStyle &= ~WS_VISIBLE;
	if( hParent )
		lpwsParent = _GetHWNDPtr( hParent );
	
    if( (dwMainStyle & WS_POPUP) && lpwsParent )//hParent )
    {
        lpwsParent = _GetSafeOwner( lpwsParent );//, 0 );
    }
    else if( dwMainStyle & WS_CHILD )
    {    // 子窗口 child window
        if( lpwsParent == NULL )
		{
			ASSERT_NOTIFY( 0, "CreateWindow Error: child window no parent\r\n" );
			SetLastError( ERROR_INVALID_PARAMETER );
            return NULL;
		}
    }
    else
    {    // ws_overlapp
        lpwsParent = NULL;
    }

	DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b3.\r\n" ) );
	//得到类对象
	lpClass = GetClassPtr( (LPCSTR)lpClassName, hInstance );
	if( lpClass == NULL )
	{	//无效的类或类不存在
		WARNMSG( DEBUG_WIN_CREATEEX, ("Invalid class or the class not exist!:(ptr=0x%x.),%s.\r\n", lpClassName, lpClassName ? lpClassName : "" ) );
		SetLastError( ERROR_INVALID_PARAMETER );
		return NULL;
	}

	DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b4.\r\n" ) );

	// 分配窗口对象 alloc winstruct mem, align to long( 4 byte )
    lpws = (_LPWINDATA)_AllocWinObjectHandle( sizeof (_WINDATA) + (lpClass->wc.cbWndExtra + 3) / 4 * 4 );
    if( lpws == NULL )
	{
        return NULL;
	}
	// 得到窗口对象的句柄 2004-02-24
	hmem = (HANDLE)__GET_OBJECT_HANDLE( lpws );//PTR_TO_HANDLE( lpws );

    DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b5.\r\n" ) );
   
	//初始化窗口对象 ================now init the lpws struct begin============
	lpws->lpClass = lpClass;
	lpws->hThis = hmem;
    lpws->lpfnWndProc = lpClass->wc.lpfnWndProc;
	lpws->hOwnerProcess = GetCallerProcess();
	lpws->hSmallIcon = lpClass->wc.hIcon;
	lpws->hBigIcon = NULL; // reserve for future;
	//初始化创建结构
    cs.x = x;
    cs.y = y;
    cs.cx = dx; cs.cy = dy;
    cs.lpszClass = (LPCSTR)lpClassName;
    cs.lpszName = (LPCSTR)lpText;
    cs.style = dwMainStyle;
    cs.dwExStyle = dwExStyle;
    cs.hInstance = hInstance;
    cs.hMenu = hMenu;
    cs.hParent = lpwsParent ? lpwsParent->hThis : NULL;
    cs.lpCreateParams = lpCreateParams;
	//初始化窗口对象
    _SetWinParam( lpws, &cs );

    DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b6.\r\n" ) );

	// 将窗口插入窗口链表 insert this window to parent
	LockCSWindow();

	if( dwMainStyle & WS_CHILD )
    {	//子窗口
		Link_InsertToParent( (_LPWINDATA)HWND_BOTTOM, lpws, lpwsParent );//
    }
    else
    {	//
        if( (lpws->dwMainStyle & WS_POPUP) && hParent )
        {	//弹出式窗口
			if( lpwsParent->dwExStyle & WS_EX_TOPMOST )
                lpws->dwExStyle |= WS_EX_TOPMOST;
			Interlock_Increment( (LPLONG)&lpwsParent->uOwnCount );
        }
        Link_InsertWindowToScreen( lpws );
		lpws->lpwsOwner = lpws->lpwsParent = lpwsParent;
    }
	UnlockCSWindow();

	DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b7.\r\n" ) );
	//增加对类的引用
	AddClassRef( lpClass );

	// 发送 WS_NCCREATE and WS_CREATE to wnd proc
	lpcs = (CREATESTRUCT*)MapPtrToProcess( &cs, hgwmeProcess );
	if( WinMsg_Send( hmem, WM_NCCREATE, 0, (LPARAM)lpcs ) == TRUE )
    {   // get client bound
		RECT rc = lpws->rectWindow;
		//得到客户矩形
        _CalcClientBound( hmem, &rc );
		lpws->rectClient = rc;
		DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b9.\r\n" ) );
		//发送 WM_CREATE 消息
        if( WinMsg_Send( hmem, WM_CREATE, 0, (LPARAM)lpcs ) == 0 )
        {
			DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b10.\r\n" ) );
            if( bVisible )
            {	//显示
				if( lpws->dwMainStyle & WS_CHILD )
                    Win_Show( hmem, SW_SHOWNORMAL );
				else
					Win_SetForeground( hmem );
            }
			DEBUGMSG( DEBUG_WIN_CREATEEX, ( "b11.\r\n" ) );
            return hmem;
        }
		else
		{
			WARNMSG( DEBUG_WIN_CREATEEX, ( "WM_CREATE: return !0.\r\n" ) );
		}
    }
	else
	{
		WARNMSG( DEBUG_WIN_CREATEEX, ( "WM_NCCREATE: return FALSE,lpClassName(%s).\r\n", (DWORD)lpClassName ) );
	}
	//失败
	WARNMSG( DEBUG_WIN_CREATEEX, ( "b12.\r\n" ) );
    Win_Destroy( hmem );
    hmem = NULL;
	ASSERT( 0 );
    return hmem;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假如成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

//static BOOL _DestroyPopupWindow( HWND hOwner, DWORD dwCurThreadID )
static BOOL _DestroyPopupWindow( _LPWINDATA lpwsOwner, DWORD dwCurThreadID, UINT uiFlag )
{
    
	//EnterCriticalSection( &csWindow );  //
	LockCSWindow();
	{
		_LPWINDATA lpwsPopup = hwndScreen.lpwsChild;//hwndScreen.hChild;//Win_Get( HWND_DESKTOP, GW_CHILD );
		_LPWINDATA lpwsTemp;
		//_LPWINDATA lpwsOwner = _GetHWNDPtr( hOwner );
		
		while( lpwsPopup && lpwsOwner->uOwnCount )
		{
			//_LPWINDATA lpws = _GetHWNDPtr( hwndPopup );
			if( lpwsPopup->lpwsOwner == lpwsOwner )
			{   // yes, destroy it
				lpwsTemp = lpwsPopup->lpwsNext;

				//LeaveCriticalSection( &csWindow );
				UnlockCSWindow();

				_Win_Destroy( lpwsPopup, dwCurThreadID, WDF_HIDE | uiFlag );//TRUE );
				
				//EnterCriticalSection( &csWindow );
				LockCSWindow();

				//lpwsPopuphwndPopup = hwndTemp;
				if( Win_IsWindow( lpwsTemp->hThis ) == FALSE )
					lpwsPopup = hwndScreen.lpwsChild;  // retry all
				else
					lpwsPopup = lpwsTemp;
			}
			else
				lpwsPopup = lpwsPopup->lpwsNext;
		}
		ASSERT( lpwsOwner->uOwnCount == 0 );
	}
	//LeaveCriticalSection( &csWindow );
	UnlockCSWindow();
    return TRUE;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假如成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

static BOOL _Win_Destroy( _LPWINDATA lpws, DWORD dwCurThreadID, UINT uiFlag )//BOOL bHide )
{
    {
		LPGWEDATA lpgwe;
		
		if( lpws->dwThreadID != dwCurThreadID )
		{
			ASSERT( lpws->dwThreadID != dwCurThreadID );
			return FALSE;
		}
		if( (uiFlag & WDF_CLEAR) == 0 )
		{
			WinMsg_Send( lpws->hThis, WM_DESTROY, 0, 0 );
			WinMsg_Send( lpws->hThis, WM_NCDESTROY, 0, 0 );
		}
		
			
		if( (uiFlag & WDF_HIDE ) && 
			IS_SHOWENABLE( lpws->dwMainStyle )  // 2003-08-05, 为 ShowDesktop修改
		  )
		{
			Win_SetPos( 
			    lpws->hThis, 
			    NULL,
			    0, 0, 0, 0,
			    SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE );
		}

        while( lpws->lpwsChild )
        {   // the parent is hided, not need hide child window again
            _Win_Destroy( lpws->lpwsChild, dwCurThreadID, (uiFlag & WDF_CLEAR) );
        }

        if( lpws->uOwnCount )
            _DestroyPopupWindow( lpws, dwCurThreadID, (uiFlag & WDF_CLEAR) );

        ASSERT( lpws->dwThreadID );
		lpgwe = GetGWEPtr( lpws->dwThreadID );

		if( lpgwe )
		{			
		    if( lpgwe->hwndActive == lpws->hThis )  //LN:2003-04-23,增加
			{
		        //Interlock_CompareExchange( &dwForegroundThreadID, 0, lpws->dwThreadID );//LN:2003-04-23,增加, //LN,2003-06-03, DEL
				ExchangeForegroundThreadId( 0, lpws->dwThreadID );//LN,2003-06-03, ADD
			}

			if( lpgwe->hwndFocus == lpws->hThis )
				lpgwe->hwndFocus = NULL;
			if( lpgwe->hwndActive == lpws->hThis )
				lpgwe->hwndActive = NULL;
			if( lpgwe->hwndCapture == lpws->hThis )
				lpgwe->hwndCapture = NULL;
		}

		//Interlock_CompareExchange( &dwForegroundThreadID, 0, lpws->dwThreadID );//LN:2003-04-23,删除

        if( (lpws->dwMainStyle & WS_POPUP) && lpws->lpwsOwner )
        {
			Interlock_Decrement( (LPLONG)&lpws->lpwsOwner->uOwnCount );
        }
        // clear message relate to hWnd
		LockCSWindow();
        Link_RemoveWindow( lpws );		
		UnlockCSWindow();
		DecClassRef( lpws->lpClass );
        _DeleteWinObject( lpws );		
        return TRUE;
    }
    return FALSE;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假如成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

BOOL WINAPI Win_Destroy( HWND hWnd )
{
	_LPWINDATA lpws;
	DWORD dwThreadID = GetCurrentThreadId();
	BOOL bRetv = FALSE;
	HWND hwndFore = NULL;//Win_GetForegroundWindow

	//EnterCriticalSection( &csWindow );

	lpws = _GetHWNDPtr( hWnd );
	if( lpws )
	{
		if( !(lpws->dwMainStyle & WS_CHILD) &&
			IS_SHOWENABLE( lpws->dwMainStyle ) && // 2003-08-05, 为 ShowDesktop修改
			!(lpws->dwMainStyle & WS_DISABLED) )
		{
			hwndFore = Win_GetForeground();
		}
		
		if( dwThreadID == lpws->dwThreadID )
		{
			DWORD dwStyle = lpws->dwMainStyle;
			HWND hwndDesktop = GetDesktopExplore();
			
			bRetv = _Win_Destroy( lpws, dwThreadID, WDF_HIDE );
			if( hwndDesktop != hWnd && !(dwStyle & WS_CHILD) )
			{
				AddMsgToThreadQueue( hwndDesktop, WM_SHELLNOTIFY, SN_SETFOREGROUNDWINDOW, 0, ATQ_ONLYONE | QS_POSTMESSAGE );
			}
		}
		if( hwndFore == hWnd )
		{   //重新设置新的前景窗口
			//EnterCriticalSection( &csWindow );
			LockCSWindow();
			
			lpws = hwndScreen.lpwsChild;
			while( lpws )
			{
				if( !(lpws->dwMainStyle & WS_CHILD) &&
					IS_SHOWENABLE( lpws->dwMainStyle ) && // 2003-08-05, 为 ShowDesktop修改
					!(lpws->dwMainStyle & WS_DISABLED) )
				{
					break;
				}
				lpws = lpws->lpwsNext;
			}
			
			//LeaveCriticalSection( &csWindow );
			UnlockCSWindow();
			if( lpws )
				Win_SetForeground( lpws->hThis );
		}
	}
	// LN, 2003.05.14-end
	return bRetv;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假如成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

BOOL WINAPI Win_Enable( HWND hWnd, BOOL bEnable )
{
    _LPWINDATA lpws = _GetHWNDPtr( hWnd );
    DWORD b = FALSE;
    
    if( lpws )
    {
		LPGWEDATA lpgwe = GetGWEPtr( lpws->dwThreadID );
		ASSERT( lpws->dwThreadID );
		if( lpgwe )
		{
			
			DWORD dwStyle = lpws->dwMainStyle;
			b = dwStyle & WS_DISABLED;
			if( bEnable )
				dwStyle &= ~WS_DISABLED;
			else
				dwStyle |= WS_DISABLED;
			//_SetState( lpws, WS_DISABLED, !bEnable );
			if( dwStyle != lpws->dwMainStyle )
			{
				//Win_SetLong( hWnd, GWL_STYLE, dwStyle );
				lpws->dwMainStyle = dwStyle;
				WinMsg_Send( hWnd, WM_NCPAINT, 1, NULL );

				if( bEnable == FALSE )
				{	
					//if( hwndActive && (hwndFocusWindow == hWnd || Win_IsChild( hWnd, hwndFocusWindow )) )
					if( lpgwe->hwndFocus == hWnd ||
						( lpgwe->hwndFocus && Win_IsChild( hWnd, lpgwe->hwndFocus ) ) )
						Win_SetFocus( NULL );
					if(  lpgwe->hwndActive == hWnd ||
						( lpgwe->hwndActive && Win_IsChild( hWnd, lpgwe->hwndActive ) ) )
						Win_SetActive( NULL );
					if( lpgwe->hwndCapture == hWnd || 
						( lpgwe->hwndCapture && Win_IsChild( hWnd, lpgwe->hwndCapture ) ) )
						Win_SetCapture( NULL );
				}
				WinMsg_Send( hWnd, WM_ENABLE, (WPARAM)bEnable, 0 );
			}
		}
    }
    return b;
}


⌨️ 快捷键说明

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