?? main.cpp
字號:
/******************************************************************************\
*
* 文件名: main.cpp
* 目的:
* 使用說明: 在保持此程序完整性的情況下,你可以任意傳播它。你也可以修改它
* 來為你服務(wù),如果你要將此程序用于商業(yè)目的,請告知我
*
* 版權(quán)所有 2002 張?jiān)?*
\******************************************************************************/
#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;
//函數(shù)聲明
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) ;
// 創(chuàng)建框架窗口
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,"工作區(qū)",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 ;
//創(chuàng)建客戶區(qū)窗口
hwndClient = CreateWindow( _T("MDICLIENT"), (LPCTSTR) NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,
0, 0, 0, 0, hwnd, (HMENU) 0xCAC, hInst, (LPSTR) &css);
//顯示客戶區(qū)
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;
//關(guān)閉當(dāng)前窗口
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);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -