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

📄 wndbase.cpp

📁 WinCE下文字滚动的实例
💻 CPP
字号:
#include "stdafx.h"
#include "WndBase.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWndBase::CWndBase():
m_hInst(NULL),
m_hWnd(NULL),
m_hWndParent(NULL),
m_pszWndClass(NULL),
m_pszWndName(NULL),
m_bCreated(FALSE),
m_hEventCreated(NULL),
m_bMsgThrdInside(FALSE)
{

}

CWndBase::~CWndBase()
{
	if(m_pszWndClass != NULL)
	{
		delete []m_pszWndClass;
		m_pszWndClass = NULL;
	}

	if(m_pszWndName != NULL)
	{
		delete []m_pszWndName;
		m_pszWndName = NULL;
	}
}

//----------------------------------------------------------------------
//Description:
//	Create the window
//
//Parameters:
//	hInst : [in] The handle of instance of the application
//	hWndParent : [in] The parent window
//	pcszWndClass : [in] The class of the window
//	pcszWndName : [in] The name of the windows
//	bMsgThrdInside : [in] The message thread process is inside or not
//
//----------------------------------------------------------------------
BOOL CWndBase::Create(HINSTANCE hInst, HWND hWndParent, const TCHAR *pcszWndClass, const TCHAR *pcszWndName,BOOL bMsgThrdInside)
{
	m_hInst = hInst;
    m_hWndParent = hWndParent;
	m_bMsgThrdInside = bMsgThrdInside;

	//Store the window class name
	if(m_pszWndClass != NULL)
	{
		delete []m_pszWndClass;
		m_pszWndClass = NULL;
	}

	int iLen = _tcslen(pcszWndClass);
	m_pszWndClass = new TCHAR[iLen + 1];
	if(m_pszWndClass == NULL)
	{
		return FALSE;
	}
	_tcscpy(m_pszWndClass,pcszWndClass);



	//Store the window name
	if(m_pszWndName != NULL)
	{
		delete []m_pszWndName;
		m_pszWndName = NULL;
	}

	iLen = _tcslen(pcszWndName);
	m_pszWndName = new TCHAR[iLen + 1];
	if(m_pszWndName == NULL)
	{
		return FALSE;
	}
	_tcscpy(m_pszWndName,pcszWndName);


	//Create the window
	if(bMsgThrdInside == TRUE)
	{

		HANDLE hdThrd = CreateThread(NULL,0,CreateProc,this,0,NULL);
		if(hdThrd == NULL )
		{
			return FALSE;
		}
		else
		{
			CloseHandle(hdThrd);

			//Create the event and wait
			m_hEventCreated = CreateEvent(NULL,FALSE,FALSE,NULL);
			if(m_hEventCreated != NULL)
			{
				WaitForSingleObject(m_hEventCreated,INFINITE);
				CloseHandle(m_hEventCreated);
				m_hEventCreated = NULL;

				return m_bCreated;
			}
			else
			{
				return FALSE;
			}
			
		}
	}
	else
	{
		return CreateWnd();
	}


}

//----------------------------------------------------------------------
//Description:
//    Register window
//
//----------------------------------------------------------------------
BOOL CWndBase::RegisterWnd(HINSTANCE hInst,const TCHAR *pcszWndClass)
{	
	WNDCLASS wc = {0};
	wc.style         = 0;
    wc.lpfnWndProc   = (WNDPROC)CWndBase::StaticWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = m_hInst;
    wc.hIcon         = NULL; 
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = pcszWndClass;

	return RegisterClass(&wc);

	
}


//----------------------------------------------------------------------
//Description:
//    Static WndProc wrapper and actual WndProc
//
//----------------------------------------------------------------------
LRESULT CWndBase::StaticWndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
   CWndBase *pObject = (CWndBase*)GetWindowLong(hWnd, GWL_USERDATA);

    if(pObject)
    {
        return pObject->WndProc(hWnd,wMsg,wParam,lParam);
    }
    else
    {
        return DefWindowProc(hWnd,wMsg,wParam,lParam);
    }
}

//----------------------------------------------------------------------
//Description:
//    Actual WndProc
//
//----------------------------------------------------------------------
LRESULT CWndBase::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
	switch(wMsg)
	{
		case WM_DESTROY:
			OnDestroy(hWnd,wMsg,wParam,lParam);
			break;
	}
	return DefWindowProc(hWnd,wMsg,wParam,lParam);
}


//----------------------------------------------------------------------
//Description:
//    Show the window
//
//----------------------------------------------------------------------
BOOL CWndBase::ShowWindow(BOOL bShow)
{
    if(m_hWnd == NULL)
    {
        return FALSE;
    }

    if(bShow == TRUE)
    {
		SetForegroundWindow(m_hWnd);
		SetWindowPos(m_hWnd,HWND_TOP,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);			
    }
    else
    {
        ::ShowWindow(m_hWnd,SW_HIDE);
    }
    


    return TRUE;
}


//----------------------------------------------------------------------
//Description:
//    The process thread is for creating the window
//
//----------------------------------------------------------------------
DWORD CWndBase::CreateProc(PVOID pArg)
{
	//Get the object instance
	CWndBase *pObject = (CWndBase *)pArg;
	
	//Create the window
	pObject->m_bCreated = pObject->CreateWnd();

	//Set the event
	if(pObject->m_hEventCreated != NULL)
	{
		SetEvent(pObject->m_hEventCreated);
	}

	if(pObject->m_bCreated == FALSE)
	{
		//Failed in creating the window, so return and needn't the message loop
		return 0x01;
	}


	//The message loop	
	MSG msg;
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;

}



//----------------------------------------------------------------------
//Description:
//    Create the window
//
//----------------------------------------------------------------------
BOOL CWndBase::CreateWnd(void)
{	

    if(RegisterWnd(m_hInst,m_pszWndClass) == FALSE)
	{
		return FALSE;
	}

	RECT rcArea = {0};
	SystemParametersInfo(SPI_GETWORKAREA, 0, &rcArea, 0);

    m_hWnd = CreateWindowEx(0,
				m_pszWndClass,
				m_pszWndName,
                WS_TABSTOP,
                rcArea.left,
                rcArea.top,
                rcArea.right - rcArea.left,
                rcArea.bottom - rcArea.top,
                m_hWndParent, 
                NULL, 
                m_hInst, 
                0);
    
    if (IsWindow(m_hWnd) == FALSE)
    {
        return FALSE;
    }

    // If the window is created successfully, store this object so the 
    //static wrapper can pass calls to the real WndProc.
    SetWindowLong(m_hWnd, GWL_USERDATA, (DWORD)this);

	return TRUE;
}

//----------------------------------------------------------------------
//Description:
//    On Message WM_DESTROY
//
//----------------------------------------------------------------------
void CWndBase::OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{

	if(m_bMsgThrdInside == TRUE)
	{
		//Exit the inside thread
		PostQuitMessage(0x00);
	}


}


//----------------------------------------------------------------------
//Description:
//    Get the instance
//
//----------------------------------------------------------------------
HINSTANCE CWndBase::GetHinstance(void)
{
	return m_hInst;
}

//----------------------------------------------------------------------
//Description:
//    Get the window handle
//
//----------------------------------------------------------------------
HWND CWndBase::GetWindow(void)
{
	return m_hWnd;
}

//----------------------------------------------------------------------
//Description:
//    Get the window parent
//
//----------------------------------------------------------------------
HWND CWndBase::GetWindowParent(void)
{
	return m_hWndParent;
}


//----------------------------------------------------------------------
//Description:
//    Set the parent window
//
//----------------------------------------------------------------------
BOOL CWndBase::SetParent(HWND hWndParent)
{
	m_hWndParent = hWndParent;
	return (::SetParent(m_hWnd,m_hWndParent) != NULL);
}

⌨️ 快捷键说明

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