C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2005, 09:08 AM   #1
Registered User
 
Join Date: Aug 2005
Posts: 79
Using private class members in static functions

Hi I having a problem with using my private class members in a private static funtion.

Code:
#ifndef PRINTING_H
#define PRINTING_H

class CPrinting
{
	public:
	    
	    CPrinting();
	    ~CPrinting();
	    
	    bool DoPageSetup(HWND hParenWindow);	    
	    bool DoPrint(HINSTANCE hInstance, HWND hParentWindow, LPSTR pszTitleName, int iEditControlID, int iPrintDialogID, int iFileNameID);	    
	    
	private:
	    
	    static BOOL CALLBACK PaintHook(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam);	   
	    static BOOL CALLBACK AbortProcedure(HDC hDC, int iCode);
	    static BOOL CALLBACK PrintDialogProcedure(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam);
	    
	    PAGESETUPDLG m_psd;
	    PRINTDLG m_pd;
	    DOCINFO m_di;
	    TEXTMETRIC m_tm;
	    LPSTR m_pszBuffer;
	    
	    char m_szJobName[MAX_PATH + 64];
	    
	    HWND m_hPrintDialog;
	    
	    bool m_bUserAbort;	    
	    
	    int m_iHorzRes;
	    int m_iVertRes;	   
	    int m_iChar;
	    int m_iCharsPerLine;	    	    
	    int m_iLinesPerPage;
	    int m_iTotalLines;
	    int m_iTotalPages;
	    int m_iLineNumber;    
};

#endif // PRINTING_H
Code:
#include <windows.h>
#include "printing.h"

CPrinting::CPrinting()
{
	
}

CPrinting::~CPrinting()
{
	
}

bool CPrinting::DoPageSetup(HWND hParentWindow)
{
	ZeroMemory(&m_psd, sizeof(m_psd));
	
	m_psd.lStructSize = sizeof(m_psd);
	m_psd.hwndOwner = hParentWindow;
	m_psd.hDevMode = NULL;
	m_psd.hDevNames = NULL;
	m_psd.Flags = PSD_INTHOUSANDTHSOFINCHES | PSD_MARGINS | PSD_ENABLEPAGEPAINTHOOK;
	m_psd.rtMargin.top = 1000;
	m_psd.rtMargin.left = 1250;
	m_psd.rtMargin.right = 1250;
	m_psd.rtMargin.bottom = 1000;
	m_psd.lpfnPagePaintHook = (LPPAGEPAINTHOOK)PaintHook;
	
	if(!PageSetupDlg(&m_psd))
	{
		return false;
	}	
	return true;
}

BOOL CALLBACK CPrinting::PaintHook(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam)
{
	switch(messages)
	{
		case WM_PSD_MARGINRECT:
		     {
		     	LPRECT lprc;
		     	COLORREF crMarginRect;
		     	HDC hDC, hDCOld;
		     	
		     	hDC = (HDC)wParam;
		     	lprc = (LPRECT)lParam;
		     	
		     	crMarginRect = GetSysColor(COLOR_HIGHLIGHT);
		     	
		     	hDCOld = static_cast<HDC>(SelectObject(hDC, CreatePen(PS_DASHDOT, static_cast<int>(.5), crMarginRect)));
		     	
		     	Rectangle(hDC, lprc->left, lprc->top, lprc->right, lprc->bottom);
		     	
		     	SelectObject(hDC, hDCOld);
		     	
		     	return true;
		     }
		     break;
		     
		default:
		     return false;    
	}
	return true;
}

bool CPrinting::DoPrint(HINSTANCE hInstance, HWND hParentWindow, LPSTR pszTitleName, int iEditControlID, int iPrintDialogID, int iFileNameID)
{
	ZeroMemory(&m_pd, sizeof(m_pd));
	
	m_pd.lStructSize = sizeof(m_pd);
	m_pd.hwndOwner = hParentWindow;
	m_pd.hDevMode = NULL;
	m_pd.hDevNames = NULL;
	m_pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
	m_pd.nCopies = 1;
	m_pd.nFromPage = 1;
	m_pd.nToPage = 0xFFFFFFFF;
	m_pd.nMinPage = 1;
	m_pd.nMaxPage = 0xFFFFFFFF;
	
	if(!PrintDlg(&m_pd))
	{
		return false;
	}
	
	GetWindowText(hParentWindow, m_szJobName, sizeof(m_szJobName));
	
	ZeroMemory(&m_di, sizeof(m_di));
	
	m_di.cbSize = sizeof(m_di);
	m_di.lpszDocName = m_szJobName;
	
	GetTextMetrics(m_pd.hDC, &m_tm);	
	
	m_iHorzRes = GetDeviceCaps(m_pd.hDC, HORZRES);
	m_iVertRes = GetDeviceCaps(m_pd.hDC, VERTRES);	
	m_iChar = m_tm.tmHeight + m_tm.tmExternalLeading;
	m_iCharsPerLine = m_iHorzRes / m_tm.tmAveCharWidth;
	m_iLinesPerPage = m_iVertRes / m_iChar;	
	m_iTotalLines = SendDlgItemMessage(hParentWindow, iEditControlID, EM_GETLINECOUNT, 0, 0);
	m_iTotalPages = (m_iLinesPerPage + m_iTotalLines - 1) / m_iLinesPerPage;
	
	m_pszBuffer = new char[sizeof(char) * m_iCharsPerLine + 1];
	
	EnableWindow(hParentWindow, false);
	
	m_hPrintDialog = CreateDialog(hInstance,
	                              MAKEINTRESOURCE(iPrintDialogID),
	                              hParentWindow,
	                              PrintDialogProcedure);
	
	SetDlgItemText(m_hPrintDialog, iFileNameID, pszTitleName);
	
	SetAbortProc(m_pd.hDC, AbortProcedure);
	
	if(StartDoc(m_pd.hDC, &m_di) > 0)
	{
		for(int iColCopy = 0; 
		    iColCopy < ((m_pd.Flags & PD_COLLATE) ? m_pd.nCopies : 1); 
		    iColCopy++)
		{
			for(int iPage = 0; iPage < m_iTotalPages; iPage++)
			{
				for(int iNoColCopy = 0; 
				    iNoColCopy < ((m_pd.Flags & PD_COLLATE) ? 1 : m_pd.nCopies); 
				    iNoColCopy++)
				{
					if(StartPage(m_pd.hDC) < 0)
					{
						return false;
						break;
					}			
					
					for(int iLine = 0; iLine < m_iLinesPerPage; iLine++)
					{
						m_iLineNumber = m_iLinesPerPage * iPage + iLine;
						
						*(int *)m_pszBuffer = m_iCharsPerLine;
						
						TextOut(m_pd.hDC, 0, m_iChar * iLine, m_pszBuffer, 
						        static_cast<int>(SendDlgItemMessage(hParentWindow, iEditControlID, EM_GETLINE,
						                                            static_cast<WPARAM>(m_iLineNumber), (LPARAM)m_pszBuffer)));
					}
					
					if(EndPage(m_pd.hDC) < 0)
					{
						return false;
						break;
					}
					
					if(m_bUserAbort)
					{
						break;
					}
				}
				
				if(false || !m_bUserAbort)
				{
					break;
				}
			}
			
			if(false || !m_bUserAbort)
            {
				break;
			}			
		}
	}
	
	if(!m_bUserAbort)
	{
		EnableWindow(hParentWindow, true);		
	}
	
	delete[] m_pszBuffer;
	
	DeleteDC(m_pd.hDC);
	
	return true && !m_bUserAbort;
}

BOOL CALLBACK CPrinting::AbortProcedure(HDC hDC, int iCode)
{
	MSG messages;
	
	while(!m_bUserAbort && PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
	{
		if(!m_hPrintDialog || IsDialogMessage(m_hPrintDialog, &messages))
		{
			TranslateMessage(&messages);
			DispatchMessage(&messages);
		}
	}
	return !m_bUserAbort;
}

BOOL CALLBACK CPrinting::PrintDialogProcedure(HWND hwnd, UINT messages, WPARAM wParam, LPARAM lParam)
{
	switch(messages)
	{
		case WM_INITDIALOG:
		     EnableMenuItem(GetSystemMenu(hwnd, false), SC_CLOSE, MF_GRAYED);
		     return true;
		
		case WM_COMMAND:
		     {
		     	m_bUserAbort = true;
		     	
		     	EnableWindow(GetParent(hwnd), true);
		     	
		     	DestroyWindow(hwnd);
		     	
		     	m_hPrintDialog = 0;
		     }
             return true;     
	}
	return false;
}
Compile log:

Compiling: printing.cpp
printing.cpp: In static member function `static BOOL CPrinting::AbortProcedure(HDC__*, int)':
printing.h:61: error: invalid use of member `CPrinting::m_bUserAbort' in static member function
printing.cpp:218: error: from this location
printing.h:59: error: invalid use of member `CPrinting::m_hPrintDialog' in static member function
printing.cpp:220: error: from this location
printing.h:59: error: invalid use of member `CPrinting::m_hPrintDialog' in static member function
printing.cpp:220: error: from this location
printing.h:61: error: invalid use of member `CPrinting::m_bUserAbort' in static member function
printing.cpp:226: error: from this location
printing.cpp: In static member function `static BOOL CPrinting::PrintDialogProcedure(HWND__*, UINT, WPARAM, LPARAM)':
printing.h:61: error: invalid use of member `CPrinting::m_bUserAbort' in static member function
printing.cpp:239: error: from this location
printing.h:59: error: invalid use of member `CPrinting::m_hPrintDialog' in static member function
printing.cpp:245: error: from this location
Process terminated with status 1 (0 minutes, 2 seconds)

So how do I go about using those variables in those static functions? I read the FAQ on the static keyword, but I really didn't understand how it worked.
sethjackson is offline   Reply With Quote
Old 09-23-2005, 09:27 AM   #2
Registered User
 
Join Date: Jan 2005
Posts: 7,137
You cannot access non-static member variables or methods from within static methods. Since the static method is the same for all instances, how would it know which instance's m_bUserAbort, m_hPrintDialog, and other member variables to use?

If AbortProcedure must be static and it must use the members of an instance of the class, then you have to find a way to pass the class pointer to the function so that it knows which instance to work on.
Daved is offline   Reply With Quote
Old 09-23-2005, 09:54 AM   #3
Registered User
 
Join Date: Jun 2005
Posts: 1,343
In other words, a static member function can access private members of a particular instance of the class. For example, your Abort Procedure might do something like this;

Code:
CPrinting some_object;

BOOL CALLBACK CPrinting::AbortProcedure(HDC hDC, int iCode)
{
	MSG messages;
	
	while(!m_bUserAbort && PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
	{
		if(!m_hPrintDialog || IsDialogMessage(some_object.m_hPrintDialog, &messages))
		{
			TranslateMessage(&messages);
			DispatchMessage(&messages);
		}
	}
	return !some_object.m_bUserAbort;
}
Trying to access non-static members without going via a particular object is not allowed. And that's what your original code does .....
grumpy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to initialize a private static const array of a private child class in a class? ryanli C++ Programming 3 04-10-2009 02:20 AM
Code review Elysia C++ Programming 71 05-13-2008 09:42 PM
Static member functions more efficient? drrngrvy C++ Programming 6 06-16-2006 07:07 AM
Problems with static const class members sigfriedmcwild C++ Programming 5 12-05-2004 07:57 AM
bug for static functions in template class Unregistered C++ Programming 3 09-16-2001 06:38 PM


All times are GMT -6. The time now is 10:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22