⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 docking.cpp

📁 mp3 频谱分析源码, mp3播放器开发必备源码,
💻 CPP
字号:
#include "stdafx.h"
#include "docking.h"

void dock(int *x, int *y, int w, int h, int ox, int oy, int ow, int oh, int snapd);

// specialised function which causes the window specified to dock
// with any of the winamp windows. x & y are pointers to the current
// position of the window, they will be modified for the dock, sd
// is the snapping distance.
void winamp_dock(HWND hwnd, int* x, int* y, int sd)
{
		int newx;
		int newy;
		int w; // window width
		int h; // window height
		int sw; // screen width
		int sh; // screen height
		
		RECT rect;
		RECT rect_wa; // winamp rect
		RECT rect_eq; // eq rect
		RECT rect_pe; // playlist editor rect
		RECT rect_mb; // minibrowser rect

		HWND wa_hwnd;
		
		bool bvis_wa=false;
		bool bvis_eq=false;
		bool bvis_pe=false;
		bool bvis_mb=false;

		// get winamp window rects and visibility state
		wa_hwnd = FindWindow(WINAMP_EQ,NULL);
		bvis_eq = IsWindowVisible(wa_hwnd)!=0;
		GetWindowRect(wa_hwnd,&rect_eq);
		wa_hwnd = FindWindow(WINAMP_PE,NULL);
		bvis_pe = IsWindowVisible(wa_hwnd)!=0;
		GetWindowRect(wa_hwnd,&rect_pe);
		wa_hwnd = FindWindow(WINAMP_MB,NULL);
		bvis_mb = IsWindowVisible(wa_hwnd)!=0;
		GetWindowRect(wa_hwnd,&rect_mb);
		wa_hwnd = (HWND)GetWindowLong(hwnd,GWL_HWNDPARENT);
		bvis_wa = IsWindowVisible(wa_hwnd)!=0;
		GetWindowRect(wa_hwnd,&rect_wa);

		// set our variables
		newx = *x;
		newy = *y;

		// get our own rect
		GetWindowRect(hwnd,&rect);
		
		// use w and h for simplicity
		w  = rect.right - rect.left;
		h  = rect.bottom - rect.top;

		// get the work area and leave rect with that value
		SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);

		// get work area width/height, not screen.
		sw = rect.right;
		sh = rect.bottom;
	
		// dock to work area border
		if (newx > (-sd+rect.left) && newx < (sd+rect.left)) newx = rect.left;
		if ((newx + w) > sw - sd && (newx + w) < sw + sd) newx = sw - w;
		if (newy > (-sd+rect.top) && newy < (sd+rect.top)) newy = rect.top;
		if ((newy + h) > sh - sd && (newy + h) < sh + sd) newy = sh - h;

		// dock to winamp windows
		if(bvis_wa) dock(&newx,&newy,w,h,rect_wa.left,rect_wa.top,rect_wa.right - rect_wa.left,rect_wa.bottom - rect_wa.top,sd);
		if(bvis_eq) dock(&newx,&newy,w,h,rect_eq.left,rect_eq.top,rect_eq.right - rect_eq.left,rect_eq.bottom - rect_eq.top,sd);
		if(bvis_pe) dock(&newx,&newy,w,h,rect_pe.left,rect_pe.top,rect_pe.right - rect_pe.left,rect_pe.bottom - rect_pe.top,sd);
		if(bvis_mb) dock(&newx,&newy,w,h,rect_mb.left,rect_mb.top,rect_mb.right - rect_mb.left,rect_mb.bottom - rect_mb.top,sd);
		
		*x = newx;
		*y = newy;
}

// docks a window with any of the rectangles listed in ar_rect
// and also the edges of the work area. This is a generalisation
// of the above function.
void rect_list_dock(HWND hwnd, int* x, int* y, int sd, LPRECT ar_rect, int rects)
{
		int newx;
		int newy;
		int w; // window width
		int h; // window height
		int sw; // screen width
		int sh; // screen height
		
		RECT rect;
		
		// set our variables
		newx = *x;
		newy = *y;

		// get our own rect
		GetWindowRect(hwnd,&rect);
		
		w  = rect.right - rect.left;
		h  = rect.bottom - rect.top;

		// get the work area and leave rect with that value
		SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);

		// get work area width/height, not screen.
		sw = rect.right;
		sh = rect.bottom;
	
		// dock to work area border
		if (newx > (-sd+rect.left) && newx < (sd+rect.left)) newx = rect.left;
		if ((newx + w) > sw - sd && (newx + w) < sw + sd) newx = sw - w;
		if (newy > (-sd+rect.top) && newy < (sd+rect.top)) newy = rect.top;
		if ((newy + h) > sh - sd && (newy + h) < sh + sd) newy = sh - h;

		// dock to the list of rectangles
		for(int i=0;i<rects;i++) {
			dock(&newx,&newy,w,h,ar_rect[i].left,ar_rect[i].top,ar_rect[i].right - ar_rect[i].left,ar_rect[i].bottom - ar_rect[i].top,sd);
		}

		*x = newx;
		*y = newy;
}


// function which calculates the whether to dock or not and sets
// the x and y parameters accordingly. This code was actually found
// in an xmms (X Multi Media System) plugin source (www.xmms.org), sorry
// to whoever i pinched it from, it does a great job.
void dock(int *x, int *y, int w, int h, int ox, int oy, int ow, int oh, int snapd)
{
	if (*x + w > ox - snapd && *x + w < ox + snapd && *y > oy - h && *y < oy + oh) {
		*x = ox - w;
		if (*y > oy - snapd && *y < oy + snapd) *y = oy;
		if (*y + h > oy + oh - snapd && *y + h < oy + oh + snapd) *y = oy + oh - h;
	}
	if (*x > ox + ow - snapd && *x < ox + ow + snapd && *y > oy - h && *y < oy + oh) {
		*x = ox + ow;
		if (*y > oy - snapd && *y < oy + snapd) *y = oy;
		if (*y + h > oy + oh - snapd && *y + h < oy + oh + snapd) *y = oy + oh - h;
	}
	if (*y + h > oy - snapd && *y + h < oy + snapd && *x > ox - w && *x < ox + ow) {
		*y = oy - h;
		if (*x > ox - snapd && *x < ox + snapd) *x = ox;
		if (*x + w > ox + ow - snapd && *x + w < ox + ow + snapd) *x = ox + ow - w;
	}
	if (*y > oy + oh - snapd && *y < oy + oh + snapd && *x > ox - w && *x < ox + ow) {
		*y = oy + oh;
		if (*x > ox - snapd && *x < ox + snapd) *x = ox;
		if (*x + w > ox + ow - snapd && *x + w < ox + ow + snapd) *x = ox + ow - w;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -