Click here to Skip to main content
15,885,159 members
Articles / Web Development / HTML

Colorz - The RGB Assistant

Rate me:
Please Sign up or sign in to vote.
3.46/5 (13 votes)
28 Aug 2006MIT 130.7K   1.6K   29  
Aids developers with color intensities
#include "WinMain.h"		/*/ Standard application includes /*/

/*/
/ / BOOL
/ /		CenterWnd (HWND hwnd, HWND hwndPreferred)
/ /
/ / PURPOSE:
/ /		Function to center a window, either in correspondence
/ /		with a preferred window, a parent window, or the work area.
/ /
/ /	CREDITS:
/ /		This function originated as the CenterWindow function found in the
/ /		book "Win32 Programming" by Brent E. Rector and Joseph M. Newcomer.
/ /		I, Jeremy Falcon, modified it to compile cleanly under warning level
/ /		4 and to gain a slight performance increase under certain conditions.
/*/

BOOL
CenterWnd (HWND hwnd, HWND hwndPreferred)
{
	BOOL	result;
	POINT	PreferredPoint, CenteredPoint;
	RECT	WndRect, PreferredRect, WorkArea = {0,0,0,0};
	SIZE	CenteredWnd;

	/*/ The macros used here are defined in windowx.h /*/

	if(NULL == hwnd)	return FALSE;
	if(!IsWindow(hwnd))	return FALSE;

	/*/ See if a preferred window is specified /*/
	if(NULL == hwndPreferred)
		/*/ See if window to be centered is a child /*/
		hwndPreferred = GetWindowOwner(hwnd);

	/*/ Center around the preferred window if exists; otherwise, center in the work area /*/
    if(NULL != hwndPreferred)
	{
        if(!IsWindow(hwndPreferred)) return FALSE;
        GetWindowRect(hwndPreferred, &PreferredRect);
    }
    else
	{
		/*/ Get the rectangle for the work area /*/
		result = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &WorkArea, 0);

		/*/
		/ / If the above call failed Explorer is not running correctly;
		/ / therefore, there is no work area (and taskbar). So, we'll
		/ / use the screen size instead for our coordinates.
		/*/

		if(!result)
		{
			SetRect(&WorkArea, 0, 0, 
					GetSystemMetrics (SM_CXSCREEN),
					GetSystemMetrics (SM_CYSCREEN));
		}

        PreferredRect = WorkArea;
	}

    /*/ Preferred center point /*/
    PreferredPoint.x = (PreferredRect.left + PreferredRect.right)  / 2;
    PreferredPoint.y = (PreferredRect.top  + PreferredRect.bottom) / 2;

    /*/ Get the window's (to be centered) rectangle and compute the width/height /*/
    GetWindowRect(hwnd, &WndRect);
    CenteredWnd.cx = (WndRect.right  - WndRect.left);
    CenteredWnd.cy = (WndRect.bottom - WndRect.top);

	/*/ Center window in preferred horizontally (left side) /*/
    CenteredPoint.x = PreferredPoint.x - (CenteredWnd.cx / 2);

	/*/ Center window in owner vertically (top side) /*/
    CenteredPoint.y = PreferredPoint.y - (CenteredWnd.cy / 2);

    /*/
	/ / If the left edge of the centered window is clipped by the work area
	/ / move the window to the right until the edge is exposed.
	/*/
	if(CenteredPoint.x < WorkArea.left) CenteredPoint.x = WorkArea.left;

	/*/
	/ / If the right edge of the centered window is clipped by the work area
	/ / move the window to the left until the edge is exposed.
	/*/
	else if(CenteredPoint.x + CenteredWnd.cx > WorkArea.right)
		CenteredPoint.x = (WorkArea.right - CenteredWnd.cx);

	/*/
	/ / If the top edge of the centered window is clipped by the work area
	/ / move the window down until the edge is exposed.
	/*/
	if (CenteredPoint.y < WorkArea.top) CenteredPoint.y = WorkArea.top;

	/*/
	/ / If the bottom edge of the centered window is clipped by the work area
	/ / move the window up until the edge is exposed.
	/*/
	else if (CenteredPoint.y + CenteredWnd.cy > WorkArea.bottom)
		CenteredPoint.y = (WorkArea.bottom - CenteredWnd.cy);

    /*/ Reposition the centered window /*/
	return SetWindowPos (hwnd, NULL, CenteredPoint.x, CenteredPoint.y,
						 0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER);
}

/*/
/ / BOOL
/ /		AddClipboardText (HWND hwnd, char * szText)
/ /
/ /			hwnd		Window handle for calling window
/ /			szText		Pointer to a null-terminated string
/ /						containing the text to add to the clipboard
/ /
/ / PURPOSE:
/ /		Adds the contents pointed to by szText to the
/ /		Windows clipboard. Returns FALSE if unsuccessful.
/*/

BOOL AddClipboardText(HWND hwnd, char * szText)
{
	/*/ post data to clipboard /*/
	if( OpenClipboard(hwnd) )
	{
		HGLOBAL GlobalBuff;
		char * buffer;

		//szText += '\0';
		EmptyClipboard();

		/*/ use GMEM_DDESHARE if you want Win32s compatiblity /*/
		GlobalBuff = GlobalAlloc(GMEM_MOVEABLE,strlen(szText)+1);

		buffer = (char*)GlobalLock(GlobalBuff);
		/*/ ensure we are working with a null-terminated string /*/
		szText[strlen(szText)+1] = '\0';
		strcpy(buffer, szText);
		GlobalUnlock(GlobalBuff);

		if(SetClipboardData(CF_TEXT,GlobalBuff) == NULL) return FALSE;
		CloseClipboard();
	}
	else
		return FALSE;

	return TRUE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Team Leader
United States United States
I've been in software development since 1994. Over the years I've learned quite a lot in what it takes to complete the process of pushing out a quality product to customers in a timely fashion. As most of my colleagues could attest, there have been many challenges in our new and growing field in the past couple of decades as the industry matures rapidly. Much more so than most others historically speaking.

As such, I've learned one of the best aspects of software engineering is embracing the change that inherently comes along with it as new technologies constantly emerge to help us improve our world one application at a time as we make sense of the overwhelming amount of data now prevalent in the Information Age.

We truly live in a time unlike that ever known to mankind in recorded history, and it is my hope to do my part to help it along to face the challenges and demands of tomorrow.

Comments and Discussions