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

📄 win32window.cpp

📁 game code对于游戏地图的编辑代码
💻 CPP
字号:
#include <stdarg.h>
#include "../../Main/QString.h"
#include "win32window.h"

Win32Window::Win32Window(void):
						mbActive(false),
						mbReady(false),
						mbClosed(false),
						mHWnd (0),
						mParentHWnd(0),
						mbCanTurnToFullscreen(false)
{
}

Win32Window::~Win32Window(void)
{
	Destroy();
}

LRESULT CALLBACK WndProcHelper(HWND hWnd,
							   UINT uMsg,
							   WPARAM wParam,
							   LPARAM lParam )
{
	return Win32Window::WndProc(hWnd,uMsg,wParam,lParam);
}

void Win32Window::Create(const String& name,Bool bwindowed,
			Int32 width,Int32 height,Int32 left,Int32 top,void* miscParam,...)
{
	//Get Instance & parentHWnd
	HWND parentHWND;
	Int32 tmpPtr;

	va_list marker;
	va_start(marker,top);

	tmpPtr = va_arg(marker,Int32);		// HINSTANCE
	mhInst  = *(HINSTANCE*)tmpPtr;

	tmpPtr =va_arg(marker,Int32);		// Parent Window
	Win32Window* pwnd = (Win32Window*)tmpPtr;
	if( pwnd == 0 )
		parentHWND = 0;
	else
		parentHWND = pwnd->getWindowHandle();

	va_end(marker);

	mWidth = width;
	mHeight = height;
	if ( bwindowed )
	{
		if (!left && GetSystemMetrics(SM_CXSCREEN) > mWidth)
			mLeft = (GetSystemMetrics(SM_CXSCREEN) / 2) - (mWidth / 2);
		else
			mLeft = left;
		if (!top && GetSystemMetrics(SM_CYSCREEN) > mHeight)
			mTop = (GetSystemMetrics(SM_CYSCREEN) / 2) - (mHeight / 2);
		else
			mTop = top;
	}
	else
		mTop = mLeft = 0;

	// Register the window class
	WNDCLASS wndClass = { CS_HREDRAW | CS_VREDRAW, WndProcHelper, 0, 4, mhInst,
		LoadIcon( NULL, "IDI_ICON1" ),
		LoadCursor( NULL, IDC_ARROW ),
		(HBRUSH)GetStockObject( BLACK_BRUSH ), NULL,
		TEXT(name.c_str()) };
	RegisterClass( &wndClass );

	// Create our main window
	// Pass pointer to self
	HWND hWnd = CreateWindow(TEXT(name.c_str()),
		TEXT(name.c_str()),
		WS_OVERLAPPEDWINDOW, mLeft, mTop,
		mWidth, mHeight, 0L, 0L, mhInst, this);
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	// Store info
	mHWnd = hWnd;
	mName = name;
	mbFullScreen = !bwindowed;
	mbCanTurnToFullscreen = true;
}

void Win32Window::Create(const String& name,HWND hWnd)
{
	mName = name;
	mHWnd = hWnd;
	RECT rect;
	GetWindowRect(hWnd,&rect);
	mWidth = rect.right - rect.left;
	mHeight = rect.bottom - rect.top;
	mLeft = rect.left;
	mTop  = rect.top;
	mbFullScreen = false;
	SetWindowLong(hWnd,0,(Int32)this);
	mbActive = true;
	mbCanTurnToFullscreen = false;
}

void Win32Window::Destroy(void)
{
	DestroyWindow(mHWnd);
	UnregisterClass(mName.c_str(),mhInst);
}

Bool Win32Window::TurnToFullScreen()
{
	if( !mbCanTurnToFullscreen )
		return false;
    if( mbFullScreen )    
		return true;
    SetWindowLong( mHWnd, GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE );
	mbFullScreen = true;
	return true;
}

Bool Win32Window::isActive()
{
	return mbActive;
}
Bool Win32Window::isClosed()
{
	return mbClosed;
}
Bool Win32Window::isFullScreen()
{
	return mbFullScreen;
}

void Win32Window::Resize(Int32 newWidth,Int32 newHeight)
{
	mWidth = newWidth;
	mHeight= newHeight;
	SetWindowPos(mHWnd,HWND_NOTOPMOST,0,0,mWidth,mHeight,SWP_NOMOVE|SWP_NOZORDER);
}
void Win32Window::MoveTo(Int32 newleft,Int32 newtop)
{
}
void Win32Window::WindowMovedOrResized()
{
}
LRESULT Win32Window::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	LPCREATESTRUCT lpcs;
	Win32Window* win;

	// look up window instance
	if( WM_CREATE != uMsg )
		// Get window pointer
		win = (Win32Window*)GetWindowLong( hWnd, 0 );

	switch( uMsg )
	{
	case WM_ACTIVATE:
		if( WA_INACTIVE == LOWORD( wParam ) )
			win->mbActive = false;
		else
			win->mbActive = true;
		break;

	case WM_CREATE:
		// Log the new window
		// Get CREATESTRUCT
		lpcs = (LPCREATESTRUCT)lParam;
		win = (Win32Window*)(lpcs->lpCreateParams);
		// Store pointer in window user data area
		SetWindowLong( hWnd, 0, (Int32)win );
		win->mbActive = true;

		return 0;
		break;

	case WM_KEYDOWN:
		// TEMPORARY CODE
		// TODO - queue up keydown / keyup events with
		//  window name and timestamp to be processed
		//  by main loop

		// ESCAPE closes window
		if (wParam == VK_ESCAPE)
		{
			PostMessage(win->getWindowHandle(),WM_CLOSE,0L,0L);
		}
		break;

	case WM_PAINT:
		// If we get WM_PAINT messges, it usually means our window was
		// comvered up, so we need to refresh it by re-showing the contents
		// of the current frame.
		if( win->mbActive && win->mbReady )
			win->Update();
		break;

	case WM_MOVE:
		// Move messages need to be tracked to update the screen rects
		// used for blitting the backbuffer to the primary
		// *** This doesn't need to be used to Direct3D9 ***
		break;

	case WM_ENTERSIZEMOVE:
		// Previent rendering while moving / sizing
		win->mbReady = false;
		break;

	case WM_EXITSIZEMOVE:
		win->WindowMovedOrResized();
		win->mbReady = true;
		break;

	case WM_SIZE:
		// Check to see if we are losing or gaining our window.  Set the 
		// active flag to match
		if( SIZE_MAXHIDE == wParam || SIZE_MINIMIZED == wParam )
			win->mbActive = false;
		else
		{
			win->mbActive = true;
			if( win->mbReady )
				win->WindowMovedOrResized();
		}
		break;

	case WM_GETMINMAXINFO:
		// Prevent the window from going smaller than some minimu size
		((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
		((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
		break;

	case WM_CLOSE:
		DestroyWindow( win->mHWnd );
		win->mbClosed = true;
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

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