Dynamic Link Libraries, Multithreading... are all somehow advanced concepts for beginners. But as the word "simple", they are conceptually simple. The aim of this article is to give you some useful technical initiative knowledge on these concepts, especially for the beginners. The article is not moving towards the theoretical definitions of the concept.
DLL part:
#include <afxwin.h> extern "C" _declspec(dllexport) void SayHello() { AfxMessageBox(“Hello World”); }
Thatzaal all with the DLL code… Build it and get your DLL.
Application part:
typedef void (*SayHello) (); // typedef a function pointer // load your dll into the memory HMODULE hmodule=::LoadLibrary("yourDLLName.dll"); if(hmodule) { //get your function SayHello sm=(SayHello) GetProcAddress(hmodule,"SayHello"); sm(); // thatzaal call your function now }
Tips:
GetProcessAddress(LoadLibrary(“yourDLLName.dll”),”SayHello”)();
GetProcessAddress()
struct threadinfo{ CString somedata; } info;
CWinThread
CWinThread *m_pthread
m_pthread=AfxBeginThread(functionname,(LPVOID) &info);
AfxBeginThread() API takes two parameters, the function name (will be your thread) and the necessary information as threadinfo structure.
AfxBeginThread()
threadinfo
UINT functionname(LPVOID param) { threadinfo *info=(threadinfo *)param; CString s=info->somedata; // handle your thread code here… }
::TerminateThread(m_pthread->m_hThread,0);
NOTIFYICONDATA
CMenu
Say for example:
NOTIFYICONDATA m_traydata; CMenu m_menu;
OnSysCommand()
void CYourDialog::OnSysCommand(UINT nID, LPARAM lParam) { if(nID==SC_MINIMIZE) { m_traydata.cbSize=sizeof(NOTIFYICONDATA); m_traydata.hWnd =this->m_hWnd; m_traydata.uID = 1; m_traydata.hIcon=AfxGetApp()->LoadIcon(IDI_YOURICON); m_traydata.uCallbackMessage =WM_MY_TRAY; strcpy(m_traydata.szTip,"Systray application"); m_traydata.uFlags=NIF_ICON|NIF_MESSAGE; m_menu.DestroyMenu(); // if already exists if(!m_menu.LoadMenu(IDR_MENU1)) return; Shell_NotifyIcon(NIM_DELETE,&m_traydata); // if already exisits If(!Shell_NotifyIcon(NIM_ADD,&m_traydata)) return; this->ShowWindow(SW_HIDE); } }
#define WM_MY_TRAY (WM_USER+1)
OnTrayNotify()
void OnTrayNotify(WPARAM wParam, LPARAM lParam);
ON_MESSAGE(WM_MY_TRAY,OnTrayNotify)
MESSAGE_MAP
void CYourDialog::OnTrayNotify(WPARAM wParam, LPARAM lParam) { UINT uID,uMsg; uID=(UINT)wParam; uMsg=(UINT)lParam; if(uID!=1) return; CPoint pt; switch(uMsg) { case WM_LBUTTONDOWN: this->ShowWindow(SW_SHOW); this->ShowWindow(SW_NORMAL); Shell_NotifyIcon(NIM_DELETE,&m_traydata); break; case WM_RBUTTONDOWN: case WM_CONTEXTMENU: GetCursorPos(&pt); OnTrayRButton(pt); break; } return; }
OnTrayRButton(CPoint point)
void CYourDialog::OnTrayRButton(CPoint point) { // assuming that the IDR_MENU1 is having atleast one submenu m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_BOTTOMALIGN| TPM_LEFTBUTTON| TPM_RIGHTBUTTON,point.x,point.y,this); }
OnDestroy
void CYourDialog::OnDestroy() { CDialog::OnDestroy(); Shell_NotifyIcon(NIM_DELETE,&m_traydata); }
Method 1:
m_menu
m_menu.LoadMenu(IDR_MENU1);
WM_CONTEXTMENU
void CYourDlg:: OnContextMenu(CWnd* pWnd, CPoint point) { m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTBUTTON, point.x,point.y,this); }
Method 2:
void CYourDlg:: OnContextMenu(CWnd* pWnd, CPoint point) { m_menu.DestroyMenu(); // Destroy the old menu if any. m_menu.CreatePopupMenu(); m_menu.AppendMenu(MF_STRING,1000,"item 1"); m_menu.AppendMenu(MF_SEPARATOR); m_menu.AppendMenu(MF_STRING,1001,"item 2"); m_menu.AppendMenu(MF_STRING,1002,"item 3"); m_menu.AppendMenu(MF_POPUP,(UINT) m_menu2.m_hMenu,”sub menu”); m_menu.TrackPopupMenu(TPM_RIGHTBUTTON,point.x,point.y,this); }
wParam
WM_COMMAND
MF_POPUP
CPtrList
CPtrList::AddTail()
CPtrList:::AddHead()
CPtrList::InsertAfter()
CPtrList::InsertBefore()
CPtrList::SetAt()
void*
CPtrList::GetHead()
CPtrList::GetTail()
CPtrList::GetAt()
CPtrList:::GetNext()
CPtrList::GetPrev()
void
POSITION
CPtrList::FindIndex(index)
POSTION
CPtrList::Find(void *)
CPtrList::GetNext(POSITION)
CPtrList::RemoveAt()
CPtrList::RemoveAll()
Example:
// Adding an object to the list // Should be deleted. Caution: Memory Leak m_objList.AddTail((void *) new CPoint(2,2)); // Traversing a CPtrList; and assigning value to the objects POSITION pos=m_objList.GetHeadPosition(); while(pos) { CPoint *obj=(CPoint *)m_objList.GetNext(pos); obj->x=10;obj->y=10; } // Removing the objects from the list POSITION pos=m_objList.GetHeadPosition(); while(pos) { CPoint *obj=(CPoint *)m_objList.GetNext(pos); delete obj; } m_objList.RemoveAll();
CListCtrl::SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
CListCtrl::InsertColumn(index,title,alignment,<title length>*25,-1)
Alignment - LVCFMT_LEFT, LVCFMT_RIGHT, LVCFMT_CENTER
LVCFMT_LEFT
LVCFMT_RIGHT
LVCFMT_CENTER
CListCtrl::SetColumnWidth(index,size);
CListCtrl::InsertItem(index,””); CListCtrl::SetItemText(index,column,item_text);
CString txt=CListCtrl::GetItemText(item,column);
POSITION pos=CListCtrl:: GetFirstSelectedItemPosition(); while(pos) { int item=CListCtrl::GetNextSelectedItem(pos); CString txt=CListCtrl::GetItemText(item,column); }
CListCtrl::SetItemState(index,LVIS_FOCUSED,LVIS_FOCUSED);
CListCtrl::DeleteAllItems();
CDatabase db; db.OpenEx("DRIVER=Microsoft Access Driver (*.mdb);DBQ=mydatabase.mdb;")
CRecordset rs; rs.m_pDatabase=&db;
rs.GetFieldValue(index,CString&);
rs.GetRecordCount();
if(rs.GetRecordCount()==0) return; rs.MoveFirst(); for(int i=0; i<rs.GetRecordCount(); i++) { CString emp_no,emp_name,; rs.GetFieldValue(0,emp_no); rs.GetFieldValue(1,emp_name); MessageBox(emp_no+” “+emp_name); rs.MoveNext(); }
(to be continued...)
I am not telling that these are the full concepts indicated by the titles. But these are the basics. The article may not cover the full area over the concepts. None of the articles can satisfy one's expectations. But, each article should be a seed for your technical growth. Thus, I believe that this would be a seed. Thank you all.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
General News Suggestion Question Bug Answer Joke Rant Admin
Skills that self-taught computer programmers lack