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

📄 main.cpp

📁 jedit 一个小型java编译器的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************\
*
*  文件名:    main.cpp
*  目的:	  
*  使用说明:  在保持此程序完整性的情况下,你可以任意传播它。你也可以修改它
*			  来为你服务,如果你要将此程序用于商业目的,请告知我
*
*  版权所有	 2002  张元一
*
\******************************************************************************/
#include<windows.h>
#include<tchar.h>
#include<shlobj.h>

#include<string>
#include"resource.h"
#include"interface.h"	  
#include"MDIWnd.h"   
#include"iniload.h" 

#define ID_STATUSBAR	20000
#define WM_SAVEFILE	20001

char JDKPath[MAX_PATH]="";
char APPPath[MAX_PATH]="";
char InitDir[MAX_PATH]="";
char ClassPath[MAX_PATH]="";

HWND         hwndClient ;
HWND		 hStatusBar;
DockCtrl DC;
set<MDIWnd> MDIWndContainer;
set<MDIWnd> WorkSpace;


FEdit *fdOutput;

//全局变量
HINSTANCE hInst;				
HWND Mainhwnd;
HWND hwndFrame;

HWND hwndList;
//函数声明
LRESULT CALLBACK FrameWndProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildWndProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK NewWndProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK SettingProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK OutputProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK WorkSpaceProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ReplaceProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK GotoLineProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK FindProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK AboutProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);



void CreateMDIChild(char* filename);
JavaEdit* GetJEDIT(HWND hwnd);
void InitStatusBar(HWND hwnd);
void CompileProg(char *filename);
void RunProg(char *filename,bool=false);

bool ShowOpenDialog(HWND hwnd,char* filename,char *filter);
bool ShowSaveDialog(HWND hwnd,char* filename);

void GetWndRect(RECT *rect);
bool WriteFile(char* pathname,char* filename,HWND hwnd);
MDIWnd*	  GetMDIWnd(HWND hwnd);
int WINAPI WinMain (HINSTANCE hInstance,
					HINSTANCE,
                    PSTR szCmdLine, 
					int iCmdShow)
{
     HWND        hwndFrame, hwndClient ;
     MSG         Msg ;
	 HACCEL		 hAccel;
     WNDCLASSEX  wndclass ;

	 hInst=hInstance;

     // 注册框架窗口类

     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = FrameWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON)) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1) ;
     wndclass.lpszMenuName  = MAKEINTRESOURCE(IDM_MAIN);
     wndclass.lpszClassName = _T("MdiFrame");
     wndclass.hIconSm       = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON)) ;

     RegisterClassEx (&wndclass) ;

     // 注册子窗口类

     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = ChildWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof (HANDLE) ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (hInstance,MAKEINTRESOURCE(IDI_ICONJAVA)) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = _T("ChildClass");
     wndclass.hIconSm       = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICONJAVA)) ;

     RegisterClassEx (&wndclass) ;
     // 创建框架窗口

     hwndFrame = CreateWindowEx (WS_EX_CLIENTEDGE,_T("MdiFrame"), _T("JEdit"),
                               WS_CAPTION|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SYSMENU| WS_CLIPCHILDREN|WS_THICKFRAME,
                               0, 0,
                               GetSystemMetrics(SM_CXSCREEN),
							   GetSystemMetrics(SM_CYSCREEN)-30,
                               NULL, NULL, hInstance, NULL) ;

    hwndClient = GetWindow (hwndFrame, GW_CHILD) ;

    ShowWindow (hwndFrame, iCmdShow) ;
    UpdateWindow (hwndFrame) ;
	Mainhwnd=hwndFrame;

	IniLoad il;
	il.Load("ini\\javaconf.ini");
	string s;
	il.Get("javahome",s);
	strcpy(JDKPath,s.c_str());
	il.Get("bgcolor",s);
	JavaEdit::hBgColor=atoi(s.c_str());
	il.Get("textcolor",s);
	JavaEdit::hTextColor=atoi(s.c_str());
	il.Get("keywordcolor",s);
	JavaEdit::KeywordColor=atoi(s.c_str());
	il.Get("commentcolor",s);
	JavaEdit::CommentColor=atoi(s.c_str());
	il.Get("initdir",s);
	strcpy(InitDir,s.c_str());
	il.Get("classpath",s);
	strcpy(ClassPath,s.c_str());
	il.Get("bgimage",s);
	strcpy(JavaEdit::BGFile,s.c_str());

	GetCurrentDirectory(MAX_PATH,APPPath);

	RECT CRect={0,0,0,22};
	DC.CreateEx(hInst,hwndFrame,CRect,0);
	DC.ShowWindow(SW_SHOW);
	DC.UpdateWindow();
	DC.Add(hInst,"输出",0,GetSystemMetrics(SM_CYSCREEN)-102,GetSystemMetrics(SM_CXSCREEN),200,DOCK_BOTTOM,DOCK_BOTTOM,OutputProc,true);
   	DC.Add(hInst,"工作区",0,0,200,400,DOCK_LEFT,DOCK_LEFT,WorkSpaceProc,true);

    hAccel=LoadAccelerators(hInstance,_T("MAINMENU"));
   	if(hAccel==NULL)
		MessageBox(hwndFrame,_T("无法加载加速键"),_T("错误"),NULL);
	while(GetMessage(&Msg,NULL,0,0))
	{
		if(!TranslateAccelerator(hwndFrame,hAccel,&Msg))
		{
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
	for(int i=0;i<MDIWndContainer.length();i++)
			delete MDIWndContainer[i].JEdit;
	delete fdOutput;
	return Msg.wParam ;
}
LRESULT CALLBACK FrameWndProc (HWND hwnd, UINT iMsg, WPARAM wParam,
                                                     LPARAM lParam)
{
    CLIENTCREATESTRUCT  css;
    char FileName[MAX_PATH]="";
	int		i;
	HWND hwndChild;
	JavaEdit *JEdit;
	UINT nHitTest;
	CHOOSECOLOR cc;
	COLORREF rgbCustom[16] = {0};
	MDIWnd *mdiWnd;
	WORD x,y;
	RECT ClientRect;
    switch (iMsg)
    {
    case WM_CREATE :         

        css.hWindowMenu  = NULL ;
        css.idFirstChild = 100 ;
		//创建客户区窗口
		hwndClient = CreateWindow( _T("MDICLIENT"), (LPCTSTR) NULL, 
            WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL, 
            0, 0, 0, 0, hwnd, (HMENU) 0xCAC, hInst, (LPSTR) &css); 
		//显示客户区
        ShowWindow(hwndClient, SW_SHOW);
		EnableMenuItem(GetMenu(hwnd),IDM_UNDO,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_REDO,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_CUT,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_COPY,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_PASTE,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_DELETE,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_FIND,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_REPLACE,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_COMPILE,MF_BYCOMMAND|MF_GRAYED);
		EnableMenuItem(GetMenu(hwnd),IDM_RUN,MF_BYCOMMAND|MF_GRAYED);
        hStatusBar=CreateStatusWindow(WS_CHILD|CCS_BOTTOM|WS_THICKFRAME|WS_VISIBLE,(LPCTSTR)NULL,hwnd,ID_STATUSBAR);
		InitStatusBar(hwnd);
		return 0 ;
	case WM_DWPOSCHANGED:
		hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
		mdiWnd=GetMDIWnd(hwndChild);
		if(mdiWnd!=NULL&&mdiWnd->max==true)
		{
			GetWndRect(&ClientRect);
			DC.GetRect(ClientRect);
			MoveWindow(hwndChild,ClientRect.left,ClientRect.top-GetSystemMetrics(SM_CYMIN),
				ClientRect.right-ClientRect.left,ClientRect.bottom-ClientRect.top+GetSystemMetrics(SM_CYMIN),
				true);
		}
		return 0;
	case WM_WINDOWPOSCHANGED:
		hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
		mdiWnd=GetMDIWnd(hwndChild);
		DefWindowProc(hwnd,WM_WINDOWPOSCHANGED,wParam,lParam);
		if(mdiWnd!=NULL&&mdiWnd->max==true)
		{
			GetWndRect(&ClientRect);
			DC.GetRect(ClientRect);
			MoveWindow(hwndChild,ClientRect.left,ClientRect.top-GetSystemMetrics(SM_CYMIN),
				ClientRect.right-ClientRect.left,ClientRect.bottom-ClientRect.top+GetSystemMetrics(SM_CYMIN),
				true);
		}
		nHitTest=SendMessage(hwnd,WM_NCHITTEST,wParam,lParam);
		if(nHitTest==HTMINBUTTON)
			return 0;
		if(DC.hWnd!=NULL)
		{
			SendMessage(DC.hWnd,WM_PARENTPOSCHANGED,0,0);
		}
		InitStatusBar(hwnd);
		return 0;
	case WM_NCLBUTTONDOWN:
		x=LOWORD(lParam);
		y=HIWORD(lParam);
		nHitTest=DefWindowProc(hwnd,WM_NCHITTEST,wParam,lParam);
		DefWindowProc(hwnd,WM_NCLBUTTONDOWN,wParam,lParam);
		if(nHitTest==5&&y>20&&x>GetSystemMetrics(SM_CXSCREEN)-40)
		{
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			mdiWnd=GetMDIWnd(hwndChild);
			if(mdiWnd!=NULL)
				mdiWnd->max=false;
		}
		return 0;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
			//新建消息
		case IDM_NEW:
			DialogBox(hInst,MAKEINTRESOURCE(IDD_NEW),hwnd,(DLGPROC)NewWndProc);
			break;
		case IDM_OPEN:
			if(ShowOpenDialog(hwnd,FileName,"Java源程序(*.JAVA)\0*.java\0"))
			{
				strcpy(InitDir,FileName);
				CreateMDIChild(FileName);
			}
			break;
		case IDM_SAVE:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			SendMessage(hwndChild,WM_SAVEFILE,0L,0L);
			break;
		case IDM_SAVEALL:
			for(i=0;i<MDIWndContainer.length();i++)
				SendMessage(MDIWndContainer[i].hwnd,WM_SAVEFILE,0L,0L);
			break;
		case IDM_SAVEAS:
			if(ShowSaveDialog(hwnd,FileName))
			{
				hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
				JEdit=GetJEDIT(hwndChild);
				strcpy(JEdit->FileName,FileName);
				JEdit->SaveFile();
				SetWindowText(hwndChild,FileName);
			}
			break;
		case IDM_COMPILE:
			if(strcmp(JDKPath,"")==0)
			{
				MessageBox(hwnd,"请指定正确的编译器路径!","错误",
					MB_OK|MB_ICONERROR);
				return 0;
			}
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			SendMessage(hwndChild,WM_SAVEFILE,0L,0L);
		
			JEdit=GetJEDIT(hwndChild);
			if(JEdit==NULL)
				return false;
			DC.PopUp("输出");
			fdOutput->InitBuffer();
			fdOutput->Insert("正在编译...\n");
			CompileProg(JEdit->FileName);
			break;
		case IDM_RUN:
			if(strcmp(JDKPath,"")==0)
			{
				MessageBox(hwnd,"请指定正确的编译器路径!","错误",
					MB_OK|MB_ICONERROR);
				return 0;
			}
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			SendMessage(hwndChild,WM_SAVEFILE,0L,0L);
		
			JEdit=GetJEDIT(hwndChild);
			if(JEdit==NULL)
				return false;
			RunProg(JEdit->FileName);
			break;
		case IDM_RUNAPPLET:
			if(strcmp(JDKPath,"")==0)
			{
				MessageBox(hwnd,"请指定正确的编译器路径!","错误",
					MB_OK|MB_ICONERROR);
				return 0;
			}
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			SendMessage(hwndChild,WM_SAVEFILE,0L,0L);
		
			JEdit=GetJEDIT(hwndChild);
			if(JEdit==NULL)
				return false;
			RunProg(JEdit->FileName,true);
			break;
		case IDM_SETTING:
			DialogBox(hInst,MAKEINTRESOURCE(IDD_SETTING),hwnd,(DLGPROC)SettingProc);
			break;
			//窗口重叠
		case IDM_CASCADE:
			SendMessage(hwndClient,WM_MDICASCADE,0,0L);
			break;
			//窗口并列
		case IDM_TILE:
			SendMessage(hwndClient,WM_MDITILE,0,0L);
			break;
			//激活下一个窗口
		case IDM_NEXT:
			SendMessage(hwndClient,WM_MDINEXT,0,0L);
			break;
			//关闭当前窗口
		case IDM_CLOSE:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			SendMessage(hwndClient,WM_MDINEXT,(WPARAM)hwndChild,0L);
			SendMessage(hwndClient,WM_MDIDESTROY,(WPARAM)hwndChild,0L);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, (DLGPROC)AboutProc );
			break;
		case IDM_QUIT:
			PostQuitMessage(0);
			break;
		case IDM_TEXTCOLOR:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			memset(&cc,0,sizeof(cc));
			cc.lStructSize=sizeof(CHOOSECOLOR);
			cc.hwndOwner=hwnd;
			cc.rgbResult=JavaEdit::hTextColor;
			cc.Flags=CC_FULLOPEN|CC_RGBINIT;
			cc.lpCustColors=rgbCustom;
			if(ChooseColor(&cc))
			{
				JavaEdit::hTextColor=cc.rgbResult;
				if(JEdit!=NULL)
					InvalidateRect(JEdit->m_hWnd,NULL,false);
			}
			break;
		case IDM_KWCOLOR:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			memset(&cc,0,sizeof(cc));
			cc.lStructSize=sizeof(CHOOSECOLOR);
			cc.hwndOwner=hwnd;
			cc.rgbResult=JavaEdit::KeywordColor;
			cc.Flags=CC_FULLOPEN|CC_RGBINIT;
			cc.lpCustColors=rgbCustom;
			if(ChooseColor(&cc))
			{
				JavaEdit::KeywordColor=cc.rgbResult;
				if(JEdit!=NULL)
					InvalidateRect(JEdit->m_hWnd,NULL,false);
			}
			break;
		case IDM_COMMENTCOLOR:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			memset(&cc,0,sizeof(cc));
			cc.lStructSize=sizeof(CHOOSECOLOR);
			cc.hwndOwner=hwnd;
			cc.rgbResult=JavaEdit::CommentColor;
			cc.Flags=CC_FULLOPEN|CC_RGBINIT;
			cc.lpCustColors=rgbCustom;
			if(ChooseColor(&cc))
			{
				JavaEdit::CommentColor=cc.rgbResult;
				if(JEdit!=NULL)
					InvalidateRect(JEdit->m_hWnd,NULL,false);
			}
			break;
		case IDM_BGCOLOR:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			memset(&cc,0,sizeof(cc));
			cc.lStructSize=sizeof(CHOOSECOLOR);
			cc.hwndOwner=hwnd;
			cc.rgbResult=JavaEdit::hBgColor;
			cc.Flags=CC_FULLOPEN|CC_RGBINIT;
			cc.lpCustColors=rgbCustom;
			if(ChooseColor(&cc))
			{
				JavaEdit::hBgColor=cc.rgbResult;
				JavaEdit::hBackGround=NULL;
				strcpy(JavaEdit::BGFile,"");
				if(JEdit!=NULL)
					InvalidateRect(JEdit->m_hWnd,NULL,false);
			}
			break;
		case IDM_BGPIC:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(ShowOpenDialog(hwnd,JavaEdit::BGFile,"位图文件(*.BMP)\0*.bmp\0"))
			{
				if(JavaEdit::hBackGround!=NULL)
					DeleteObject(JavaEdit::hBackGround);
				JavaEdit::hBackGround=(HBITMAP)LoadImage(hInst,
					JavaEdit::BGFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
				if(JavaEdit::hBackGround==NULL)
					MessageBox(hwnd,"无法读取指定文件!","错误",MB_OK);
				if(JEdit!=NULL)
					InvalidateRect(JEdit->m_hWnd,NULL,false);
			}
			break;
		case IDM_DEFAULT:
			JavaEdit::hTextColor=RGB(0,0,0);
			JavaEdit::hBgColor=RGB(255,255,255);
			JavaEdit::KeywordColor=RGB(0,0,255);
			JavaEdit::CommentColor=RGB(0,128,0);
			strcpy(JavaEdit::BGFile,"");
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(JEdit!=NULL)
					InvalidateRect(JEdit->m_hWnd,NULL,false);
			break;
		case IDM_GOTOLINE:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_GOTOLINE), hwnd, (DLGPROC)GotoLineProc );
			break;
		case IDM_FIND:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_FIND), hwnd, (DLGPROC)FindProc );
			break;
		case IDM_REPLACE:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_REPLACE), hwnd, (DLGPROC)ReplaceProc );
			break;
		case IDM_UNDO:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(JEdit)
				JEdit->Undo();
			break;
		case IDM_REDO:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(JEdit)
				JEdit->Redo();
			break;
		case IDM_CUT:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(JEdit)
			{
				JEdit->CopySel();
				JEdit->DeleteSel();
			}
			SendMessage(hwndChild,WM_LCCHANGED,0,0);
			break;
		case IDM_COPY:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(JEdit)
				JEdit->CopySel();
			break;
		case IDM_PASTE:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);
			if(JEdit)
			{
				JEdit->DeleteSel();
				JEdit->PasteChar();
			}
			SendMessage(hwndChild,WM_LCCHANGED,0,0);
			break;
		case IDM_DELETE:
			hwndChild=(HWND)SendMessage(hwndClient,WM_MDIGETACTIVE,0,0L);
			JEdit=GetJEDIT(hwndChild);

⌨️ 快捷键说明

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