📄 mywindow.h
字号:
int i,j;
j=y-Edit->top;
if (0<=j && j<=23 && Edit->left+Edit->Width-18<=x && x<= Edit->left+Edit->Width) return true;
if (x< Edit->left || Edit->left+Edit->Width < x ||y-Edit->top<23) return false;
for (i=0;i<9 && Edit->Items[i];i++);
j=(y-Edit->top-23)/17;
if (0<=j && j<i) {
strcpy(Edit->Text,Edit->Items[j]);
i=strlen(Edit->Text);
Edit->Index=i;
Edit->CurLocate=i<Edit->MaxChars?i:Edit->MaxChars;
return true;
}
return false;
}
/*找到鼠标点击的编辑框*/
Hwnd FindEdit(Hwnd FirstEdit,int x,int y){
while (FirstEdit)
if (GetEdit(FirstEdit)->left <=x && x<=(GetEdit(FirstEdit)->left+GetEdit(FirstEdit)->Width) &&
GetEdit(FirstEdit)->top <=y && y<=GetEdit(FirstEdit)->top+22)
return FirstEdit;
else FirstEdit=NextEdit(FirstEdit);
return 0;
}
/*找到鼠标点击的按钮*/
Hwnd FindButton(Hwnd FirstButton,int x,int y){
while (FirstButton)
if (GetButton(FirstButton)->left <=x && x<=GetButton(FirstButton)->right &&
GetButton(FirstButton)->top <=y && y<=GetButton(FirstButton)->bottom)
return FirstButton;
else FirstButton=NextButton(FirstButton);
return 0;
}
/*找到鼠标点击的窗体,并把它放到最顶层*/
Hwnd FindWindow(int x,int y){
pWindow Window;
int p=pStack-1;
while (WndStack[--p]){
Window=GetWindow(WndStack[p]);
if (Window->left <=x && x<= Window->right &&
Window->top <=y && y<=Window->bottom){
memcpy(WndStack+p,WndStack+p+1,(pStack-p) *sizeof WndStack[0]);
WndStack[pStack-1]=Window->handle;
return Window->handle;
}
}
return 0;
}
/*关闭窗体的函数*/
void CloseWindow(Hwnd handle){
pWindow Window=GetWindow(handle);
if (!Window->Showed) return;
if (Window->OnClose) {
if (!Window->OnClose(Window->handle)) return;
}
HideWindow(Window->handle);
Window->Showed=false;
if (Window->Closed)
Window->Closed(Window->handle);
}
void DefEditDown(Hwnd handle,int x=0,int y=0){
if (handle==0) return ;
pEdit Edit=GetEdit(handle);
pWindow Window=GetWindow(Edit->father);
if (Window->focus!=handle) {
if (GetEdit(Window->focus)->ShowCur){
GetEdit(Window->focus)->ShowCur=false;
DrawCur(Window->focus);
}
Window->focus=handle;
Edit->ShowCur=true;
int len=strlen(Edit->Text);
Edit->CurLocate=len<Edit->MaxChars?len:Edit->MaxChars;
Edit->Index=len;
if (Window->ShowItems){
Window->ShowItems=false;
DrawWindow(TopWnd);
}else DrawEdit(handle);
}
if (Edit->Items[0] && x>Edit->Width-18 && y<=21){ /*按下下拉框按纽*/
Window->ShowItems=!Window->ShowItems;
if (Window->ShowItems){
DrawEdit(handle);
DrawItems();
}
else DrawWindow(TopWnd);
}
}
/*=================================*/
/* 默认按钮消息函数定义 */
/* 主要完成按钮动作绘制 */
/*=================================*/
void DefButtonDown(Hwnd handle){
DestroyMouse();
DrawButton(handle,true);
DrawMouse();
}
void DefButtonUp(Hwnd handle){
DestroyMouse();
DrawButton(handle);
DrawMouse();
}
/* 默认关闭按钮回调函数 */
/* 所有关闭按钮可以此函数作回调函数*/
void DefCloseButton(Hwnd handle){
CloseWindow(GetButton(handle)->father);
}
/*=================================*/
/* 窗体消息回调函数定义 */
/*=================================*/
#define imp 0x7fff
void WindowProc(
HWND handle, /* handle of window*/
UINT uMsg, /* message identifier*/
int wParam, /* first message parameter*/
int lParam) /* second message parameter*/
{
pWindow Window=GetWindow(handle);
Hwnd hd;
static int LastX,LastY;
static pButton LastButton=NULL;
switch (uMsg){ /*模仿Windows系统进行消息处理*/
case WM_LEFTDOWN:
if (Window->ShowItems ) {
hd=FindItems(wParam,lParam);
Window->ShowItems=false;
DrawWindow(TopWnd);
if (hd) break;
}
if ((hd=FindButton(Window->FirstButton,wParam,lParam))!=0) {
DefButtonDown(hd);
LastButton=GetButton(hd);
} else if ((hd=FindEdit(Window->FirstEdit,wParam,lParam))!=0) {
DefEditDown(hd,wParam-GetEdit(hd)->left,lParam-GetEdit(hd)->top);
} else if (FindList(wParam,lParam));
else if (0<=lParam && lParam<= 21 && 0<=wParam && wParam<= Window->Width-16) { /*窗体的拖动初始化*/
Window->StartDrag=true;
Window->DragX=wParam;
Window->DragY=lParam;
LastX=LastY=imp;
SetMouseRect(wParam,lParam,MAXX-Window->Width+wParam,MAXY-21-Window->Height+lParam);
}
break;
case WM_LEFTUP:
if (Window->StartDrag && LastX!=imp) { /*窗体拖动完毕,移动窗体到目标位置*/
DragWindow(handle,LastX-Window->DragX,LastY-Window->DragY,DARKGRAY);
MoveWindow(handle,wParam-Window->DragX,lParam-Window->DragY);
SetMouseRect(0,0,MAXX,MAXY);
Window->StartDrag=false;
break;
}
SetMouseRect(0,0,MAXX,MAXY);
Window->StartDrag=false;
hd=FindButton(Window->FirstButton,wParam,lParam);
if (hd && (Hwnd)LastButton==hd) {
DefButtonUp(hd);
LastButton=0;
if (GetButton(hd)->OnClick!=NULL) (*(GetButton(hd)->OnClick))(hd);
}
break;
case WM_MOUSEMOVE:
if (Window->StartDrag){
if (LastX!=imp) DragWindow(handle,LastX-Window->DragX,LastY-Window->DragY,DARKGRAY);
DragWindow(handle,wParam-Window->DragX,lParam-Window->DragY,DARKGRAY);
LastX=wParam;
LastY=lParam;
}
if (LastButton){
if (LastButton->left >wParam || wParam > LastButton->right ||
LastButton->top >lParam || lParam > LastButton->bottom){
DefButtonUp(LastButton->handle);
LastButton=NULL;
}
}
break;
case WM_KEYCLICK:
if ((wParam & 0xff)=='\t' && Window->focus) { /*当TAB键按下时,切换焦点*/
if (GetEdit(Window->focus)->Next)
DefEditDown(GetEdit(Window->focus)->Next);
else DefEditDown(Window->FirstEdit);
}
if (wParam==0x4b00 && Window->focus) { /*当按下向左方向键时,光标前移*/
pEdit Edit=GetEdit(Window->focus);
if (Edit->Index) {
Edit->Index--;
if (Edit->CurLocate) Edit->CurLocate--;
Edit->ShowCur=true;
DrawEdit(Edit->handle);
}
}
if (wParam==0x4d00 && Window->focus) { /*当按下向右方向键时,光标后移*/
pEdit Edit=GetEdit(Window->focus);
if (strlen(Edit->Text) > Edit->Index){
if (Edit->Text[Edit->Index] & 128){
Edit->Index+=2;
Edit->CurLocate+=2;
} else{
Edit->Index++;
Edit->CurLocate++;
}
if (Edit->CurLocate>Edit->MaxChars)
Edit->CurLocate=Edit->MaxChars;
Edit->ShowCur=true;
DrawEdit(Edit->handle);
}
}
if (wParam==0x4900 && Window->FirstList){ /*当按下PageUp键时,列表框上翻页*/
ListPageDown(Window->FirstList,1-GetList(Window->FirstList)->CanList);
}
if (wParam==0x5100 && Window->FirstList){ /*当按下PageDown键时,列表框下翻页*/
ListPageDown(Window->FirstList,GetList(Window->FirstList)->CanList-1);
}
if ((wParam & 0xFF) == 13){ /*当按下Enter键时*/
if (Window->DefButton)
if (GetButton(Window->DefButton)->OnClick) GetButton(Window->DefButton)->OnClick(Window->DefButton);
}
break;
case WM_TIMER:
UpdateTime();
if (Window->focus){ /*在焦点输入框以闪烁的方式显示光标*/
DrawCur(Window->focus);
GetEdit(Window->focus)->ShowCur=!GetEdit(Window->focus)->ShowCur;
}
break;
}
}
/*=================================*/
/* 消息发送核心函数定义 */
/*=================================*/
bool MakeMsg(){ /*返回假则表示结束运行*/
static long ThisTime=0,LastTime=0;
if (TopWnd==0) return false;
if (bioskey(1)){
int key=bioskey(0);
if ((key & 0xFF)==' ' && (bioskey(2) & 4) ){
ImmIsChn=!ImmIsChn;
DrawImmBar();
}
else{
AddKey(key);
DrawImmBar();
WindowProc(TopWnd,WM_KEYCLICK,key,0); /*发送键盘事件*/
}
}
MouseUpdate();
pWindow Window=GetWindow(TopWnd); /*获取顶层窗体*/
ThisTime=clock(); /*时间事件处理*/
if (ThisTime-LastTime>10){ /*大约每50MS发送一次消息*/
WindowProc(TopWnd,WM_TIMER,(int)&ThisTime,(int)&LastTime);
LastTime=ThisTime;
}
if (MouseX != OldX || MouseY != OldY) /*鼠标移动事件*/
WindowProc(TopWnd,WM_MOUSEMOVE,MouseX- Window->left,MouseY- Window->top);
if (Left_Pressed && !OldLeft){ /*鼠标左键按下*/
if (MouseX >= Window->left && MouseX <= Window->right &&
MouseY >= Window->top && MouseY <= Window->bottom) /*判断鼠标是否在顶层窗口以内*/
WindowProc(TopWnd,WM_LEFTDOWN, MouseX- Window->left,MouseY- Window->top);
else if (GetWindow(TopWnd)->CanSwitch) {
if (FindWindow(MouseX,MouseY)){
DrawWindow(TopWnd); /*切换当前窗体*/
Window=GetWindow(TopWnd); /*发送左击信息给新的当前窗口*/
WindowProc(TopWnd,WM_LEFTDOWN, MouseX- Window->left,MouseY- Window->top);
}
}
}
else if(!Left_Pressed && OldLeft) /*鼠标左键弹出*/
WindowProc(TopWnd,WM_LEFTUP, MouseX- Window->left,MouseY- Window->top);
if (Right_Pressed && !OldRight) /*鼠标右键按下*/
WindowProc(TopWnd,WM_RIGHTCLICK, MouseX- Window->left,MouseY- Window->top);
OldX=MouseX;
OldY=MouseY;
OldLeft=Left_Pressed;
OldRight=Right_Pressed;
return true;
}
void OnInput(char *Text){
pWindow Window=GetWindow(TopWnd);
if (!Window->focus) return;
char buf[50];
int i;
pEdit Edit=GetEdit(Window->focus);
if (Text[0]==8){
i=DelChar(Edit->Text,Edit->Index);
if (i==0) return;
Edit->Index-=i;
if (Edit->CurLocate>Edit->Index) Edit->CurLocate=Edit->Index;
}
else if (strlen(Edit->Text)+strlen(Text)<45){
strcpy(buf,Edit->Text+Edit->Index);
Edit->Text[Edit->Index]=0;
strcat(Edit->Text,Text);
strcat(Edit->Text,buf);
Edit->Index+=strlen(Text);
}
DrawEdit(Edit->handle);
}
/*=================================*/
/* 消息弹出函数定义 */
/*=================================*/
void MessageBox(char *Text,char *Title,bool Locked=true){ /*Locked 表示是否锁死线程*/
pWindow Window=GetWindow(MsgBoxHd);
pLabel Label=GetLabel(Window->FirstLabel);
strcpy(Window->Caption,Title);
strcpy(Label->Caption,Text);
ShowWindow(MsgBoxHd);
if (Locked)
while (Window->Showed) MakeMsg();
}
void YesButton(Hwnd handle){
GetWindow(GetButton(handle)->father)->tag=true;
DefCloseButton(handle);
}
void NoButton(Hwnd handle){
GetWindow(GetButton(handle)->father)->tag=false;
DefCloseButton(handle);
}
bool DlgBox(char *Text,char *Title,char *YesButton="确定",char *NoButton="取消"){ /*弹出选择窗口,选择是/否*/
pWindow Window=GetWindow(DlgBoxHd);
pLabel Label=GetLabel(Window->FirstLabel);
strcpy(Window->Caption,Title);
strcpy(Label->Caption,Text);
Hwnd handle=Window->FirstButton;
handle=NextButton(handle);
strcpy(GetButton(handle)->Caption,YesButton);
handle=NextButton(handle);
strcpy(GetButton(handle)->Caption,NoButton);
ShowWindow(DlgBoxHd);
while (Window->Showed) MakeMsg();
return Window->tag;
}
void WindowsInit(){
static AlreadyInit=false; /*确保初始化工作只执行一次*/
if (AlreadyInit) return;
AlreadyInit=true;
push(0);
DrawDesktop();
DrawImmBar();
LoadIcoFile("Ico\\OK.ico",OKIco);
LoadIcoFile("Ico\\Cancel.ico",CancelIco);
LoadIcoFile("Ico\\No.ico",NoIco);
LoadIcoFile("Ico\\Wnd.ico",WndIco);
LoadIcoFile("Ico\\Close.ico",CloseIco);
LoadIcoFile("Ico\\About.ico",AboutIco);
LoadIcoFile("Ico\\Warn.Ico",WarnIco);
LoadIcoFile("Ico\\Ask.Ico",AskIco);
MsgBoxHd=CreateWindow("",160,170,MAXX-160,MAXY-170,WarnIco,0,false);
GetWindow(MsgBoxHd)->DefButton=CreateButton("确定",MsgBoxHd,&DefCloseButton,130,90,DEF_BUT_WIDTH,DEF_BUT_HEIGHT);
CreateIco(WarnIco,MsgBoxHd,30,50);
CreateLabel("",MsgBoxHd,60,50);
DlgBoxHd=CreateWindow("",160,170,MAXX-160,MAXY-170,AskIco,0,false);
GetWindow(DlgBoxHd)->DefButton=CreateButton("",DlgBoxHd,&YesButton,80,90,DEF_BUT_WIDTH,DEF_BUT_HEIGHT,OKIco);
CreateButton("",DlgBoxHd,&NoButton,180,90,DEF_BUT_WIDTH,DEF_BUT_HEIGHT,NoIco);
CreateIco(AskIco,DlgBoxHd,30,50);
CreateLabel("",DlgBoxHd,60,50);
ImmInit(&OnInput);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -