Click here to Skip to main content
15,886,069 members
Articles / Containers / Virtual Machine

An extendable report editor

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
3 Sep 2008CPOL3 min read 41K   2K   35  
An extendable report editor. You can simply add your own controls without recompiling the program or writing annoying plug-ins.
// WndSplit.cpp: implementation of the CWndSplit class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WndSplit.h"
#include "mem_tool.h"
#include "global.h"
#include "canvas.h"

#define SPLIT_WND_CLASS_NAME "split_simple_wnd"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWndSplit::CWndSplit()
{
    this->InitBasic();
}
CWndSplit::~CWndSplit()
{
    this->Destroy();
}
int CWndSplit::InitBasic()
{
	CWnd::InitBasic();

	m_nWidth = 8;
	m_hbm = NULL;
	m_hbrDithered = NULL;
	m_nOldBarPos = 0;
	m_nBarpreMove = 0;
	this->m_nBarPos = 0;
	this->m_bCapture = false;
	this->m_bVertical = true;
	this->pane0 = NULL;
	this->pane1 = NULL;
	
	memset(&this->border_rect,0,sizeof(RECT));

    return OK;
}
int CWndSplit::Init()
{
    this->InitBasic();
	CWnd::Init();
	
	m_nWidth = 6;
	WORD HashPattern[] = {0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA};
	m_hbm = ::CreateBitmap (8, 8, 1, 1, HashPattern);
	m_hbrDithered = ::CreatePatternBrush (m_hbm);
	m_nOldBarPos = m_nBarPos;
	m_nBarpreMove = m_nBarPos;
	this->m_bCapture = false;
	this->m_bVertical = true;
	
	this->wnd_create->style = WS_CHILD|WS_VISIBLE;	
	this->wnd_create->class_name = SPLIT_WND_CLASS_NAME;

    return OK;
}

int  CWndSplit::Destroy()
{
	if(this->m_hbrDithered)
	{
		::DeleteObject(m_hbrDithered);
		this->m_hbrDithered = NULL;
	}
	if(this->m_hbm)
	{
		::DeleteObject(m_hbm);
		this->m_hbm = NULL;
	}
	
	CWnd::Destroy();
    this->InitBasic();
    return OK;
}
int CWndSplit::OnLButtonDown(WPARAM wparam,LPARAM lparam)
{
	::SetCapture(hwnd);
	m_bCapture = true;
	DrawBar(m_nBarPos);
	m_nBarpreMove = m_nBarPos;

	return OK;
}
int CWndSplit::OnPaint(WPARAM wparam,LPARAM lparam)
{	
	CCanvas can;
	PAINTSTRUCT ps;
	RECT r;

	can.Init();

	HDC hdc = ::BeginPaint(this->hwnd,&ps);

	::GetClientRect(this->hwnd,&r);
	can.AttachDC(hdc);	
	can.SetPen(PS_SOLID,0,RGB(120,152,181));
	
	can.MoveTo(0,0);can.LineTo(0,r.bottom);
	can.MoveTo(r.right-1,0);can.LineTo(r.right-1,r.bottom);

	::EndPaint(this->hwnd,&ps);

	return OK;
}
int CWndSplit::OnLButtonUp(WPARAM wparam,LPARAM lparam)
{
	DrawBar(m_nBarPos);
	::ReleaseCapture();
	
	RecalcLayout(this->border_rect);
	m_bCapture = false;

	SendMessage(this->wnd_create->hparent,WM_SPLIT_DRAG_END,0,(LPARAM)this->hwnd);

	return OK;
}
int CWndSplit::OnMouseMove(WPARAM wparam,LPARAM lparam)
{
	if (m_bCapture)
	{
		POINT pt = {0};
		static POINT Oldpt = {0};
		::GetCursorPos(&pt);
		::ScreenToClient (this->wnd_create->hparent, &pt);

		if ((pt.x == Oldpt.x) && (pt.y == Oldpt.y))
			return ERROR;

		Oldpt.x = pt.x;
		Oldpt.y = pt.y;

		int cx = this->border_rect.right - this->border_rect.left;
		int cy = this->border_rect.bottom - this->border_rect.top;

		if (m_bVertical)
		{
			m_nBarPos = pt.x;
			if (m_nBarPos < m_nWidth/2)
				m_nBarPos = m_nWidth/2;

			if (m_nBarPos > (cx - m_nWidth/2))
				m_nBarPos = cx - m_nWidth/2;
		}
		else
		{
			m_nBarPos = pt.y;
			if (m_nBarPos < (m_nWidth/2))
				m_nBarPos = m_nWidth/2;

			if (m_nBarPos > (cy - m_nWidth/2))
				m_nBarPos = cy - m_nWidth/2;
		}

		DrawBar(m_nOldBarPos);
		DrawBar(m_nBarPos);

		m_nOldBarPos = m_nBarPos;
	}

	return OK;
}
int CWndSplit::Registe()
{       
    WNDCLASSEX wcex;    

    wcex.cbSize             = sizeof(WNDCLASSEX); 
    wcex.style              = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc        = (WNDPROC)WndProc;
    wcex.cbClsExtra         = 0;
    wcex.cbWndExtra         = 0;
    wcex.hInstance          = ::GetModuleHandle(NULL);
    wcex.hIcon              = NULL;
	if (m_bVertical)
	    wcex.hCursor            = ::LoadCursor (NULL, IDC_SIZEWE);
	else
		wcex.hCursor            = ::LoadCursor (NULL, IDC_SIZENS);
    wcex.hbrBackground      = (HBRUSH)(COLOR_BTNFACE+1);
    wcex.lpszMenuName       = NULL;
    wcex.lpszClassName      = SPLIT_WND_CLASS_NAME;
    wcex.hIconSm            = NULL;
    
	RegisterClassEx(&wcex);     
    
	return OK;
}

int CWndSplit::DrawBar(int Pos)
{
	if (m_bCapture)
	{
		HDC hDC = ::GetDC(this->wnd_create->hparent);
		HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, (HBRUSH)m_hbrDithered);

		int cx = this->border_rect.right - this->border_rect.left;
		int cy = this->border_rect.bottom - this->border_rect.top;

		if (m_bVertical)
			::PatBlt (hDC, Pos - m_nWidth/2, this->border_rect.top, m_nWidth, cy, PATINVERT);
		else
			::PatBlt (hDC, this->border_rect.left, Pos - m_nWidth/2, cx, m_nWidth, PATINVERT);

		// Clean up
		::SelectObject(hDC, hOldBrush);
		::ReleaseDC(this->wnd_create->hparent, hDC);
	}

	return OK;
}

int CWndSplit::RecalcLayout(RECT r)
{

	int cx = r.right - r.left;
	int cy = r.bottom - r.top;
	int w2 = m_nWidth / 2;
	
	memcpy(&this->border_rect,&r,sizeof(RECT));

	if (m_bVertical)
	{
		// Reposition our window panes and bar
		::SetWindowPos(pane0, NULL, 0, 0, m_nBarPos - w2, cy, SWP_SHOWWINDOW );
		::SetWindowPos(pane1, NULL, m_nBarPos - w2 + m_nWidth, r.top, cx - m_nBarPos - m_nWidth + w2, cy, SWP_SHOWWINDOW );
		::SetWindowPos(this->hwnd, NULL, m_nBarPos - w2, r.top, m_nWidth, cy, SWP_SHOWWINDOW );
	}
	else
	{
		// Reposition our window panes and bar
		::SetWindowPos(pane0, NULL, 0, 0, cx, m_nBarPos - w2, SWP_SHOWWINDOW );
		::SetWindowPos(pane1, NULL, 0, m_nBarPos - w2 + m_nWidth, cx, cy - m_nBarPos - m_nWidth + w2, SWP_SHOWWINDOW );
		::SetWindowPos(this->hwnd, NULL, r.top, m_nBarPos - w2, cx, m_nWidth , SWP_SHOWWINDOW );
	}

	return OK;
}

int CWndSplit::RecalcLayout()
{
	if(hwnd == NULL)
		return ERROR;

	::GetClientRect(this->wnd_create->hparent, &this->border_rect);
	this->RecalcLayout(this->border_rect);

	return OK;
}

int CWndSplit::SetBarPos(int pos)
{
	this->m_nOldBarPos = pos;
	this->m_nBarPos = pos;
	this->RecalcLayout(this->border_rect);

	return OK;
}

int CWndSplit::GetPos() 
{
	return this->m_nBarPos;
}

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 Code Project Open License (CPOL)


Written By
Software Developer
China China
26 years old, 2 years work experience.

Comments and Discussions