?? interface.cpp
字號:
#include"interface.h"
#include"resource.h"
bool DockCtrl::IsClassRegistered=false;
HMENU DockCtrl::hMenu=NULL;
#define POPUP_STYLES (WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_SYSMENU | WS_THICKFRAME)
#define POPUP_EXSTYLES (WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE)
LRESULT DockCtrl::CtrlWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int i;
switch( uMsg )
{
case WM_CREATE:
SetTimer(hWnd,1000,200,NULL);
break;
case WM_PARENTPOSCHANGED:
AdjustWnd();
for(i=0;i<Container.length();i++)
{
SetWindowState(Container[i].hWnd);
}
return 0;
case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
OnDraw(ps.hdc);
EndPaint(hWnd, &ps);
return 0;
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd,wParam,lParam);
break;
case WM_TIMER:
OnMouseMove(hWnd,wParam,lParam);
break;
case WM_NCACTIVATE:
SendMessage(Parent, WM_NCACTIVATE, true, (LONG)-1);
return DefWindowProc(hWnd, WM_NCACTIVATE, true, (LONG)-1);
case WM_DESTROY:
KillTimer(hWnd,1001);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK DockCtrl::CtrlWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DockCtrl * pWindow;
if ( uMsg==WM_NCCREATE )
{
assert( ! IsBadReadPtr((void *) lParam, sizeof(CREATESTRUCT)) );
MDICREATESTRUCT * pMDIC = (MDICREATESTRUCT *) ((LPCREATESTRUCT) lParam)->lpCreateParams;
pWindow = (DockCtrl *) (pMDIC->lParam);
assert( ! IsBadReadPtr(pWindow, sizeof(DockCtrl)) );
SetWindowLong(hWnd, GWL_USERDATA, (LONG) pWindow);
}
else
pWindow=(DockCtrl *)GetWindowLong(hWnd, GWL_USERDATA);
if ( pWindow )
return pWindow->CtrlWndProc(hWnd, uMsg, wParam, lParam);
else
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT DockCtrl::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// UINT uHitTest;
POINT point;
RECT rect;
int i;
WndInfo *WI;
WI=GetWI(hWnd);
if(WI!=NULL)
WI->lpfnWndProc(hWnd,uMsg,wParam,lParam);
switch( uMsg )
{
case WM_TIMER:
if(!WI->AutoHide||DWLButtonFlag)
return 0;
GetCursorPos(&point);
GetClientRect(rect);
ScreenToClient(hWnd,&point);
if(point.x>rect.left&&point.x<rect.right
&&point.y>rect.top&&point.y<rect.bottom)
return 0;
for(i=0;i<Container.length();i++)
if(Container[i].State==DOCK_POPUP)
PopIn(Container[i].hWnd);
return 0;
/* case WM_NCHITTEST: //若用戶單擊客戶區,則將其作為單擊工具欄處理
uHitTest = DefWindowProc(hWnd, WM_NCHITTEST, wParam, lParam);
if(uHitTest == HTCLIENT)
uHitTest = HTCAPTION;
return uHitTest;*/
case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
OnDWDraw(ps.hdc,hWnd);
EndPaint(hWnd, &ps);
return 0;
case WM_LBUTTONDOWN:
OnDWLButtonDown(hWnd,wParam,lParam);
return 0;
case WM_MOUSEMOVE:
if(DWLButtonFlag)
OnDWMouseMove(hWnd,wParam,lParam);
return 0;
case WM_LBUTTONUP:
if(DWLButtonFlag)
OnDWLButtonUp(hWnd,wParam,lParam);
return 0;
case WM_RBUTTONDOWN:
point.x = LOWORD (lParam) ;
point.y = HIWORD (lParam) ;
ClientToScreen (hWnd, &point) ;
TrackPopupMenu (GetSubMenu(hMenu,0), TPM_RIGHTBUTTON, point.x, point.y,
0, hWnd, NULL) ;
return 0;
case WM_COMMAND:
switch(wParam)
{
case IDM_HIDE:
if(WI->State==DOCK_POPUP)
{
PopIn(hWnd);
}
break;
case IDM_POPUP:
if(WI->State==DOCK_HIDE)
{
PopUp(hWnd);
}
break;
case IDM_AUTOHIDE:
if(WI->AutoHide)
{
WI->AutoHide=false;
CheckMenuItem(hMenu,IDM_AUTOHIDE,MF_UNCHECKED);
}
else
{
WI->AutoHide=true;
CheckMenuItem(hMenu,IDM_AUTOHIDE,MF_CHECKED);
}
break;
}
break;
case WM_NCACTIVATE:
SendMessage(Parent, WM_NCACTIVATE, true, (LONG)-1);
return DefWindowProc(hWnd, WM_NCACTIVATE, true, (LONG)-1);
case WM_DESTROY:
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK DockCtrl::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DockCtrl * pWindow;
if ( uMsg==WM_NCCREATE )
{
assert( ! IsBadReadPtr((void *) lParam, sizeof(CREATESTRUCT)) );
MDICREATESTRUCT * pMDIC = (MDICREATESTRUCT *) ((LPCREATESTRUCT) lParam)->lpCreateParams;
pWindow = (DockCtrl *) (pMDIC->lParam);
assert( ! IsBadReadPtr(pWindow, sizeof(DockCtrl)) );
SetWindowLong(hWnd, GWL_USERDATA, (LONG) pWindow);
}
else
pWindow=(DockCtrl *)GetWindowLong(hWnd, GWL_USERDATA);
if ( pWindow )
return pWindow->WndProc(hWnd, uMsg, wParam, lParam);
else
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void DockCtrl::AdjustWnd()
{
int CtrlWndWidth;
if(State==STATE_IN)
CtrlWndWidth=10;
else
CtrlWndWidth=GetCtrlWndLength();
RECT rect;
POINT point;
GetClientRect(rect);
point.x=rect.right-CtrlWndWidth;
point.y=YPos;
ClientToScreen(Parent,&point);
MoveWindow(hWnd,point.x,point.y,CtrlWndWidth,20,true);
}
WndInfo* DockCtrl::GetWI(HWND hwnd)
{
for(int i=0;i<Container.length();i++)
if(Container[i].hWnd==hwnd)
return &Container[i];
return NULL;
}
void DockCtrl::SetWindowState(HWND hwnd)
{
WndInfo* WI=GetWI(hwnd);
RECT rect;
GetClientRect(rect);
POINT point;
int Width,Height;
switch(WI->DockType)
{
case DOCK_LEFT:
if(WI->State==DOCK_HIDE)
{
point.x=rect.left;
point.y=WI->Y;
Width=15;
Height=WI->Height;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x-1,point.y,Width,Height,true);
}
else
{
point.x=rect.left;
point.y=WI->Y;
Width=WI->Width;
Height=WI->Height;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y,Width,Height,true);
}
break;
case DOCK_RIGHT:
if(WI->State==DOCK_HIDE)
{
point.x=rect.right;
point.y=WI->Y;
Width=6;
Height=WI->Height;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x-6,point.y,Width,Height,true);
}
else
{
point.x=rect.right;
point.y=WI->Y;
Width=WI->Width;
Height=WI->Height;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y,Width,Height,true);
}
break;
case DOCK_TOP:
if(WI->State==DOCK_HIDE)
{
point.x=WI->X;
point.y=rect.top;
Width=WI->Width;
Height=6;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y,Width,Height,true);
}
else
{
point.x=WI->X;
point.y=rect.top;
Width=WI->Width;
Height=WI->Height;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y,Width,Height,true);
}
break;
case DOCK_BOTTOM:
if(WI->State==DOCK_HIDE)
{
point.x=WI->X;
point.y=rect.bottom;
Width=WI->Width;
Height=15;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y-15,Width,Height,true);
}
else
{
point.x=WI->X;
point.y=rect.bottom;
Width=WI->Width;
Height=WI->Height;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y,Width,Height,true);
}
break;
case NODOCK:
point.x=WI->X;
point.y=WI->Y;
Width=WI->Width;
Height=WI->Height;
if(Width+point.x>rect.right)
Width=rect.right-point.x;
if(Height+point.y>rect.bottom)
Height=rect.bottom-point.y;
ClientToScreen(Parent,&point);
MoveWindow(hwnd,point.x,point.y,Width,Height,true);
break;
}
}
bool DockCtrl::Add(HINSTANCE hInst,char* name,
int x,int y,int width,int height,UINT docktype,UINT dockstyle,
WNDPROC lpfnwndproc,bool af)
{
if(Parent==NULL)
return false;
MDICREATESTRUCT mdic;
memset(& mdic, 0, sizeof(mdic));
mdic.lParam = (LPARAM) this;
HWND hwnd=CreateWindowEx(POPUP_EXSTYLES,"DockWnd",name,
POPUP_STYLES,
x,y,
width,height,
Parent,NULL,
hInst,
&mdic);
if(hwnd==NULL)
return false;
WndInfo WI(hwnd,name,x,y,width,height,docktype,dockstyle,lpfnwndproc,af);
Container.add(WI);
WI.lpfnWndProc(hwnd,WM_CREATE,0,0);
AdjustWnd();
SetWindowState(hwnd);
::ShowWindow(hwnd,SW_SHOW);
::UpdateWindow(hwnd);
return true;
}
bool DockCtrl::RegisterClass(HINSTANCE hInst)
{
if(IsClassRegistered)
return true;
hMenu = LoadMenu (hInst, MAKEINTRESOURCE(IDM_DWMENU));
CheckMenuItem(hMenu,IDM_AUTOHIDE,MF_CHECKED);
WNDCLASSEX wc;
memset(&wc, 0, sizeof(wc));
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=NULL;
wc.hInstance=hInst;
wc.lpfnWndProc=CtrlWindowProc;
wc.lpszClassName="DockCtrl";
wc.lpszMenuName=NULL;
wc.style=0;
wc.hIconSm=NULL;
if(!::RegisterClassEx(&wc))
return false;
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=NULL;
wc.hInstance=hInst;//GetModuleHandle(0);
wc.lpfnWndProc=WindowProc;
wc.lpszClassName="DockWnd";
wc.lpszMenuName=NULL;
wc.style=0;
wc.hIconSm=NULL;
if(!::RegisterClassEx(&wc))
return false;
IsClassRegistered=true;
return true;
}
bool DockCtrl::CreateEx(HINSTANCE hInst,HWND parent,RECT prect,int ypos)
{
if(parent==NULL)
return false;
Left=prect.left;
Right=prect.right;
Bottom=prect.bottom;
Top=prect.top;
YPos=ypos;
RECT rect;
Parent=parent;
GetClientRect(rect);
if(!RegisterClass(hInst))
{
MessageBox(parent,"注冊類失敗,無法創建控件!",
"錯誤",MB_OK|MB_ICONERROR);
return false;
}
MDICREATESTRUCT mdic;
memset(& mdic, 0, sizeof(mdic));
mdic.lParam = (LPARAM) this;
POINT lt;
lt.x=rect.right-10;
lt.y=ypos;
ClientToScreen(Parent,<);
hWnd=CreateWindowEx(NULL,"DockCtrl",NULL,
WS_POPUP,lt.x,lt.y,
10,20,Parent,NULL,hInst,&mdic);
if(hWnd==NULL)
{
MessageBox(parent,"創建窗口失敗!",
"錯誤",MB_OK|MB_ICONERROR);
return false;
}
return true;
}
/******************************************************************************\
*
* 函數名: Draw3DBar
*
* 參數: hdc - 設備上下文
* x - 矩形左上角x坐標
* y - 矩形左上角y坐標
* width - 矩形寬
* height - 矩形高
* color1 - 左上兩條線顏色
* color2 - 右下兩條線顏色
*
* 返回值: 無返回值
*
\******************************************************************************/
void Draw3DBar(HDC hdc,int x,int y,int width,int height,COLORREF color1,COLORREF color2)
{
HPEN hPen1,hPen2,hOldPen;
hPen1=CreatePen(PS_SOLID,1,color1);
hPen2=CreatePen(PS_SOLID,1,color2);
hOldPen=(HPEN)SelectObject(hdc,hPen1);
MoveToEx(hdc,x,y,NULL);
LineTo(hdc,x,height);
MoveToEx(hdc,x,y,NULL);
LineTo(hdc,width,y);
SelectObject(hdc,hPen2);
MoveToEx(hdc,width,height,NULL);
LineTo(hdc,width,y);
MoveToEx(hdc,width,height,NULL);
LineTo(hdc,x,height);
SelectObject(hdc,hOldPen);
DeleteObject(hPen1);
DeleteObject(hPen2);
DeleteObject(hOldPen);
}
void DrawSeparetor(HDC hdc,int x)
{
HPEN hPen1,hPen2,hOldPen;
hPen1=CreatePen(PS_SOLID,1,RGB(128,128,128));
hPen2=CreatePen(PS_SOLID,1,RGB(255,255,255));
hOldPen=(HPEN)SelectObject(hdc,hPen1);
MoveToEx(hdc,x,2,NULL);
LineTo(hdc,x,17);
SelectObject(hdc,hPen2);
MoveToEx(hdc,x+1,2,NULL);
LineTo(hdc,x+1,17);
SelectObject(hdc,hOldPen);
DeleteObject(hPen1);
DeleteObject(hPen2);
DeleteObject(hOldPen);
}
void DockCtrl::OnMouseMove(HWND hwnd,WPARAM wParam,LPARAM lParam)
{
int x,y;
POINT point;
GetCursorPos(&point);
ScreenToClient(hwnd,&point);
x=point.x;
y=point.y;
if(y<0||y>20)
{
Focus=-1;
InvalidateRect(hwnd,NULL,false);
return;
}
if(x>0&&x<10)
{
Focus=0;
InvalidateRect(hwnd,NULL,false);
return;
}
int Start=10;
for(int i=0;i<Container.length();i++)
{
if(x>Start&&x<Start+Container[i].GetNameLength())
{
Focus=i+1;
InvalidateRect(hwnd,NULL,false);
return;
}
Start+=Container[i].GetNameLength();
}
Focus=-1;
InvalidateRect(hwnd,NULL,false);
}
void DockCtrl::OnLButtonDown(HWND hwnd,WPARAM wParam,LPARAM lParam)
{
int x,y;
Focus=-1;
POINT point;
GetCursorPos(&point);
ScreenToClient(hwnd,&point);
x=point.x;
y=point.y;
if(y<0||y>20)
return;
if(x>0&&x<10)
{
if(State==STATE_OUT)
{
State=STATE_IN;
}
else
{
State=STATE_OUT;
}
AdjustWnd();
InvalidateRect(hwnd,NULL,true);
}
int Start=10;
for(int i=0;i<Container.length();i++)
{
if(x>Start&&x<Start+Container[i].GetNameLength())
{
if(Container[i].State==DOCK_HIDE)
PopUp(Container[i].hWnd);
else
PopIn(Container[i].hWnd);
break;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -