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

📄 pi3win.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/EnhPi3/Pi3Win.cpp,v $
 * $Date: 2003/05/13 18:41:58 $
 *
 Description:

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <assert.h>
#include <windowsx.h>
#include <windows.h>
#include <stdio.h>

#include "Common.h"
#include "resrc1.h"

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
#define MYWM_NOTIFYICON		(WM_APP+101)
static HINSTANCE __hInstance = 0;
static void (* __fnOnWindowingThreadTermination)( void * ) = 0;
static void *__pWindowingThreadTerminationData = 0;
static int iHaveSystray = 0;
static HWND __hStatusDialog = 0;
static HMENU __hPopup = 0;
static const char **__ppArgv;
enum { INFO_BUF=1023 };
static char szURL[INFO_BUF+1];
static char szAdminPath[INFO_BUF];
#define STATUS_DESCRIPTION	"Pi3Web Server"

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
BOOL Internal_SendTrayMessage(
		HWND hDlg,
		DWORD dwMessage,
		UINT uID,
		HICON hIcon,
		PSTR pszTip )
{
	BOOL bResult;
	NOTIFYICONDATA tND;
	tND.cbSize		= sizeof(NOTIFYICONDATA);
	tND.hWnd		= hDlg;
	tND.uID			= uID;
	tND.uFlags		= NIF_MESSAGE|NIF_ICON|NIF_TIP;
	tND.uCallbackMessage	= MYWM_NOTIFYICON;
	tND.hIcon		= hIcon;
	if (pszTip)
		{ strncpy(tND.szTip, pszTip, sizeof(tND.szTip)); }
	else
		{ tND.szTip[0] = '\0'; };

	bResult = Shell_NotifyIcon(dwMessage, &tND);

	if (hIcon)
		{ DestroyIcon(hIcon); };

	return bResult;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpDI)
{
	HICON hIcon;

	hIcon = (HICON)LoadImage(__hInstance, MAKEINTRESOURCE(lpDI->CtlID),
		IMAGE_ICON, 16, 16, 0);
	if (!hIcon)
		{
		return FALSE;
		}

	DrawIconEx(lpDI->hDC, lpDI->rcItem.left, lpDI->rcItem.top, hIcon,
		16, 16, 0, NULL, DI_NORMAL);

	return TRUE;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
BOOL Internal_initIcon( HWND hDlg )
{
	if ( !iHaveSystray )
		{
		/* --- show minimized icon --- */
		::ShowWindow( hDlg, SW_MINIMIZE );
		return 1;
		};

	/* --- else --- */
	BOOL bRet = Internal_SendTrayMessage(hDlg, NIM_ADD, IDI_PI3WEB, 
			NULL, NULL);

	if ( bRet )
		{
		bRet &= Internal_SendTrayMessage(hDlg, NIM_MODIFY, IDI_PI3WEB, 
			(HICON)LoadImage(__hInstance, 
				MAKEINTRESOURCE( IDI_PI3WEB ),
				IMAGE_ICON, 16, 16, 0),
				STATUS_DESCRIPTION );

		/* --- initialise popup menu --- */ 
		HMENU hMenu = LoadMenu( __hInstance, MAKEINTRESOURCE(IDR_MENU ) );
		__hPopup = GetSubMenu( hMenu, 0 );
		};

	return bRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
BOOL Internal_removeIcon()
{
	BOOL bRet; 

	if ( iHaveSystray )
		{
		bRet = Internal_SendTrayMessage(__hStatusDialog, 
			NIM_DELETE, IDI_PI3WEB, NULL, NULL);
		}
	else { return TRUE; };

	return bRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
BOOL CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
		{
		case WM_INITDIALOG:
			{
			__hStatusDialog = hDlg;
			if ( !Internal_initIcon( hDlg ) )
				{
				::SetTimer( hDlg, 0, 10000, 0 );
				};
			HWND hWndURL = ::GetDlgItem( hDlg, IDC_URL );
			if ( hWndURL )
				{ ::SendMessage( hWndURL, WM_SETTEXT, 0, (LPARAM)szURL ); };
			};
			break;

		case WM_TIMER:
			if ( Internal_initIcon( hDlg ) )
				{
				::KillTimer( hDlg, 0 );
				};
			break;

		case WM_DRAWITEM:
			if ( iHaveSystray )
				{ return IconDrawItem( (LPDRAWITEMSTRUCT)lParam ); };
			break;

		case WM_DESTROY:
			Pi3_endWindowing();	
			break;

		case WM_COMMAND:
			switch (GET_WM_COMMAND_ID(wParam, lParam))
				{
				case IDM_SHUTDOWN:
					DestroyWindow( hDlg );
					break; 	

				case IDABORT:
					if ( !iHaveSystray )
						{
						ShowWindow(hDlg, SW_MINIMIZE);
						}
					else
						{
						ShowWindow(hDlg, SW_HIDE);
						};
					break;

				case IDM_HOMEPAGE:
					if ( !CTRL_openURL( NULL, szURL ) )
						{ 
						MessageBox( hDlg, "Could not open home page", 
							"Open Home Page", MB_OK );
						};
					break;

				case IDM_PROPERTIES:
					{
					if ( !CTRL_startPi3( szAdminPath, 0 ) )
						{
						/* --- Error --- */
						MessageBox( hDlg, "Could not open properties", "Error",
							MB_OK );
						};
					};
					break;
				}
			break;


		case MYWM_NOTIFYICON:
			switch (lParam)
				{
				/*
				** Make dialog come to the front
				*/
				case WM_LBUTTONDOWN:
					ShowWindow(hDlg, SW_SHOW);
					SetForegroundWindow(hDlg);
					break;

				case WM_RBUTTONUP:
					/*	
					** Popup menu
					*/
					{
					POINT tPt;
					GetCursorPos( &tPt );
					TrackPopupMenu(
						__hPopup,
						TPM_RIGHTBUTTON | TPM_LEFTALIGN,	
						tPt.x, 	
						tPt.y, 
						NULL,	
						hDlg, 	
						NULL );	
					}
					break;

				default:
					break;
				}
			break;

		default:
				return FALSE;
		};

	return TRUE;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Windowing loop with modeless dialog and tray icon.
\*____________________________________________________________________________*/
int WindowsLoop()
{
	MSG tMsg;
	OSVERSIONINFO tOS;

	/* ---
	Check support for systray
	--- */
	tOS.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
	if ( GetVersionEx( &tOS )==0 )
		{
		return -1;
		};
	if ( tOS.dwMajorVersion>=4 )
		{
		/* ---
		Windows 95 or NT4.0 - can do tray icons.
		--- */
		iHaveSystray = 1;
		}
	else
		{
		iHaveSystray = 0;
		};

	/*
	** Register URL class
	*/
	AD_registerURLClass( __hInstance );

	/* ---
	Create modeless dialog box
	--- */
	if ( !CreateDialog( __hInstance,
		MAKEINTRESOURCE(IDD_PI3WEB), NULL, (DLGPROC)DlgProc) )
		{ goto error; };

	/* ---
	Wait for end of dialog
	--- */
	while( GetMessage( &tMsg, NULL, 0, 0 ) )
		{
		TranslateMessage( &tMsg );
		DispatchMessage( &tMsg );
		};

	/*
	** Unregister URL class
	*/
	AD_unregisterURLClass( __hInstance );

	return 0;

error:
	AD_unregisterURLClass( __hInstance );
	return -1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
DWORD WINAPI Internal_windowingThread( void *pDummy )
{
	WindowsLoop();

	(__fnOnWindowingThreadTermination)( __pWindowingThreadTerminationData );

	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Create icons, menus etc for Pi3 application, also start a thread to 
	do the message loop.

	Returns 1 on success, otherwise 0.
\*____________________________________________________________________________*/
int Pi3_startWindowing( HINSTANCE hInstance, const char *ppArgv[],
	void (* fnOnTermination )( void * ), void *pTerminationData )
{ 
	DWORD dwThreadId;
	HANDLE hThread;

	__ppArgv = ppArgv;
	__hInstance = hInstance;
	__fnOnWindowingThreadTermination=fnOnTermination;
	__pWindowingThreadTerminationData=pTerminationData;

	const char *pURL = AD_lookupValue( __pConfig, "Identity", "URL" );
	strncpy( szURL, pURL ? pURL : "", INFO_BUF );
	szURL[INFO_BUF] = '\0';
	const char *pExe = AD_lookupValue( __pConfig, "General", "ExePath" );
	const char *pConfig = AD_lookupValue( __pConfig, "Startup", "ConfigName" );
	sprintf( szAdminPath, "\"%s\" /ADMIN %s", 
		pExe ? pExe : "",  pConfig ? pConfig : "" ); 

	/*
	** Create the windowing thread
	*/
	hThread=CreateThread(
		NULL,						/*	security attributes			*/
    	0,							/*	initial thread stack size, in bytes  */
		Internal_windowingThread,	/*	pointer to thread function	*/
		0,							/*	argument for new thread		*/ 
		0,							/*	creation flags				*/
		&dwThreadId					/*	pointer to returned thread identifier */
		);
	
	if ( hThread==NULL )
		{
		return 0;
		}
	else
		{
		/* --- success, but we don't need the thread handle --- */
		CloseHandle( hThread );
		return 1;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void Pi3_endWindowing()
{
	if ( !__hStatusDialog )
		{ return; /* already stopped */ };

	Internal_removeIcon();

	__hStatusDialog = 0;
	PostQuitMessage( 0 );	
};


⌨️ 快捷键说明

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