Click here to Skip to main content
15,885,767 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.
// Wnd.cpp: implementation of the CWnd class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Wnd.h"
#include "wndmap.h"
#include "assert.h"
#include "syslog.h"
#include "mem_tool.h"
#include "notepad.h"

#define DEF_WND_CLASS "my_def_window"

//a global window handle map
CWndMap wnd_map;  
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWnd::CWnd()
{
    this->InitBasic();
}
CWnd::~CWnd()
{
    this->Destroy();
}
int CWnd::InitBasic()
{
	this->hwnd = NULL;
	this->is_attached = FALSE;
	this->wnd_create = NULL;
	this->id = 0;

    return OK;
}
int CWnd::Init()
{
    this->InitBasic();
    
	NEW(this->wnd_create,WndCreate);
	memset(this->wnd_create,0,sizeof(WndCreate));

	this->wnd_create->class_name = DEF_WND_CLASS;
	this->wnd_create->x = 100;
	this->wnd_create->y = 100;
	this->wnd_create->w = 300;
	this->wnd_create->h = 200;
	this->wnd_create->style = WS_VISIBLE|WS_OVERLAPPEDWINDOW;
	this->wnd_create->hinst = GetModuleHandle(NULL);	
	this->wnd_create->id = (HMENU)this->GetUniqueID();

    return OK;
}
int  CWnd::Destroy()
{
    DEL(this->wnd_create);

	if(this->is_attached)
	{
		this->hwnd = NULL;
	}
	else
	{
		if(this->hwnd)
		{
			::DestroyWindow(this->hwnd);
			wnd_map.Del(this->hwnd);
			this->hwnd = NULL;
		}
	}
    
	this->InitBasic();

    return OK;
}
int CWnd::Create()
{
	ASSERT(this->hwnd == NULL);

	WndCreate *w = this->wnd_create;

	wnd_map.SetTempPtr(this);
    this->hwnd = CreateWindowEx(w->ex_style,
                                w->class_name,
                                w->title,
                                w->style,
                                w->x,w->y,w->w,w->h,
                                w->hparent,
                                NULL,
                                w->hinst,
                                NULL);
	ASSERT(this->hwnd);

	this->id = w->id;
	this->is_attached = FALSE;

	return OK;
}
LRESULT CALLBACK CWnd::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    CWnd *wndp = NULL;

    wndp = (CWnd*)wnd_map.Find(hwnd);

    if(wndp == NULL)
    { 
		ASSERT(wnd_map.temp_ptr);
        wnd_map.Add(hwnd,wnd_map.temp_ptr);          
        wndp = (CWnd *) wnd_map.temp_ptr;			
		//this is before CreateWindow,So please keep it
		wndp->hwnd = hwnd; 
        wnd_map.SetTempPtr(NULL);
    }

    ASSERT(wndp);

	return wndp->ProcessAllMsg(message,wParam,lParam);
}
int CWnd::ProcessAllMsg(UINT message, WPARAM wparam, LPARAM lparam)
{
	switch(message)
	{
		case WM_CREATE: this->OnCreate(wparam,lparam); break;
		case WM_CLOSE: this->OnClose(wparam,lparam);return TRUE;
		case WM_PAINT: this->OnPaint(wparam,lparam);break;
		case WM_SIZE: this->OnSize(wparam,lparam);break;
		case WM_COMMAND:  this->OnCommand(wparam,lparam);break;
		case WM_NOTIFY:  this->OnNotify(wparam,lparam);break;
		case WM_LBUTTONDOWN:  this->OnLButtonDown(wparam,lparam);break;
		case WM_LBUTTONUP:  this->OnLButtonUp(wparam,lparam);break;
		case WM_RBUTTONDOWN:  this->OnRButtonDown(wparam,lparam);break;
		case WM_RBUTTONUP:  this->OnRButtonUp(wparam,lparam);break;
		case WM_HSCROLL:  this->OnScroll(wparam,lparam);break;
		case WM_VSCROLL:  this->OnScroll(wparam,lparam);break;
		case WM_MOUSEMOVE:  this->OnMouseMove(wparam,lparam);break;
		case WM_CHAR:  this->OnChar(wparam,lparam);break;
		case WM_KEYDOWN:  this->OnKeyDown(wparam,lparam);break;
		case WM_WINDOWPOSCHANGED: this->OnPosChanged(wparam,lparam);break;
		default: return ::DefWindowProc(this->hwnd, message, wparam, lparam);
	}

	return ::DefWindowProc(this->hwnd, message, wparam, lparam);
}
int CWnd::AttachWnd(HWND hwnd)
{
	this->is_attached = TRUE;
	this->hwnd = hwnd;

	return OK;
}
int CWnd::RegDefWnd()
{
    WNDCLASSEX wcex;    

    wcex.cbSize             = sizeof(WNDCLASSEX); 
    wcex.style              = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    wcex.lpfnWndProc        = (WNDPROC)CWnd::WndProc;
    wcex.cbClsExtra         = 0;
    wcex.cbWndExtra         = 0;
    wcex.hInstance          = GetModuleHandle(NULL);
    wcex.hIcon              = NULL;
    wcex.hCursor            = LoadCursor(NULL,IDC_ARROW);
    wcex.hbrBackground      = (HBRUSH)(COLOR_BTNFACE+1);
    wcex.lpszMenuName       = NULL;
    wcex.lpszClassName      = DEF_WND_CLASS;
    wcex.hIconSm            = NULL;
    
	return RegisterClassEx(&wcex);
}
int CWnd::SetText(char *str)
{
	if(this->hwnd)
		SendMessage(hwnd,WM_SETTEXT,0,(LPARAM)str);    
	else
		this->wnd_create->title = str;

	return OK;
}
int CWnd::GetText(char *buf, int max)
{
	ASSERT(hwnd);
	return SendMessage(hwnd,WM_GETTEXT,(WPARAM)max,(LPARAM)buf);
}
int CWnd::MoveWindow(int x, int y, int w, int h)
{
	if(this->hwnd)
	{
		::MoveWindow(this->hwnd,x,y,w,h,TRUE);
	}

	this->wnd_create->x = x;
	this->wnd_create->y = y;
	this->wnd_create->w = w;
	this->wnd_create->h = h;

	return OK;
}
int CWnd::Show()
{
	return ShowWindow(hwnd,SW_SHOW);
}
int CWnd::Hide()
{
	return ShowWindow(hwnd,SW_HIDE);
}
int CWnd::Close()
{
	return PostMessage(hwnd,WM_CLOSE,0,0);
}
int CWnd::OnCommand(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnNotify(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnLButtonDown(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnLButtonUp(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnRButtonDown(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnRButtonUp(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnScroll(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnMouseMove(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnChar(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnKeyDown(WPARAM wparam,LPARAM lparam)
{
	return OK;
}
int CWnd::OnCreate(WPARAM wparam, LPARAM lparam)
{
	return OK;
}
int CWnd::OnClose(WPARAM wparam, LPARAM lparam)
{
	this->Destroy();
	return OK;
}
int CWnd::OnPaint(WPARAM wparam, LPARAM lparam)
{
	PAINTSTRUCT ps;

	::BeginPaint(hwnd,&ps);
	::EndPaint(hwnd,&ps);

	return OK;
}
int CWnd::OnSize(WPARAM wparam, LPARAM lparam)
{
	return OK;
}
int CWnd::OnPosChanged(WPARAM wparam, LPARAM lparam)
{
	return OK;
}
int CWnd::GetTextLen()
{
	ASSERT(hwnd);
	return (int)GetWindowTextLength(hwnd);
}
int CWnd::GetUniqueID()
{
	static int i = 1000;
	return i++;
}

int CWnd::IsMyCommand(WPARAM wparam)
{
	if((HMENU)LOWORD(wparam) == this->id)
		return TRUE;

	return FALSE;
}

int CWnd::IsMyNotify(LPARAM lparam)
{
    LPNMHDR p;
    
	p=(LPNMHDR)lparam;   
	ASSERT(p);

	if(p->hwndFrom == this->hwnd)
		return TRUE;

    return FALSE;
}

int CWnd::GetNotifyCode(LPARAM lparam)
{
    LPNMHDR p;
    
	p=(LPNMHDR)lparam;
	ASSERT(p);

    return p->code;
}

int CWnd::SetTextF(char *szFormat, ...)
{
     char szBuffer [4096] ;
     
	 va_list pArgList ;
     va_start (pArgList, szFormat) ;
     vsprintf (szBuffer, szFormat, pArgList) ;
     va_end (pArgList) ;
     
	 this->SetText(szBuffer);

     return OK;
}

int CWnd::CreateCtrl(char *ctrl_name)
{
	ASSERT(this->hwnd == NULL);

	WndCreate *w = this->wnd_create;

	w->class_name = ctrl_name;

	this->hwnd = CreateWindowEx(w->ex_style,
                                w->class_name,
                                w->title,
                                w->style,
                                w->x,w->y,w->w,w->h,
                                w->hparent,
                                w->id,
                                w->hinst,
                                NULL);
	ASSERT(this->hwnd);

	this->id = w->id;

	return OK;
}

int CWnd::SetParent(HWND hp)
{
	ASSERT(this->wnd_create);
	
	wnd_create->hparent = hp;

	return OK;
}

int CWnd::GetCommandCode(WPARAM wparam)
{
	return HIWORD(wparam);
}

int CWnd::GetWidth()
{
	RECT r;
	::GetWindowRect(hwnd,&r);

	return r.right - r.left;
}

int CWnd::GetHeight()
{
	RECT r;
	::GetWindowRect(hwnd,&r);

	return r.bottom - r.top;
}

int CWnd::SetStockFont(int stock_font)
{
	HFONT hfont = (HFONT)::GetStockObject(stock_font);
	::SendMessage(hwnd,WM_SETFONT,(WPARAM)hfont,MAKELPARAM(1, 0));    

	return OK;
}

DWORD CWnd::GetStyle()
{
	return ::GetWindowLong(hwnd,GWL_STYLE);
}

int CWnd::IsVisible()
{
	return this->GetStyle() & WS_VISIBLE;
}


int CWnd::MoveWindow(RECT *pr)
{
	ASSERT(pr);
	this->MoveWindow(pr->left,pr->top,pr->right - pr->left,pr->bottom - pr->top);
	return OK;
}

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