📄 winapi.c
字号:
loses the keyboard focus and a WM_SETFOCUS message to the window that receives the keyboard focus. The none-active-window will be activated when SetFocus to it's sub-window.OUTPUTS If succeeds, the return value is the handle of the window that previously had the keyboard focus.REMARK 对隐藏的窗口允许设为焦点 焦点所在的主窗体不窗隐藏与否,都激活为活动窗体,因此活动窗体允许是隐藏的。 设置焦点有两种情形: (1) 用户正常调用以设置焦点窗口 如果焦点转移产生活动窗体切换,将触发窗体的重布局与显示刷新 (2) 系统调用以转移待销毁窗口的焦点 这种转移可能不能一步到位,可先转移到待销毁的主窗体,然后再转移到其它主窗体。 如果焦点转移产生活动窗体切换,将触发窗体的重布局与显示刷新---------------------------------------------------------------------------*/HWND WINAPI SetFocus(HWND hWnd){ PWND top, top2; /* if NULL or hidden, set focus to root*/ if(!IsWnd(hWnd) || WndGetAttr(hWnd,WS_HIDE|WS_DISABLED)) { hWnd = (HWND)g_RootWnd; } if( hWnd == (HWND)g_CurrentFocus) { return (HWND)g_CurrentFocus; } g_LastFocus=g_CurrentFocus; WndSubAttr(g_LastFocus,WS_FOCUS); SendMessage((HWND)g_LastFocus, WM_KILLFOCUS, (DWORD)hWnd, 0); g_CurrentFocus = (PWND)hWnd; WndAddAttr(g_CurrentFocus,WS_FOCUS); SendMessage((HWND)g_CurrentFocus, WM_SETFOCUS, (DWORD)g_LastFocus, 0); g_CurrentFocus->Family->HisFocus=g_CurrentFocus; top = g_LastFocus->Family->TopWnd; top2 = g_CurrentFocus->Family->TopWnd; if(top2 != top) { if(top2->Prev && !WndGetAttr(top2,WS_ALWAYSONBOTTOM) ) { if(WndGetAttr(top2,WS_ALWAYSONTOP) || !WndGetAttr(top2->Prev,WS_ALWAYSONTOP)) { CM_RaiseWindow(top2); } } /* send deactivate*/ SendMessage((HWND)top, WM_ACTIVATE, WA_INACTIVE ,(DWORD)top2); if(WndGetAttr(top,WS_BORDER)) InvalidateNCArea((HWND)top); /* send activate*/ SendMessage((HWND)top2, WM_ACTIVATE, WA_ACTIVE ,(DWORD)top); if(WndGetAttr(top2,WS_BORDER)) InvalidateNCArea((HWND)top2); } return (HWND)g_LastFocus;}//---------------------------------------------------------------------------/* * Check to see if the two windows have overlap area. */#define IsWndOverlap(pWnd1,pWnd2) !(pWnd1)->unmapcount && !(pWnd2)->unmapcount && (pWnd2)->WndRect.right>(pWnd1)->WndRect.left && (pWnd1)->WndRect.right>(pWnd2)->WndRect.left && (pWnd2)->WndRect.bottom>(pWnd1)->WndRect.top && (pWnd1)->WndRect.bottom>(pWnd2)->WndRect.top//---------------------------------------------------------------------------/* * Raise a window to the highest level among its siblings. */void CM_RaiseWindow(PWND pWnd){ PWND sibwp,topwnd; if (!IsWnd(pWnd) || pWnd == g_RootWnd || !pWnd->Prev) return; topwnd = pWnd->Parent->Children; if( WndGetAttr(topwnd,WS_ALWAYSONTOP) && !WndGetAttr(pWnd,WS_ALWAYSONTOP) ) { do { topwnd = topwnd->Next; if (topwnd == pWnd) return; }while (WndGetAttr(topwnd,WS_ALWAYSONTOP)); } /* * Find the sibling just before this window so we can unlink it. * Also, determine if any sibling ahead of us overlaps the window. * Remember that for exposure events. */ sibwp=topwnd; while (sibwp != pWnd) { if(IsWndOverlap(sibwp, pWnd)) { CM_ExposeArea(pWnd,&sibwp->WndRect); } sibwp = sibwp->Next; } /* * Now unlink the window and relink it in at proper position of the sibling chain. */ pWnd->Prev->Next=pWnd->Next; if(pWnd->Next)pWnd->Next->Prev=pWnd->Prev; pWnd->Next=topwnd; pWnd->Prev=topwnd->Prev; if(topwnd->Prev)topwnd->Prev->Next=pWnd; else pWnd->Parent->Children=pWnd; topwnd->Prev=pWnd;}//---------------------------------------------------------------------------/* * Lower a window to the lowest level among its siblings. */void CM_LowerWindow(PWND pWnd){ PWND botwnd; BOOL bLowerNormalWindow; if (!IsWnd(pWnd) || pWnd == g_RootWnd || !pWnd->Next)return; /*walk down the sibling chain looking for the last sibling. */ botwnd = pWnd; bLowerNormalWindow=!WndGetAttr(pWnd,WS_ALWAYSONBOTTOM); while (botwnd->Next) { if( WndGetAttr(botwnd->Next,WS_ALWAYSONBOTTOM) && bLowerNormalWindow)break; botwnd = botwnd->Next; if(IsWndOverlap(botwnd, pWnd)) { CM_ExposeArea(botwnd,&pWnd->WndRect); } } if(botwnd == pWnd)return; /* * Now unlink the window and relink it in at the proper position of the * sibling chain. */ pWnd->Next->Prev=pWnd->Prev; if(pWnd->Prev)pWnd->Prev->Next=pWnd->Next; else pWnd->Parent->Children=pWnd->Next; pWnd->Next=botwnd->Next; if(botwnd->Next) botwnd->Next->Prev=pWnd; pWnd->Prev=botwnd; botwnd->Next=pWnd;} void CM_KillFocus(PWND pWnd){ if(pWnd==g_CurrentFocus && pWnd!=g_RootWnd) { PWND HisFocus; while(pWnd->Parent!=g_RootWnd) { pWnd=pWnd->Parent; if( !WndGetAttr(pWnd,WS_HIDE|WS_DISABLED) ) { SetFocus((HWND)pWnd); return; } } while(pWnd->Prev) { pWnd=pWnd->Prev; } while(pWnd) { HisFocus=pWnd->Family->HisFocus; if(HisFocus!=g_CurrentFocus && !WndGetAttr(pWnd,WS_HIDE|WS_DISABLED) ) { if(IsWnd(HisFocus) && !WndGetAttr(HisFocus,WS_HIDE|WS_DISABLED) ) { SetFocus((HWND)HisFocus); } else { SetFocus((HWND)pWnd); } return; } pWnd=pWnd->Next; } SetFocus((HWND)g_RootWnd); }}HWND CM_GetChildTabStop(HWND hWnd){ PWND pWnd=WNDPTR(hWnd)->Children; while(pWnd) { if(WndGetAttr(pWnd,WS_TABSTOP|WS_DISABLED|WS_HIDE)==WS_TABSTOP)break; else pWnd=pWnd->Next; } if(pWnd) { int minTabOrder=pWnd->TabOrder; PWND seekWnd=pWnd->Next; while(seekWnd) { if(WndGetAttr(seekWnd,WS_TABSTOP|WS_DISABLED|WS_HIDE)==WS_TABSTOP) { if(seekWnd->TabOrder<=minTabOrder) { minTabOrder=seekWnd->TabOrder; pWnd=seekWnd; } } seekWnd=seekWnd->Next; } } return (HWND)pWnd;}HWND CM_GetNextTabStop(HWND hWnd){ if(hWnd) { PWND seekPrevWnd,seekNextWnd,pWnd;
int OrginTabOrder,NextTabOrder,PrevTabOrder;
if(!WNDPTR(hWnd)->Parent || WNDPTR(hWnd)->Parent==g_RootWnd)
{ return CM_GetChildTabStop(hWnd);
}
seekPrevWnd=seekNextWnd=NULL;
PrevTabOrder=NextTabOrder=OrginTabOrder=WNDPTR(hWnd)->TabOrder;
pWnd=WNDPTR(hWnd)->Parent->Children; while(pWnd) { if((HWND)pWnd!=hWnd && WndGetAttr(pWnd,WS_TABSTOP|WS_DISABLED|WS_HIDE)==WS_TABSTOP) { if(pWnd->TabOrder >= OrginTabOrder ) { if(!seekNextWnd || pWnd->TabOrder < NextTabOrder) { if(pWnd->TabOrder==OrginTabOrder+1)return (HWND)pWnd; NextTabOrder=pWnd->TabOrder; seekNextWnd=pWnd; } } else { if(!PrevTabOrder || pWnd->TabOrder < PrevTabOrder) { PrevTabOrder=pWnd->TabOrder; seekPrevWnd=pWnd; } } } pWnd=pWnd->Next; } if(seekNextWnd)return seekNextWnd; else if(seekPrevWnd) return seekPrevWnd; else return CM_GetChildTabStop((HWND)hWnd); } return NULL;}HWND CM_GetPrevTabStop(HWND hWnd){ if(hWnd) { PWND pWnd=(PWND)hWnd; HWND seekPrevWnd=NULL; HWND seekNextWnd=NULL; int OrginTabOrder=pWnd->TabOrder; int NextTabOrder=OrginTabOrder; int PrevTabOrder=OrginTabOrder; pWnd=pWnd->Parent->Children; while(pWnd) { if((HWND)pWnd!=hWnd && WndGetAttr(pWnd,WS_TABSTOP|WS_DISABLED|WS_HIDE)==WS_TABSTOP) { if( pWnd->TabOrder <= OrginTabOrder ) { if(!seekPrevWnd || pWnd->TabOrder > PrevTabOrder) { if(pWnd->TabOrder==OrginTabOrder-1) return (HWND)pWnd; PrevTabOrder=pWnd->TabOrder; seekPrevWnd=(HWND)pWnd; } } else { if(!seekNextWnd || pWnd->TabOrder > NextTabOrder) { NextTabOrder=pWnd->TabOrder; seekNextWnd=(HWND)pWnd; } } } pWnd=pWnd->Next; } return (seekPrevWnd)?seekPrevWnd:seekNextWnd; } return NULL;}void WINAPI SetForegroundWindow(HWND hWnd){ if(hWnd && hWnd!=(HWND)g_RootWnd) { /* raise to top of z order*/ CM_RaiseWindow((PWND)hWnd); }}void WINAPI SetBackgroundWindow(HWND hWnd){ if(hWnd && hWnd!=(HWND)g_RootWnd) { /* lower top level parent to bottom of z order*/ CM_LowerWindow((PWND)hWnd); } }/*---------------------------------------------------------------------------FUNCTION GetWindowTextDESCRIPTION 读取窗口的标题---------------------------------------------------------------------------*/char *GetWindowText(HWND hWnd){ if(!hWnd)return NULL; else if(WNDPTR(hWnd)->Caption.Info.Head==0xFF) { return WNDPTR(hWnd)->Caption.Info.pText; } else return WNDPTR(hWnd)->Caption.Text;}/*---------------------------------------------------------------------------FUNCTION CM_AllocateWindowTextDESCRIPTION 修改/保存窗口的标题到窗体结构变量,并保证最小的标题资源堆栈 返回值表示窗口标题是否被修改。---------------------------------------------------------------------------*/BOOL CM_AllocateWindowText(PWND pWnd,LPCSTR lpString,int heap_size){ BOOL heapused; char *Oldtext; if(pWnd) { heapused=(pWnd->Caption.Info.Head==0xff); Oldtext=(heapused)?pWnd->Caption.Info.pText:pWnd->Caption.Text; } else return false; if(heap_size>0) { int OldHeapSize=(heapused)?pWnd->Caption.Info.Size:sizeof(TVariantText); if(heap_size>OldHeapSize) { if(heapused)FreeMem(pWnd->Caption.Info.pText); pWnd->Caption.Info.Head=0xFF; pWnd->Caption.Info.Stop=0; pWnd->Caption.Info.Size=heap_size; Oldtext=(char *)GetMem(heap_size); pWnd->Caption.Info.pText=Oldtext; } } if(!lpString) { Oldtext[0]='\0'; return true; } else if(strcmp(Oldtext,lpString)!=0) { int oldlen=(strlen(Oldtext) +4)&~0x03;/*包括NULL结束符号空间以及边界对齐的空间隙*/ int newstrlen=strlen(lpString); int newAlocateLen=( ( (heap_size)?heap_size:newstrlen ) +4 ) &~0x03;; if(newAlocateLen>oldlen && newAlocateLen>=FixedWndTextSize) { if(heapused) { if(newAlocateLen > pWnd->Caption.Info.Size) { FreeMem(pWnd->Caption.Info.pText); } else newAlocateLen=0; } else { pWnd->Caption.Info.Head=0xFF; pWnd->Caption.Info.Stop=0; } if(newAlocateLen) { pWnd->Caption.Info.Size=newAlocateLen; Oldtext=(char *)GetMem(newAlocateLen); pWnd->Caption.Info.pText=Oldtext; } } memcpy(Oldtext,lpString,newstrlen); Oldtext[newstrlen]='\0'; return true; } return false;}//---------------------------------------------------------------------------BOOL WINAPI SetWindowText(HWND hWnd,char *lpString){ return SendMessage(hWnd, WM_SETTEXT, 0, (DWORD)lpString);}//---------------------------------------------------------------------------BOOL WINAPI SetWindowLogo(HWND hWnd,TBitmap *LogoBitmap){ return SendMessage(hWnd, WM_SETLOGO, 0, (DWORD)LogoBitmap);}//---------------------------------------------------------------------------void SetWindowTextColor(HWND hWnd,TCOLOR rgbColor){ if(IsWnd(hWnd)) { PIXEL NewTextPixel=ColorMapToPixel((RGBQUAD *)&rgbColor); if(NewTextPixel!= ((PWND)hWnd)->Foreground) { ((PWND)hWnd)->Foreground=NewTextPixel; Invalidate(hWnd); } }}//---------------------------------------------------------------------------void SetWindowBackColor(HWND hWnd,TCOLOR rgbColor){ if(IsWnd(hWnd)) { PIXEL NewTextPixel=ColorMapToPixel((RGBQUAD *)&rgbColor); if(NewTextPixel!= ((PWND)hWnd)->Background) { ((PWND)hWnd)->Background=NewTextPixel; Invalidate(hWnd); } }}/*---------------------------------------------------------------------------FUNCTION FreeWindowTextDESCRIPTION
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -