|
Do you hate google?
You could have searched with "Singleton C++" and would get lots of resources where you could have read from
A class whose number of instances is only one is called a singleton class. only one instance of a class can exist at any given time.
A raw class definition would be similar to what is mentioned below
class CXSingleton
{
public:
static CXSingleton& Instance()
{
static CXSingleton ston;
return ston;
}
};
And yes there are n number of ways of implementing this
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
|
|
|
|
|
Singleton!
Intent
Ensure a class only has one instance, and provide a global point of access to it.
Motivation
It's important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.
How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects.
A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.
class CSingleton
{
public:
static CSingleton * getInstance() {
if (!_singleton) _singleton = new CSingleton;
return _singleton;
}
protected:
CSingleton() { }
~CSingleton() { }
private:
static CSingleton * _singleton;
};
CSingleton* CSingleton ::_singleton = NULL;
Try to look at singleton destroyers too especially when you singleton holds a database connection or similar resources.
You can also create a SINGLETON TEMPLATE like the one below. In this way your singleton holds a pointer and it ensures that only one instance exists of the object the pointer is pointing at.
template < typename T >
class CSingleton
{
public:
static CSingleton * getInstance() {
if (!_singleton) _singleton = new CSingleton;
return _singleton;
}
const T* operator->() { return m_ptr; };
protected:
CSingleton() { m_ptr = new T; }
~CSingleton() { if (m_ptr) delete m_ptr; }
private:
static CSingleton * _singleton;
T * m_ptr;
};
Hth
--
Jess Nielsen, b.sc.
Security Analyst
http://jessn.blogspot.com/
|
|
|
|
|
Hi All
i have two dialog A and B.I set hot key on Main Dialog A.It's working fine.But i am getting one error when Dialog A and B both open then i use short cut key then main Dialog A is close and B is still open.What i want to do if i use short cut key when both dialog open.Case one short key will not work on that condition or case two both dialog effect.Code is here
[code]
m_UniqueIdentifier = ::GlobalAddAtom( "SINGLE_INSTANCE_APP");
::RegisterHotKey( m_hWnd, /*Your window handle*/
m_UniqueIdentifier, /*Unique identifier to uniquely identify this hotkey*/
MOD_ALT|MOD_CONTROL, /*Modifier keys*/
VK_F2); /*Virtual
LRESULT CADlg::HotKeyHandler(WPARAM wParam, LPARAM lParam)
{
if( IsWindowVisible() )
{
ShowWindow( SW_HIDE );
}
else
{
ShowWindow( SW_NORMAL );
}
return 0;
}// End HotKeyHandler
plz help me
|
|
|
|
|
|
Just keep a pointer in your A dialog to your B dialog and call its ShowWindow apropriately.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
|
|
|
|
|
I have a simple dialog box. On its OnInitDialog() function I prompt for a existing image file name(jpg, bmp, png). I need to convert this image to bmp and draw the bitmap image in the client area.
Thanks!!!!
|
|
|
|
|
Here's one of the simplest ways I can think of:
LPCTSTR imgpathname = ...;
CImage img;
img.Load(imgpathname);
CClientDC dc(this);
img.Draw(dc, 0, 0);
The drawing code should really be done in a WM_PAINT handler (OnPaint) or
a WM_ERASEBKGND handler (OnEraseBkgnd).
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for your reply Mark. I need to save the bitmap image to a xml file along some other data (bitmap image data will be in tag.) . how do i get bitmap data that is writable to xml. I need bitmap data because the xml should be readable on a mac. How do i do this?
Thanks!!
|
|
|
|
|
Here is my class implementation:
CImageDlg::CImageDlg(CWnd* pParent )
: CDialog(CImageDlg::IDD, pParent)
{
m_StrTitle = _T("Image for ");
m_hBmp = NULL;
}
CImageDlg::~CImageDlg()
{
}
void CImageDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CImageDlg, CDialog)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CImageDlg::OnPaint()
{
CPaintDC dc(this);
RECT rect = {0};
GetClientRect(&rect);
if (m_hBmp != NULL)
{
....
BitBlt(dc.GetSafeHdc(), 0, 0, 100, 100, memDC, 0,0, SRCCOPY);
}
}
void CImageDlg::SetTitle(CString str)
{
m_StrTitle += str;
}
void CImageDlg::SetBitmap(HBITMAP hBmp)
{
m_hBmp = hBmp;
}
BOOL CImageDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetWindowText(m_StrTitle);
if (m_hBmp == NULL)
{
TCHAR szFile[260] = {0};
OPENFILENAME openfilename = {0};
wchar_t wcsMyPicturesPath[1024] = {0};
SHGetFolderPath( NULL, CSIDL_MYPICTURES, NULL, 0, wcsMyPicturesPath );
ZeroMemory(&openfilename, sizeof(openfilename));
openfilename.lStructSize = sizeof(OPENFILENAME);
openfilename.hwndOwner = this->m_hWnd;
openfilename.lpstrFile = szFile;
openfilename.lpstrFile[0] = _T('\0');
openfilename.nMaxFile = sizeof(szFile);
TCHAR szFltr[200] = {0};
lstrcat(szFltr, _T("*.JPG;*.JPEG;*.BMP;*.PNG;\0"));
openfilename.lpstrFilter = szFltr;
openfilename.nFilterIndex = 0;
openfilename.lpstrFileTitle = NULL;
openfilename.nMaxFileTitle = 0;
openfilename.lpstrInitialDir = wcsMyPicturesPath;
openfilename.lpstrTitle = NULL;
openfilename.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
if(GetOpenFileName(&openfilename) == TRUE)
{
using namespace Gdiplus;
Gdiplus::Image *img = Image::FromFile(openfilename.lpstrFile);
Gdiplus::Image *thumbImg = img->GetThumbnailImage(100, 100);
m_hBmp = (HBITMAP)thumbImg;
}
else
{
return -1;
}
}
return TRUE;
}
How should i write the drawing code now?
|
|
|
|
|
void CImageDlg::OnPaint()
{
CPaintDC dc(this);
RECT rect = {0};
GetClientRect(&rect);
if (m_hBmp != NULL)
{
CDC memDC;
MemDC.CreateCompatibleDC(&dc);
HBITMAP OrigBmp = ::SelectObject(memDC, m_hBmp);
BITMAP BInfo;
::GetObject(m_hBmp, sizeof(BInfo), &BInfo);
::BitBlt(dc.GetSafeHdc(), 0, 0, BInfo.bmWidth, BInfo.bmHeight, memDC, 0,0, SRCCOPY);
::SelectObject(memDC, OrigBmp);
}
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
|
|
|
|
|
Thanks for replying.
After I do stretchblt() the image quality is lost. how do I avoid this.
Thanks in advance.
|
|
|
|
|
CDC::SetStretchBltMode()[^], try settting it to ... COLORONCOLOR i believe.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
|
|
|
|
|
Dude it did not work. I think I have to use GDI+. That renders images beautifully. What say? But the problem there is that Im unable to convert a HBITMAP to Gdiplus::Image. Any Ideas?
|
|
|
|
|
Did you use That StretchBlitMode on memDC or dc? Anyhows, there's something like Gdiplus::Image::fromHBITMAP or something similar...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
|
|
|
|
|
I used it on memDC. Gdiplus::Image::FromHBITMAP does not exist. Image has FromFile and FromStream. FromHBITMAP is present in Gdiplus::Bitmap.
|
|
|
|
|
It works quite well enough if used for dc. Thanks.
|
|
|
|
|
i want to divide status bar control panes into 3 equal parts,how can i do this?
thanks in advance
|
|
|
|
|
Anjali Patil wrote: i want to divide status bar control panes into 3 equal parts,how can i do this?
CStatusBar documentation has got some secret API's hidden in it. If just you could have read the documentation
Look here[^] for a sample
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
|
|
|
|
|
Hi all
What does this error mean:
1>nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in UpdatePanel.obj
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj)
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
This happens here:
#include <afxdb.h>
#include <winsock2.h>
#include <tchar.h>
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>
#endif // _AFX_NO_AFXCMN_SUPPORT
#include "..\\Common\\smtpconnection2.h"
BOOL CSMTPConnection2::Connect(LPCTSTR lpszHostDomain,
DWORD dwTimeout ) throw()
{
CSimpleArray<CAtlString> arrSMTP;
_GetSMTPList(lpszHostDomain, arrSMTP);
for(int i=0; i<arrSMTP.GetSize(); i++)
{
if(CSMTPConnection::Connect(arrSMTP[i], dwTimeout) == TRUE)
return TRUE;
}
return FALSE;
}
void CSMTPConnection2::_GetSMTPList(LPCTSTR lpszHostDomain,
CSimpleArray<CAtlString>& arrSMTP)
{
PDNS_RECORD pRec = NULL;
if(DnsQuery(lpszHostDomain, DNS_TYPE_MX, DNS_QUERY_STANDARD,
NULL, &pRec, NULL) == ERROR_SUCCESS)
{
PDNS_RECORD pRecOrig = pRec;
while(pRec)
{
if(pRec->wType == DNS_TYPE_MX)
arrSMTP.Add(pRec->Data.MX.pNameExchange);
pRec = pRec->pNext;
}
DnsRecordListFree(pRecOrig,DnsFreeRecordList);
}
}
What do you think?
Thanks,
Jayjay
|
|
|
|
|
See here[^]
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
|
|
|
|
|
Hi there
I am trying to establish an Internet session (HTTP GET) with a MFC
application on a Windows Mobile 6 platform. First I tried with
OpenRequest() and SendRequest(), but when the code reached SendRequest
() the application crashed in the evil way. Secondily I tried to use
OpenURL() but it keeps saying that it cannot find the server. I have
even connected the device to my WLAN to ensure I had a stable Internet
connection.
CString GoogleMap::getMapUrl(int width, int height, double lng, double
lat, int zoom, CString format)
{
char * szUrl = (char*)malloc(256);
char * szFormat = "http://maps.google.com/staticmap?center=%f,%f&format=%s&zoom=%d&size=%dx%d&key=%s";
sprintf(szUrl, szFormat, lat, lng, format, zoom, width, height,m_szAPIKey);
CString s = CString(szUrl);
return s;
}
BYTE* GoogleMap::loadHttpFile(CString url, long &length)
{
BYTE* buffer = NULL;
CInternetSession session;
CHttpConnection * hc = NULL;
hc = session.GetHttpConnection(_T("maps.google.com"), 0, 80, NULL,NULL);
if (!hc) return NULL;
CHttpFile* pFile = (CHttpFile*)session.OpenURL(url);
length = (long)pFile->GetLength();
if (!length) return NULL;
buffer = (BYTE*)malloc(length + 1);
memset(buffer, 0, length +1);
for ( DWORD dwRead1;dwRead1=pFile->Read(buffer,length); );
return buffer;
}
Could someone please tell me what I do wrong?
Thanks in advance,
Jess
--
Jess Nielsen, b.sc.
Security Analyst
http://jessn.blogspot.com/
|
|
|
|
|
Hi, I asked a question sort of similar to this a few days ago, but the problem has now evolved; what I want to do is:
I am using C++ with the windows API, without MFC.
I want to retain all the functionality of a listbox but I want to draw everything myself. If I set the style to OWNERDRAW I can at least draw the listbox items and the box itself (if I don't give it border) but I still can't draw any scrollbars created using WS_VSCROLL or WS_HSCROLL; they seem to use the default windows drawing behavior.
So how do I completely override all the default drawing for a listbox (or any windows control for that matter)? I don't want to draw over the default scrollbars; I want them to simply not draw at all, but still "exist" and allow me to ownerdraw them.
Thanks!
KR
|
|
|
|
|
Did you see this[^]article?
Greg Ellis wrote:
Next, I had to find a way to customize the existing scrollbars or else make my own. So, I tried to subclass the CScrollbar class; whenever I tried to use the GetScollbarCtrl() function from the CListCtrl, it returned null. Obviously, the scrollbars are not real. Unfortunately, this means I had to hide the existing scrollbars and create my own (a lot more work than just skinning the existing ones).
I began to try to hide the scrollbars of the CListCtrl and then somehow create my own. I found a solution for hiding the scrollbars in a CListCtrl on the CodeGuru message boards from Filbert Fox. This worked great, so my next task was to create my own scrollbars.
I chose to derive a class from CStatic and create the scrollbar from scratch using bitmaps. It took a while and a lot of tweaking, but I got the custom scrollbar created and working including the wheel mouse, arrow keys, and pageup/pagedown keys.
|
|
|
|
|
Ugh, I can't believe there's no way to custom draw the internal scrollbars of a control... it may not have an HWND associated with it, but you can click it and use it just like any other scrollbar so it must send some sort of scrollbar messages, and it's drawn somewhere, somehow, so there has to be a way to custom draw it...
Thanks for the link though, more and more it keeps looking like I will have to put in my own custom scrollbars but I wish I could just take advantage of the internal ones.
KR
|
|
|
|
|
I have the following already implemented...
A base class (Say A) with a macro definition implementing two functions. Say A1 and A2.
A class ( Say B) derived from Class A.
Member functions of B called A1 and A2.
Now I have a need for the following...
Without changing any code in the derived class B or its member functions I want to know inside A1 or A2 functions the function/line number/file from which A1 or A2 gets called.
Is this possible??
|
|
|
|
|