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

📄 mx.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		{
			mxEvent event;
			event.event = mxEvent::MouseUp;
			event.x = (int) LOWORD (lParam);
			event.y = (int) HIWORD (lParam);
			event.buttons = 0;
			event.modifiers = 0;

			if (uMessage == WM_MBUTTONUP)
				event.buttons |= mxEvent::MouseMiddleButton;
			else if (uMessage == WM_RBUTTONUP)
				event.buttons |= mxEvent::MouseRightButton;
			else
				event.buttons |= mxEvent::MouseLeftButton;

			if (wParam & MK_LBUTTON)
				event.buttons |= mxEvent::MouseLeftButton;

			if (wParam & MK_RBUTTON)
				event.buttons |= mxEvent::MouseRightButton;

			if (wParam & MK_MBUTTON)
				event.buttons |= mxEvent::MouseMiddleButton;

			if (wParam & MK_CONTROL)
				event.modifiers |= mxEvent::KeyCtrl;

			if (wParam & MK_SHIFT)
				event.modifiers |= mxEvent::KeyShift;

			window->handleEvent (&event);
		}
		bDragging = FALSE;
		ReleaseCapture ();
	}
	break;

	case WM_MOUSEMOVE:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;

			if (bDragging)
				event.event = mxEvent::MouseDrag;
			else
				event.event = mxEvent::MouseMove;

			event.x = (int) LOWORD (lParam);
			event.y = (int) HIWORD (lParam);
			event.buttons = 0;
			event.modifiers = 0;

			if (wParam & MK_LBUTTON)
				event.buttons |= mxEvent::MouseLeftButton;

			if (wParam & MK_RBUTTON)
				event.buttons |= mxEvent::MouseRightButton;

			if (wParam & MK_MBUTTON)
				event.buttons |= mxEvent::MouseMiddleButton;

			if (wParam & MK_CONTROL)
				event.modifiers |= mxEvent::KeyCtrl;

			if (wParam & MK_SHIFT)
				event.modifiers |= mxEvent::KeyShift;

			window->handleEvent (&event);
		}
	}
	break;
	case WM_NCLBUTTONDOWN:
	case WM_NCMBUTTONDOWN:
	case WM_NCRBUTTONDOWN:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);

		if (window)
		{
			mxEvent event;
			event.event = mxEvent::NCMouseDown;
			event.x = (int) LOWORD (lParam);
			event.y = (int) HIWORD (lParam);
			event.buttons = 0;
			event.modifiers = 0;

			if (uMessage == WM_NCMBUTTONDOWN)
				event.buttons |= mxEvent::MouseMiddleButton;
			else if (uMessage == WM_NCRBUTTONDOWN)
				event.buttons |= mxEvent::MouseRightButton;
			else
				event.buttons |= mxEvent::MouseLeftButton;

			window->handleEvent (&event);
		}
	}
	break;

	case WM_NCLBUTTONUP:
	case WM_NCMBUTTONUP:
	case WM_NCRBUTTONUP:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;
			event.event = mxEvent::NCMouseUp;
			event.x = (int) LOWORD (lParam);
			event.y = (int) HIWORD (lParam);
			event.buttons = 0;
			event.modifiers = 0;

			if (uMessage == WM_NCMBUTTONUP)
				event.buttons |= mxEvent::MouseMiddleButton;
			else if (uMessage == WM_NCRBUTTONUP)
				event.buttons |= mxEvent::MouseRightButton;
			else
				event.buttons |= mxEvent::MouseLeftButton;

			window->handleEvent (&event);
		}
	}
	break;

	case WM_NCMOUSEMOVE:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;

			event.event = mxEvent::NCMouseMove;

			event.x = (int) LOWORD (lParam);
			event.y = (int) HIWORD (lParam);
			event.buttons = 0;
			event.modifiers = 0;

			window->handleEvent (&event);
		}
	}
	break;

	case WM_KEYDOWN:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;
			event.event = mxEvent::KeyDown;
			event.key = (int) wParam;
			window->handleEvent (&event);
		}
	}
	break;

	case WM_CHAR:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;
			event.event = mxEvent::KeyDown;
			event.key = (int) wParam;
			window->handleEvent (&event);
		}
	}
	break;

	case WM_KEYUP:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;
			event.event = mxEvent::KeyUp;
			event.key = (int) wParam;
			window->handleEvent (&event);
		}
	}
	break;

	case WM_MOUSEWHEEL:
	{
		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;
			memset( &event, 0, sizeof( event ) );
			event.event = mxEvent::MouseWheeled;
			event.x = (int) LOWORD (lParam);
			event.y = (int) HIWORD (lParam);

			if (wParam & MK_LBUTTON)
				event.buttons |= mxEvent::MouseLeftButton;

			if (wParam & MK_RBUTTON)
				event.buttons |= mxEvent::MouseRightButton;

			if (wParam & MK_MBUTTON)
				event.buttons |= mxEvent::MouseMiddleButton;

			if (wParam & MK_CONTROL)
				event.modifiers |= mxEvent::KeyCtrl;

			if (wParam & MK_SHIFT)
				event.modifiers |= mxEvent::KeyShift;

			event.height = (short)HIWORD( wParam );;
			RecursiveHandleEvent( window, &event );
		}
	}
	break;
	case WM_TIMER:
	{
		if (isClosing)
			break;

		mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
		if (window)
		{
			mxEvent event;
			event.event = mxEvent::Timer;
			window->handleEvent (&event);
		}
	}
	break;

	case WM_CLOSE:
		if (g_mainWindow)
		{
			if ((void *) hwnd == g_mainWindow->getHandle ())
			{
				mx::quit ();
			}
			else
			{
				ShowWindow (hwnd, SW_HIDE);

				mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
				if (window)
				{
					mxEvent event;
					event.event = mxEvent::Close;
					window->handleEvent( &event );
				}
			}
		}
		//else // shouldn't happen
			//DestroyWindow (hwnd);
		return 0;
/*
	case WM_DESTROY:
		if (g_mainWindow)
		{
			if ((void *) hwnd == g_mainWindow->getHandle ())
				mx::quit ();
		}
		break;
*/
	}

	return DefWindowProc (hwnd, uMessage, wParam, lParam);
}



int
mx::init(int argc, char **argv)
{
	WNDCLASS wc;
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = (HINSTANCE) GetModuleHandle (NULL);
    wc.hIcon = LoadIcon (wc.hInstance, "MX_ICON");
	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "mx_class";

	if (!wc.hIcon)
		wc.hIcon = LoadIcon (NULL, IDI_WINLOGO);

	if (!RegisterClass (&wc))
		return 0;

	InitCommonControls ();

	g_widgetList = new mxLinkedList ();

	isClosing = false;

	return 1;
}



int
mx::run()
{
	int messagecount = 0;

	while (1)
	{
		bool doframe = false;
		if ( PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE) || !g_idleWindow )
		{
			if (!GetMessage (&msg, NULL, 0, 0))
			{
				doframe = false;
				break;
			}

			TranslateMessage (&msg);
			DispatchMessage (&msg);
			messagecount++;

			if ( messagecount > 10 )
			{
				messagecount = 0;
				doframe = true;
			}
		}
		else if (g_idleWindow)
		{
			doframe = true;
			messagecount = 0;
		}

		if ( doframe && g_idleWindow )
		{
			mxEvent event;
			event.event = mxEvent::Idle;
			g_idleWindow->handleEvent (&event);
		}
	}

	return msg.wParam;
}



int
mx::check ()
{
	if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
	{
		if (GetMessage (&msg, NULL, 0, 0))
		{
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}

		return 1;
	}

	return 0;
}



void
mx::quit ()
{
	isClosing = true;

	mxWindow *mainwnd = getMainWindow();
	if ( mainwnd )
	{
		if ( !mainwnd->Closing() )
		{
			isClosing = false;
			return;
		}
	}

	if (g_widgetList)
	{
		// remove from back to front
		mxListNode *node = g_widgetList->getLast ();

		// Pass 1, see if anyone objects to closing
		while (node)
		{
			mxWidget *widget = (mxWidget *) g_widgetList->getData (node);
			node = g_widgetList->getPrev (node);

			bool canclose = true;
			if ( widget )
			{
				if ( !widget->CanClose() )
				{
					canclose = false;
				}
			}

			if ( !canclose )
			{
				isClosing = false;
				return;
			}
		}

		node = g_widgetList->getLast ();

		// Pass 2, call OnDelete to allow final cleanup
		while (node)
		{
			mxWidget *widget = (mxWidget *) g_widgetList->getData (node);
			node = g_widgetList->getPrev (node);

			if ( widget )
			{
				widget->OnDelete();
			}
		}

		node = g_widgetList->getLast ();

		// Pass 3, delete stuff
		while (node)
		{
			mxWidget *widget = (mxWidget *) g_widgetList->getData (node);
			node = g_widgetList->getPrev (node);

			// remove it!
			if ( widget )
			{
				delete widget;
			}
		}

		delete g_widgetList;
	}

	if (g_hwndToolTipControl)
		DestroyWindow (g_hwndToolTipControl);

	PostQuitMessage (0);
	UnregisterClass ("mx_class", (HINSTANCE) GetModuleHandle (NULL));
}



int
mx::setDisplayMode (int w, int h, int bpp)
{
	DEVMODE dm;

	dm.dmSize = sizeof (DEVMODE);
	dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
	dm.dmBitsPerPel = bpp;
	dm.dmPelsWidth = w;
	dm.dmPelsHeight = h;

	if (w == 0 || h == 0 || bpp == 0)
		ChangeDisplaySettings (0, 0);
	else
		ChangeDisplaySettings (&dm, CDS_FULLSCREEN);

	return 0;
}



void
mx::setIdleWindow (mxWindow *window)
{
	g_idleWindow = window;
}



int
mx::getDisplayWidth ()
{
	return (int) GetSystemMetrics (SM_CXSCREEN);
}



int
mx::getDisplayHeight ()
{
	return (int) GetSystemMetrics (SM_CYSCREEN);
}



mxWindow*
mx::getMainWindow ()
{
	return g_mainWindow;
}



const char *
mx::getApplicationPath ()
{
	static char path[256];
	GetModuleFileName (0, path, 256);
	char *ptr = strrchr (path, '\\');
	if (ptr)
		*ptr = '\0';

	return path;
}



int
mx::getTickCount ()
{
	return (int) GetTickCount ();
}

⌨️ 快捷键说明

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