📄 lwin.cpp
字号:
#include <lwin.h>
#include <dos.h>
#include <string.h>
/* Extern Datas */
void WinHandleEventSource(int Event) { Event=Event; }
void (*WinHandleEvent)(int Event)=WinHandleEventSource;
TBUTTON *WinButtons;
TWin *WinWindows;
int WinEvent;
/* static mumber */
static char WinInitFlag=FALSE;
static TGDC dc;
static void dc_s() { lSaveDC(&dc); lActiveDC(0); }
static void dc_r() { lActiveDC(&dc); }
/* The Inter Face of The Win Function */
static char Ifin(TBUTTON *b,short x,short y);
void WinRun();
void WinMainProc(int exit_code);
int Win_Button_Process();
/***********************************************************************/
char WinInit()
{ short i,j;
if (WinInitFlag==TRUE) return FALSE;
WinInitFlag=TRUE;
WinButtons=new TBUTTON[MAX_BUTTON];
WinWindows=new TWin[MAX_WINDOW];
for (i=0;i<MAX_BUTTON;i++) WinButtons[i].mode=BMODE_NULL;
for (i=0;i<MAX_WINDOW;i++) WinWindows[i].mode=WMODE_NULL;
lmInit(); lmCloseTimer(); mouse_event=0; lmOpen();
lwInitColor();
return TRUE;
}
static char Ifin(TBUTTON *b,short x,short y)
{ if (x>=b->x1&&y>=b->y1&&x<=b->x2&&y<=b->y2) return 1;
else return 0;
}
void Win_child_DrawB(TBUTTON *b,char mode)
{ lmClose();
dc_s();
lwDrawButton(b->x1,b->y1,b->x2,b->y2, b->name, b->bitmap, mode);
dc_r();
lmOpen();
}
void Win_child_DrawW(TWin *w)
{ int x1=w->x1, x2=w->x2, y1=w->y1, y2=w->y2, size=(x2-x1+1)*(y2-y1+1)+4, index;
w->savebitmap=new char[size];
dc_s();
lgetBlock(x1,y1,x2,y2,w->savebitmap);
lwCreateWin(w);
dc_r();
}
void Win_child_Clear(TWin *w)
{ int x=w->x1 , y=w->y1;
dc_s();
lputBlock(x,y,w->savebitmap);
delete w->savebitmap;
dc_r();
}
void Win_child_ForbidAll(int forbid)
{ int i;
for (i=0;i<MAX_BUTTON;i++) WinButtons[i].forbid|=forbid;
for (i=0;i<MAX_WINDOW;i++) WinWindows[i].forbid|=forbid;
}
void Win_child_UnForbidAll(int forbid)
{ int i;
for (i=0;i<MAX_BUTTON;i++) WinButtons[i].forbid&=(0xffff^forbid);
for (i=0;i<MAX_WINDOW;i++) WinWindows[i].forbid&=(0xffff^forbid);
}
///////////////////////////////////////////////////////////////////
extern short lw_b1x1, lw_b1y1, lw_b1x2, lw_b1y2, lw_b2x1, lw_b2y1, lw_b2x2, lw_b2y2;
void Win_SendEvent(int index,int event)
{ int r;
if (WinWindows[index].mode==WMODE_NULL) return;
dc_s();
lActiveDC(&WinWindows[index].wdc);
if (WinWindows[index].proc) r=(*WinWindows[index].proc)(event,index);
else r=cmNORMAL;
dc_r();
switch (r)
{ case cmDESTORY: WinDelWindow(&WinWindows[index]); break;
}
}
int Win_Button_Process()
{ int i,j,event=cmNORMAL,x,y;
if (mouse_event==1&&mouse_code==MBUTTON_L)
{ x=mouse_x; y=mouse_y;
for (i=0;i<MAX_BUTTON;i++)
if (WinButtons[i].mode==BMODE_WORK && WinButtons[i].forbid==0 &&
Ifin(&WinButtons[i],x,y)==1)
{ Win_child_DrawB(&WinButtons[i],1); lmDraw();
delay(CLICK_DELAY);
Win_child_DrawB(&WinButtons[i],0);
event=WinButtons[i].command;
if (WinButtons[i].belong_win==NUL_BELONG) WinEvent=event;
else Win_SendEvent(WinButtons[i].belong_win,event);
}
mouse_event=0;
}
return event;
}
int Win_Window_Process()
{ int i,f;
for (i=0;i<MAX_WINDOW;i++)
{
if (WinWindows[i].mode!=WMODE_NULL)
{ f=WinWindows[i].forbid&WinWindows[i].fmask;
if (f==0&&WinWindows[i].type==WTYPE_INITIACT)
Win_SendEvent(i,cmNORMAL_CALL);
}
}
return 0;
}
int WinCreateButton(TBUTTON *b)
{ int i=0;
while (WinButtons[i].mode!=BMODE_NULL&&i<MAX_BUTTON) i++;
if (i>=MAX_BUTTON) i=MAX_BUTTON-1;
b->index=i; b->mode=BMODE_WORK; b->forbid=0;
memcpy(&WinButtons[i],b,sizeof(*b));
Win_child_DrawB(b,0);
return i;
}
int WinCreateWindow(TWin *w)
{ int i=0, b1, b2;
TBUTTON b;
while (WinWindows[i].mode!=WMODE_NULL&&i<MAX_WINDOW) i++;
if (i>=MAX_WINDOW) i=MAX_WINDOW - 1;
w->index=i; w->mode=WMODE_WORK; w->forbid=0;
memcpy(&WinWindows[i],w,sizeof(*w));
Win_child_DrawW(w);
switch (w->type)
{ case WTYPE_PASSIVE: w->fmask=0xffff; w->style=0; break;
case WTYPE_INITIACT: w->fmask=WFORBID_HIGH; w->style=0; break;
}
b.x1=lw_b1x1, b.y1=lw_b1y1, b.x2=lw_b1x2, b.y2=lw_b1y2;
b.name="x"; b.bitmap=NULL; b.command=cmEXIT; b.belong_win=i;
WinCreateButton(&b);
w->b1=b.index;
b.x1=lw_b2x1, b.y1=lw_b2y1, b.x2=lw_b2x2, b.y2=lw_b2y2;
b.name="+"; b.bitmap=NULL; b.command=cmTMENU; b.belong_win=i;
WinCreateButton(&b); w->b2=b.index;
memcpy(&WinWindows[i],w,sizeof(*w));
Win_SendEvent(i,cmSTART);
return i;
}
void WinDelWindow(TWin *w)
{ int i=w->index,b1,b2;
if (i>=MAX_WINDOW) return;
if (WinWindows[i].mode==WMODE_NULL) return;
Win_child_Clear(w);
WinWindows[i].mode=WMODE_NULL;
b1=w->b1, b2=w->b2; WinButtons[b1].mode=BMODE_NULL; WinButtons[b2].mode=BMODE_NULL;
}
void WinDelButton(TBUTTON *b)
{ int i=b->index;
if (i>=MAX_BUTTON) return;
WinButtons[i].mode=BMODE_NULL;
}
void WinClearButton()
{ int i;
for (i=0;i<MAX_BUTTON;i++) WinButtons[i].mode=BMODE_NULL;
}
/**************************************************************************/
void WinRun()
{ WinInit();
WinMainProc(cmEXIT);
WinInitFlag=FALSE;
delete WinButtons, WinWindows;
}
////////////////////////////////////////////////////////////////
void WinMainProc(int exit_code)
{ char mouse_timer=lmIfTimer();
lmClose(); lmCloseTimer(); lmOpen();
WinEvent=cmNORMAL;
while (WinEvent!=exit_code)
{ WinEvent=cmNORMAL;
lmDraw();
Win_Button_Process();
Win_Window_Process();
if (WinHandleEvent) (*WinHandleEvent)(WinEvent);
}
if (mouse_timer) lmInitTimer();
}
///////////////////////////////////////////////////////////////
static int MessageBoxHandle(int event,int index)
{ static TBUTTON b;
TWin *w=&WinWindows[index];
int r=cmNORMAL;
switch (event)
{ case cmSTART: b.x1=w->x1+(w->x2-w->x1-32)/2; b.x2=b.x1+32;
b.y2=w->y2-10;
b.y1=b.y2-19; b.name="OK"; b.bitmap=0; b.command=cmOK;
b.belong_win=index; WinCreateButton(&b);
break;
case cmOK: r=cmDESTORY; WinEvent=cmEXIT; WinDelButton(&b);
break;
}
return r;
}
void WinMessageBox(char *title,char *string)
{ int slen=GModeData.LEN,swid=GModeData.WID, wordlen=strlen(string)*8,
wordlen2=strlen(title)*8, len, i;
TWin w;
if (wordlen>wordlen2) len=wordlen+30;
else len=wordlen2+60;
w.title=title;
w.x1=(slen-len)/2; w.x2=w.x1+len-1; w.y1=(swid*3/4-swid/5)/2;
w.y2=w.y1+swid/5; w.type=WTYPE_INITIACT; w.proc=MessageBoxHandle;
Win_child_ForbidAll(WFORBID_HIGH);
i=WinCreateWindow(&w);
lActiveDC(&w.wdc); louttextxy((len-wordlen)/2,15,string);
lActiveDC(0);
WinEvent=cmNORMAL;
WinMainProc(cmEXIT);
WinDelWindow(&w);
Win_child_UnForbidAll(WFORBID_HIGH);
WinEvent=cmNORMAL;
}
void WinMessageBox(short x1,short y1,short x2,short y2,char *title,char *string)
{ int wordlen=strlen(string)*8,
wordlen2=strlen(title)*8, len, i;
TWin w;
if (wordlen>wordlen2) len=wordlen+30;
else len=wordlen2+60;
w.title=title;
w.x1=x1; w.x2=x2; w.y1=y1;
w.y2=y2; w.type=WTYPE_INITIACT; w.proc=MessageBoxHandle;
Win_child_ForbidAll(WFORBID_HIGH);
i=WinCreateWindow(&w);
lActiveDC(&w.wdc); louttextxy(0,15,string);
lActiveDC(0);
WinEvent=cmNORMAL;
WinMainProc(cmEXIT);
WinDelWindow(&w);
Win_child_UnForbidAll(WFORBID_HIGH);
WinEvent=cmNORMAL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -