vt100.cpp

来自「vt100终端仿真程序」· C++ 代码 · 共 497 行

CPP
497
字号
// VT100.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

#ifdef CHECK
#include <time.h>
#endif

#include "screen.h"

#include "vt100imp.h"
#include "commctrl.h"
#include "vt100disp.h"
#include "cursor.h"

//***************define for vt100******************
#define WM_DATAARRIVAL (WM_USER+1)
#define WM_COMMERROR	(WM_USER+2)

HWND hOutWnd;
HDC hDC;
PAINTSTRUCT ps;

int CheckAuth();
int CheckDate();

extern struct  struScreen struScrMsp; //??
extern HFONT testFont4;
extern HFONT testUlineFont4;
extern int iWidth;
//*************************************************

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			hInst;			// The current instance
HWND				hwndCB;			// The command bar handle

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	MSG msg;
	HACCEL hAccelTable;

#ifdef CHECK
	if(CheckAuth()!=0){
		MessageBox(NULL,"请使用安装盘正确安装应用软件","",MB_OK);
		return 0;
	}
		
	if(CheckDate()!=0){
		MessageBox(NULL,"您所使用的软件已过期,请与开发商联系","",MB_OK);
		return 0;
	}
#endif

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_VT100);
	
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    It is important to call this function so that the application 
//    will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name

	hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_VT100, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance, szWindowClass);

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
//	hWnd = CreateWindowEx(WS_EX_TOPMOST,szWindowClass, szTitle, WS_VISIBLE,
//		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{	
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	if (hwndCB)
		CommandBar_Show(hwndCB, TRUE);

	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
//	PAINTSTRUCT ps;
//	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	RECT WndRect;
	int nWidth;

	hOutWnd=hWnd;

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;

				case IDM_COM:
				   DialogBox(hInst, (LPCTSTR)IDD_CONFIG, hWnd, (DLGPROC)ComCfg);
				   break;

				case IDM_TERMPARM:
				   DialogBox(hInst, (LPCTSTR)IDD_PARASET, hWnd, (DLGPROC)TermCfg);
				   break;

				case IDM_KEY:
				   DialogBox(hInst, (LPCTSTR)IDD_DIALOGKEY, hWnd, (DLGPROC)KeySet);
				   break;

				case IDM_ONLINE:
					Online();
					break;

				case IDM_OFFLINE:
					Offline();
					break;

				case IDM_PRT:
				   DialogBox(hInst, (LPCTSTR)IDD_PRT, hWnd, (DLGPROC)CfgPrt);
				   break;

				case IDM_LOGON:
					DialogBox(hInst, (LPCTSTR)IDD_LOGON, hWnd, (DLGPROC)LogOn);
					break;			

				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;

		case WM_DATAARRIVAL:
			OnDataArrival(wParam,lParam);
			break;

		case WM_COMMERROR:
			OnCommError(wParam,lParam);
			break;

		case WM_PAINT:
			hDC=BeginPaint(hWnd,&ps);
			GetClientRect(hWnd,&WndRect);
			nWidth=WndRect.right-WndRect.left;

			if (nWidth<700)
			{
				if (iWidth==80)
				{
					struScrMsp.iChrWidth=9;//nWidth/80;	
				}
				else
				{
					struScrMsp.iChrWidth=4;//nWidth/132;	
				}
				struScrMsp.iChrHigh=18;//struScrMsp.iChrWidth*2;
			}
			else
			{
				if (nWidth < 1000)
				{
					if (iWidth==80)
					{
						struScrMsp.iChrWidth=10;//nWidth/80;	
					}
					else
					{
						struScrMsp.iChrWidth=6;//nWidth/132;	
					}
					struScrMsp.iChrHigh=20;//struScrMsp.iChrWidth*2;
				}
				else
				{
					if (iWidth==80)
					{
						struScrMsp.iChrWidth=11;//nWidth/80;	
					}
					else
					{
						struScrMsp.iChrWidth=7;//nWidth/132;	
					}
					struScrMsp.iChrHigh=22;//struScrMsp.iChrWidth*2;
				}
			}

			LOGFONT hNormal,hUnderLine;

			hNormal.lfHeight= struScrMsp.iChrHigh;
			hNormal.lfWidth= struScrMsp.iChrWidth;
			hNormal.lfEscapement=0;
			hNormal.lfOrientation=0;
			hNormal.lfWeight=400;
			hNormal.lfItalic=FALSE;
			hNormal.lfUnderline=FALSE;
			hNormal.lfStrikeOut=0;
			hNormal.lfCharSet=DEFAULT_CHARSET ;//GB2312_CHARSET;
			hNormal.lfOutPrecision= OUT_DEFAULT_PRECIS;
			hNormal.lfClipPrecision= CLIP_DEFAULT_PRECIS;
			hNormal.lfQuality= DEFAULT_QUALITY;
			hNormal.lfPitchAndFamily =DEFAULT_PITCH|FF_MODERN;	
#ifdef UNICODE			
			wcscpy(hNormal.lfFaceName,_T("COUR"));
#else
			strcpy(hNormal.lfFaceName,"COUR");
#endif			
			testFont4=CreateFontIndirect(&hNormal);

			hUnderLine.lfHeight= struScrMsp.iChrHigh;
			hUnderLine.lfWidth= struScrMsp.iChrWidth;
			hUnderLine.lfEscapement=0;
			hUnderLine.lfOrientation=0;
			hUnderLine.lfWeight=400;
			hUnderLine.lfItalic=FALSE;
			hUnderLine.lfUnderline=TRUE;
			hUnderLine.lfStrikeOut=0;
			hUnderLine.lfCharSet=DEFAULT_CHARSET ;//GB2312_CHARSET;
			hUnderLine.lfOutPrecision= OUT_DEFAULT_PRECIS;
			hUnderLine.lfClipPrecision= CLIP_DEFAULT_PRECIS;
			hUnderLine.lfQuality= DEFAULT_QUALITY;
			hUnderLine.lfPitchAndFamily =DEFAULT_PITCH|FF_MODERN;
#ifdef UNICODE			
			wcscpy(hUnderLine.lfFaceName,_T("COUR"));
#else
			strcpy(hUnderLine.lfFaceName,"COUR");
#endif			
			testUlineFont4=CreateFontIndirect(&hUnderLine);

//			CursorMoveTo(struScrMsp.iXPos,struScrMsp.iYPos);
//			SetCursorHide();
//			SetCursorDisplay();

			OnDraw();

			EndPaint(hWnd,&ps);

			break;

		case WM_CREATE:
			hwndCB = CommandBar_Create(hInst, hWnd,IDR_TOOLBAR2);			
			CommandBar_InsertMenubar(hwndCB, hInst,IDC_VT100, 0);
			CommandBar_AddAdornments(hwndCB, 0, 0);

			DialogBox(hInst, (LPCTSTR)IDD_LOGO, hWnd, (DLGPROC)Logo);
		
			OnCreateWnd();

			break;

		case WM_CHAR:
			OnChar(wParam,lParam);
			break;

		case WM_KEYDOWN:
			OnKeyDown(wParam,lParam);
			break;

		case WM_DESTROY:
			//release resource
			OnReleaseWnd();

			CommandBar_Destroy(hwndCB);
			PostQuitMessage(0);

			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

#ifdef CHECK

int CheckAuth()
{
	HKEY hKey=NULL;
	char szSubKey[]="SOFTWARE\\DBDATA\\JOB";
	HKEY hKeyResult;
	unsigned char szData[20];
	DWORD dwType,dwDataLen;

	//VERIFY AUTH
	hKey=HKEY_LOCAL_MACHINE;
	if (RegOpenKeyEx(hKey,szSubKey,0,KEY_ALL_ACCESS,&hKeyResult)==ERROR_SUCCESS)
	{
		memset(szData,0,sizeof(szData));
		dwDataLen=10;
		RegQueryValueEx(hKeyResult,"NAME",NULL,&dwType,szData,&dwDataLen);
		RegCloseKey(hKey);

		if(dwDataLen!=8){
			MessageBox(NULL,"软件安装错误,请重新安装","Error",MB_OK);
			return FALSE;
		}

		if(szData[0]!=168){
			MessageBox(NULL,"软件安装错误,请重新安装","Error",MB_OK);
			return FALSE;
		}
	}
	else
	{
		MessageBox(NULL,"软件安装错误,请重新安装","Error",MB_OK);
		return FALSE;		
	}
	//END AUTH CHECK

	return 0;
}

int CheckDate()
{
	HKEY hKey=NULL;
	char szSubKey[]="SOFTWARE\\DBDATA\\TYPE";
	HKEY hKeyResult;
	unsigned char szData[5];
	DWORD dwDataLen;
	DWORD dwType=REG_SZ;
	int iRegPassDay,iCurPassDay,iRet;
	time_t tmCurTime;
	struct tm *tmSysTime;

	//VERIFY AUTH
	hKey=HKEY_LOCAL_MACHINE;
	if (RegOpenKeyExA(hKey,szSubKey,0,KEY_ALL_ACCESS,&hKeyResult)==ERROR_SUCCESS)
	{
		memset(szData,0,sizeof(szData));
		dwDataLen=4;
		RegQueryValueEx(hKeyResult,"OS",NULL,&dwType,szData,&dwDataLen);
		
		iRegPassDay=(szData[0]-48)*10+szData[1]-48;
		if(iRegPassDay==99)
			return 1;

		if(iRegPassDay==88)
			return 0;

		time(&tmCurTime);
		tmSysTime=gmtime(&tmCurTime);

		if(tmSysTime->tm_year!=101){
			iRet=1;
			iCurPassDay=99;
			goto finished;
		}
		
		if(tmSysTime->tm_mon<6||tmSysTime->tm_mon>8){
			iRet=1;
			iCurPassDay=99;
			goto finished;
		}

		switch(tmSysTime->tm_mon){
		case 6:
			iCurPassDay=tmSysTime->tm_mday-15;
			if(iCurPassDay<0||(iCurPassDay-iRegPassDay)<0){
				iRet=1;
				iCurPassDay=99;
			}
			else{
				iRet=0;
			}

			break;

		case 7:
			iCurPassDay=tmSysTime->tm_mday+15;
			if((iCurPassDay-iRegPassDay)<0){
				iRet=1;
				iCurPassDay=99;
			}
			else
				iRet=0;

			break;

		case 8:
			iCurPassDay=tmSysTime->tm_mday+15+30;
			if(tmSysTime->tm_mday>29||(iCurPassDay-iRegPassDay)<0){
				iRet=1;
				iCurPassDay=99;
			}
			else{
				iRet=0;
			}

			break;
		}	
	}
	else{
		MessageBox(NULL,"软件安装错误,请重新安装","Error",MB_OK);
		return 1;		
	}
	//END AUTH CHECK

finished:

	sprintf((char *)szData,"%02d",iCurPassDay);
	RegSetValueEx(hKeyResult,"OS",0,dwType,szData,2);
	
	RegCloseKey(hKey);

	return iRet;
}

#endif

⌨️ 快捷键说明

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