Wednesday, November 12, 2008
Timers
The SetTimer function creates a timer with the specified time-out value.
This is a small example that will explain you how to use timer function in vc++
Step 1: Create a sample MFC project.
Step 2: create a member variable in the dialog class .h file
Example, UINT m_nTimerID;
Step 3: In the constructor initialize the timer variable
m_nTimerID = 0;
Step 4: In the OnInitDialog function call settimer function
SetTimer(m_nTimerID, 5000, NULL);
Step 5: Write a handler for WM_TIMER message and write your code..
Example , AfxMessageBox(L"Hai");
Step 6: Create a button that will kill the timer
KillTimer(m_nTimerID);
Now when the application is launched , once in 5 seconds the message window "Hai" will appear .
If you want to stop the timer then call killTimer to kill the timer.
Monday, August 25, 2008
Sample LoadResource
step 1:Add resource like in the pic 1 .
step2 :Type the data in the notepad & paste that in resource like pic 2.
To acess the resource db we can use this code
CString dbData; dbData.LoadString(IDR_DBDATA1);
HGLOBAL hDB = NULL;
HRSRC hDBRes = FindResource(AfxGetResourceHandle(),MAKEINTRESOURCE(130),"DBDATA");
if (hDBRes==NULL) return;
DWORD size = SizeofResource(AfxGetResourceHandle(),hDBRes);
LPTSTR pRes = NULL;
hDB = LoadResource(AfxGetResourceHandle(),hDBRes);
if (!hDB) return;
pRes = (LPTSTR) (hDB);
FreeResource(hDB);
FreeResource(hDBRes);
CString csTmp = (CString) pRes;
AfxMessageBox(csTmp);
Sample FindWindow
HWND hwnd = NULL,hTmpWnd = NULL;
CWnd *WindowHandle = NULL;
LPCTSTR lpStr;
char szWndName[100],szCapWndName[20],szCapAddName[20]; c
har szCaptionName[100];
char szClassName[100]; BOOL bTrue = FALSE;
HWND hMainWnd = NULL;
HWND hCapWnd = NULL;
m_hAddressWnd = NULL;
hwnd = ::FindWindow("#32770","anadona - New Contact");
if (hwnd) { hTmpWnd = ::GetWindow(hwnd, GW_CHILD);
if ( hTmpWnd ) { ::GetWindowText(hTmpWnd, szWndName, sizeof(szWndName));
int i =0,b=0;
while (i<20)>
{ if (lstrcmp(szWndName,"Captured Text") == 0)
{ hCapWnd = hTmpWnd ;b++; }
if (lstrcmp(szWndName,"Address") == 0)
{m_hAddressWnd = hTmpWnd; b++;}
hTmpWnd = ::GetWindow(hTmpWnd, GW_HWNDNEXT);
::GetWindowText(hTmpWnd, szWndName, sizeof(szWndName));
i++; if (b==2) break;
}
hTmpWnd = hCapWnd;
::GetWindowText(hTmpWnd, szWndName, sizeof(szWndName));
if (lstrcmp(szWndName,"Captured Text") == 0)
{ hTmpWnd = ::GetWindow(hTmpWnd, GW_CHILD);
if ( hTmpWnd ) { ::GetClassName(hTmpWnd, szWndName, sizeof(szWndName));
lpStr = csAddressBlock;
::SendMessage(hTmpWnd,WM_SETTEXT,0,(LPARAM)lpStr);
hTmpWnd = ::GetWindow(hTmpWnd, GW_HWNDNEXT);
::GetWindowText(hTmpWnd, szCaptionName, sizeof(szCaptionName));
::GetClassName(hTmpWnd, szClassName, sizeof(szClassName));
while (bTrue == FALSE)
{
if ((lstrcmp(szCaptionName,"&Contact") == 0 ) && (lstrcmp(szClassName,"Button") == 0) )
{ bTrue = TRUE; break; }
hTmpWnd = ::GetWindow(hTmpWnd, GW_HWNDNEXT);
::GetWindowText(hTmpWnd, szCaptionName, sizeof(szCaptionName));
::GetClassName(hTmpWnd, szClassName, sizeof(szClassName)); }
::SendMessage(hTmpWnd,WM_LBUTTONDOWN, 1, 0);
::SendMessage(hTmpWnd,WM_LBUTTONUP, 1, 0); }
} } }
CMenu sample
2. In the OnInitDialog
CMenu menu;
menu.LoadMenu(IDR_MENU1);
SetMenu(&menu);
CMap serialization
Serialization of CMap
Serialization is a mechanism by which the objects in the memory are stored persistently in the hard diskso that they may be loaded/restored in the future.
Step 1:
Create the object of CmapCMap
Step 2:
Populate the contents of the Cmap
m_cMap[“one”] = 1;
m_cMap[“two”] = 2;
Step 3:
Store it persistently.
If this Cmap object goes out of scope then its values are lost. To persistently store them we write itscontents into a file. Following code snippet shows how to store an object's contents.
CFile cFileG;
if ( cFileG.Open("SampleFile.DB",CFile::modeCreateCFile::modeWrite) )
{CArchive cArchiveObj(&cFileG,CArchive::store);
m_cMap.Serialize(cArchiveObj);}
Step 4:
Load the contents for reinitialization.
Using this file we can initialize any Cmap object of the same type. Following code snippet shows howto load a files contents into an object.
CMap
CFile cFileG;
if ( cFileG.Open("SampleFile.DB",CFile::modeRead) )
{CArchive cArchiveObj(&cFileG,CArchive::load);
m_cLoadMap.Serialize(cArchiveObj);}
File manipulation
CFile ofile;
CFileException Ex;
CFileStatus objstatus;
TCHAR *pcontent = NULL;
CString csStr("");
if ( ! ofile.Open("g:\\Doc.txt",CFile::modeRead ,&Ex))
{ return; }
else
{
ofile.GetStatus(objstatus);
long lsize= objstatus.m_size;
pcontent = new TCHAR [lsize+1];
ofile.Read(pcontent,lsize);
pcontent[lsize]='\0';
csStr = pcontent;
ofile.Close();
if(pcontent)
{ delete [] pcontent; pcontent = NULL; }
}
if ( ! ofile.Open("g:\\Doc.txt",CFile::modeCreate CFile::modeWrite,&Ex)) { return; }
else
{
ofile.Write(csStr,csStr.GetLength());
}
Sample HTTP download
DWORD dwServiceType = 0; CString sServerName, sObject;
CString csData; INTERNET_PORT nPort; CString csTemp; int nPos=-1;
CString sGetFromURL ( _T("") );
sGetFromURL =csQuery;
if ( !AfxParseURL ( sGetFromURL, dwServiceType, sServerName, sObject, nPort ) )
throw;
CHttpConnection* pHTTPServer = NULL; pHTTPServer = csiSession.GetHttpConnection ( sServerName, nPort );
CHttpFile* pFile = NULL;
pFile = pHTTPServer->OpenRequest ( CHttpConnection::HTTP_VERB_GET, sObject, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD );
pFile->SendRequest();
TCHAR sRaw [1024];
while ( pFile->ReadString ( sRaw, 1023 ) )
{ csData += sRaw; }
pFile->Close(); pHTTPServer->Close();
Sample DLL
In sampDll.cpp
EXP int sum(int a , int b)
{ return (a+b);}
In sampDll.h
#define EXP extern "C" _declspec(dllexport)
In SampleDllapi.h
typedef int (FAR *sumfn) (int , int );
EXP int sum(int a , int b);
------------------------------------
In Exe
void CSampExeDlg::OnOK() { HMODULE hmodule = NULL; hmodule = ::LoadLibrary("SampDll.dll");
DWORD h ;
sumfn smp;
if ( hmodule ) { smp =(sumfn) GetProcAddress( hmodule,"sum");
}
int a = smp(2,7);
if ( hmodule ) {
::FreeLibrary(hmodule); }
CDialog::OnOK();}
Friday, August 8, 2008
CTreeCtrl sample
CBitmap bmpTrreIcons;
bmpTrreIcons.LoadBitmap( IDB_BITMAP1 );
m_ImageList.Create(16, 16, ILC_MASKILC_COLOR16, 0, 1);
m_ImageList.Add( &bmpTrreIcons , RGB(255,255,255));
m_TreeCtrl.SetImageList( &m_ImageList,LVSIL_NORMAL);
HTREEITEM hTree, hSub;
hTree = m_TreeCtrl.InsertItem("About me" , TVI_ROOT );
//m_TreeCtrl.Expand(hTree,TVE_EXPAND);
m_TreeCtrl.SetItemState( hTree, TVIS_EXPANDED TVIS_BOLD ,TVIS_EXPANDED TVIS_BOLD );
hSub = m_TreeCtrl.InsertItem("Personal",0, 1,hTree);
m_TreeCtrl.InsertItem("School",2, 2,hSub);
m_TreeCtrl.InsertItem("College",2, 2,hSub);
m_TreeCtrl.InsertItem("Company",0, 1,hSub);
m_TreeCtrl.Expand(hSub,TVE_EXPAND);
hSub = m_TreeCtrl.InsertItem("Official",0, 1,hTree);
m_TreeCtrl.InsertItem("1st company",2, 2,hSub);
m_TreeCtrl.InsertItem("2nd Company",4, 4,hSub);
m_TreeCtrl.Expand(hSub,TVE_EXPAND);
On Double Click
void CSampleOnDblclkTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
CString csItemText;
CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
ASSERT(pCtrl != NULL);
HTREEITEM hCurSel = pCtrl->GetNextItem(TVI_ROOT, TVGN_CARET);
if (hCurSel == NULL)
MessageBox(_T("There is no selected item"),"SampleTool",MB_TOPMOSTMB_APPLMODALMB_OKMB_ICONINFORMATION);
else
{
csItemText = pCtrl->GetItemText(hCurSel);
}
*pResult = 0;
}
CSplitterwnd with different number of rows and cloumns
On Mainframe class , we have to override OncreateClient function
m_wndSplitter1.CreateStatic(this,2,1);
m_wndSplitter.CreateStatic( &m_wndSplitter1 ,1,2,WS_CHILD WS_VISIBLE WS_BORDER,m_wndSplitter1.IdFromRowCol(0, 0) );m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CMyTreeView),CSize(70,70), pContext);m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CMyBroser),CSize(70,70), pContext);m_wndSplitter1.CreateView( 1,0,RUNTIME_CLASS(CMyListView),CSize(70,70), pContext);
bSplitterInit = TRUE;
return TRUE;
}