Code Library

Free Source Code and Program Tips

Question: In some app like WinSpy/Spy++ to make adjustments to list controls. It can correctly set an app’s list control’s modes, alignments, sorting, styles, and extended styles. It can also get the widths of the columns and count of items. The problem is that you cannot seem to get the columns’ names or the item [...]

How to subclass CTreeCtrl in CTreeView?

Another title: How to use a derived CTreeCtrl in a CTreeView? It is similar with this article: “How to use an derived CListCtrl in CListView” or “How do I subclass the CListCtrl part of a CListView Class?“. Just like CListView, there is no CTreeCtrl to subclass in CTreeView. The CTreeView is the subclass of the [...]

How to send/post message to CDocument?

Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can’t receive message. How do that? There are 3 solutions: Solution 1: Send the message to the main window. Send/Post the message to the main frame or the view, and then call some functions of the document. But if [...]

How to sort items of CListCtrl or CListView?

How to sort the items of a CListCtrl or CListView(CReportView)? First, when you add items, you need to call SetItemData to attach an data structure(an integer or a pointer) to the item. Then, define a callback sort function. int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort); And here, the prarameters lParam1 and lParam2 are the [...]

Set the head backgroud color of CPropertyPage

How to set the head backgroud color of CPropertyPage? In the DrawItem function the TextOut position is pretty half-hearted, and looks a bit better (to my eyes) if you do it something like this instead. void CMyTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDIS) { CDC* pDC = CDC::FromHandle(lpDIS->hDC); CRect rc(lpDIS->rcItem); TCHAR szTabText[255]; TC_ITEM tci; tci.cchTextMax = sizeof(szTabText)-1; tci.pszText = szTabText; [...]

In MFC program, you’d better try to avoid using std::string. There is no compelling reason to use it in MFC, and it leads to confusion. There are some standard method to translate among Cstring ,string and char[]. // Suppose CString str; std::string s; char buf[SIZE]; // from CString to std::string s = str; // from [...]

How to hook the file related event

How to hook the file related event? It depends on what you need to do when such an event takes places. If what you just want to be notified, FindFirstChangeNotification and ReadDirectoryChanges are enough. If you need to intercept adn affect the operation somehow, I have heard it can be done with a kenerl-level driver(Filesystem [...]

How to read the sectors of physical drive under win32? The API ReadFile is most useful function, and with it, you can read the physical sectors directly. Here is a sample function, BOOL ReadSectors(…) BOOL ReadSectors( BYTE bDrive, //drive index DWORD dwStartSector, //start sector WORD wSectors, //sectors number LPBYTE lpSectBuff) {  if (bDrive == 0) [...]

How to get a fixed code for serial number(2)

I have post an article about “How to get a fixed code for serial number” at http://www.ucosoft.com/archives/51.html. In that article, I suggest you try WMI class to retrieve  some hardware ID. But there is some limitation of this method. The WMI redist package is not always available on user’s system, for example, Windows NT4 and Windows [...]

How to Draw Bitmap from a bmp File?

How to load a bitmap from bmp file and draw it in DC? There are two main steps: Load the bitmap from the file to a memory DC. Paint the content of the memory DC to the one which display the bitmap. In MFC, there is no API to load a bitmap directly from a [...]