Snippets
From SpenchWiki
- Use !! to ensure that any overloaded operators used to evaluate an expression do NOT end up at operator ||
- Invalid Window filename chars
/ \ : ? * " < > |
- Change default file mode
#include "stdlib.h" #include <fcntl.h> /*extern "C" int */_fmode = _O_WTEXT; _fmode = _O_BINARY;
- fopen 'fmode': ANSI, UTF-8, UTF-16LE, and UNICODE
",ccs=UNICODE" #include <fcntl.h> _O_TEXT, _O_BINARY, _O_WTEXT, _O_U16TEXT, _O_U8TEXT
- Unicode in text files (auto-read in fopen.c)
UTF-16 BOM: FF FE UTF-8 BOM: ? ? ? "\r\n" (0x0D, 0x0A) (CRLF)
- MFC filecore.cpp helpers
AfxGetModuleShortFileName AfxGetFileTitle AfxComparePath AfxGetRoot AfxFullPath AfxResolveShortcut AfxGetInProcServer AfxStringFromCLSID AFX_COM::CreateInstance AFX_COM::GetClassObject
- Keydown detection
(GetAsyncKeyState(VK_SHIFT) & (1 << 15))
- CTreeCtrl hit-test
void OnNMClickTreeDirectories(NMHDR *pNMHDR, LRESULT *pResult) { CPoint point; GetCursorPos(&point); UINT nFlags; m_cntrlTree_Dirs.ScreenToClient(&point); HTREEITEM hItem = m_cntrlTree_Dirs.HitTest(point, &nFlags); if (nFlags & TVHT_ONITEMSTATEICON) { m_cntrlTree_Dirs.GetItemData(hItem); } }
- TrackPopupMenu
CMenu menu; menu.LoadMenu(MAKEINTRESOURCE(IDR_MENU)); CMenu* pMenu = menu.GetSubMenu(2); ASSERT(pMenu); CPoint point; ::GetCursorPos(&point); pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, point.x, point.y, this);
- WM_COMMAND
wParam
The high-order word specifies the notification code if the message is from a control. If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero. The low-order word specifies the identifier of the menu item, control, or accelerator.
lParam
Handle to the control sending the message if the message is from a control. Otherwise, this parameter is NULL.
- TN020: ID Naming and Numbering Conventions
- By convention, the ID value of 0 is not used.
- Windows implementation limitations restrict true resource IDs to be less than or equal to 0x7FFF.
- MFC's internal framework implementations reserve several ranges: 0xE000->0xEFFF and 0x7000->0x7FFF.
- Several Windows system commands use the range of 0xF000 -> 0xFFFF.
- Control IDs of 1->7 are reserved by IDOK, IDCANCEL, and so on.
- The range of 0x8000->0xFFFF for strings is reserved for menu prompts for commands.
SHAutoComplete(hwndEdit, dwFlags);
- Menu utilities
GetMenuPosFromID
- Shell Colour utilities
ColorAdjustLuma ColorHLSToRGB ColorRGBToHLS
- _CrtSetDebugFillThreshold
- File Management Functions
GetBinaryType SHGetFileInfo
- Thread naming
typedef struct tagTHREADNAME_INFO { DWORD dwType; // must be 0x1000 LPCSTR szName; // pointer to name (in user address space) DWORD dwThreadID; // thread ID (-1 = caller thread) DWORD dwFlags; // reserved for future use, must be zero } THREADNAME_INFO; void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName ) { THREADNAME_INFO info; info.dwType = 0x1000; info.szName = szThreadName; info.dwThreadID = dwThreadID; info.dwFlags = 0; __try { RaiseException( 0x406D1388, 0, sizeof(info) / sizeof(DWORD), (DWORD*)&info ); } __except( EXCEPTION_CONTINUE_EXECUTION ) { } } // Example usage: SetThreadName(-1, "Main thread");
- Creating a crash dump
#include <dbghelp.h> #include <shellapi.h> #include <shlobj.h> int GenerateDump(EXCEPTION_POINTERS* pExceptionPointers) { BOOL bMiniDumpSuccessful; WCHAR szPath[MAX_PATH]; WCHAR szFileName[MAX_PATH]; WCHAR* szAppName = L"AppName"; WCHAR* szVersion = L"v1.0"; DWORD dwBufferSize = MAX_PATH; HANDLE hDumpFile; SYSTEMTIME stLocalTime; MINIDUMP_EXCEPTION_INFORMATION ExpParam; GetLocalTime( &stLocalTime ); GetTempPath( dwBufferSize, szPath ); StringCchPrintf( szFileName, MAX_PATH, L"%s%s", szPath, szAppName ); CreateDirectory( szFileName, NULL ); StringCchPrintf( szFileName, MAX_PATH, L"%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp", szPath, szAppName, szVersion, stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay, stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond, GetCurrentProcessId(), GetCurrentThreadId()); hDumpFile = CreateFile(szFileName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0); ExpParam.ThreadId = GetCurrentThreadId(); ExpParam.ExceptionPointers = pExceptionPointers; ExpParam.ClientPointers = TRUE; bMiniDumpSuccessful = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithDataSegs, &ExpParam, NULL, NULL); return EXCEPTION_EXECUTE_HANDLER; } void SomeFunction() { __try { int *pBadPtr = NULL; *pBadPtr = 0; } __except(GenerateDump(GetExceptionInformation())) { } }
- Wildcard comparison
int wildcmp(const char *wild, const char *string) { // Written by Jack Handy - jakkhandy@hotmail.com const char *cp = NULL, *mp = NULL; while ((*string) && (*wild != '*')) { if ((*wild != *string) && (*wild != '?')) { return 0; } wild++; string++; } while (*string) { if (*wild == '*') { if (!*++wild) { return 1; } mp = wild; cp = string+1; } else if ((*wild == *string) || (*wild == '?')) { wild++; string++; } else { wild = mp; string = cp++; } } while (*wild == '*') { wild++; } return !*wild; }
- Reading hexidecimal
_tcstoul(str, ?, radix)
- gluUnProject
CVector3 GetOGLPos(int x, int y) { GLint viewport[4]; GLdouble modelview[16]; GLdouble projection[16]; GLfloat winX, winY, winZ; GLdouble posX, posY, posZ; glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); glGetDoublev( GL_PROJECTION_MATRIX, projection ); glGetIntegerv( GL_VIEWPORT, viewport ); winX = (float)x; winY = (float)viewport[3] - (float)y; glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ ); gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); return CVector3(posX, posY, posZ); }
- Time format
%d/%m/%Y %H:%M:%S
- Float format
%.*g
If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list.
- Conditional include
The __if_exists and __if_not_exists statements allow you to conditionally include code depending on the existence of a symbol.
- ChildWindowFromPoint
HWND AFXAPI _AfxChildWindowFromPoint(HWND hWnd, POINT pt) { ASSERT(hWnd != NULL); // check child windows ::ClientToScreen(hWnd, &pt); HWND hWndChild = ::GetWindow(hWnd, GW_CHILD); for (; hWndChild != NULL; hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT)) { if (_AfxGetDlgCtrlID(hWndChild) != (WORD)0 && (::GetWindowLong(hWndChild, GWL_STYLE) & WS_VISIBLE)) { // see if point hits the child window CRect rect; ::GetWindowRect(hWndChild, rect); if (rect.PtInRect(pt)) return hWndChild; } } return NULL; // not found }
- Struct-array initialisation
AFX_STATIC_DATA CRITICAL_SECTION _afxResourceLock[CRIT_MAX] = { { 0 } };
- MFC-TLS <afxtls_.h>
CSimpleList CTypedSimpleList CThreadSlotData CNoTrackObject CProcessLocalObject CThreadLocal<TYPE>::GetData CProcessLocal<TYPE>::GetData
- Adjusting CRT SxS version:
/*** *crtassem.h - Libraries Assembly information * * Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: * This file has information about Libraries Assembly version. * * [Public] * ****/ #pragma once #ifndef _VC_ASSEMBLY_PUBLICKEYTOKEN #define _VC_ASSEMBLY_PUBLICKEYTOKEN "1fc8b3b9a1e18e3b" #endif #ifndef _CRT_ASSEMBLY_VERSION #if defined _USE_RTM_VERSION #define _CRT_ASSEMBLY_VERSION "8.0.50608.0" #else #define _CRT_ASSEMBLY_VERSION "8.0.50727.762" #endif #endif #ifndef __LIBRARIES_ASSEMBLY_NAME_PREFIX #define __LIBRARIES_ASSEMBLY_NAME_PREFIX "Microsoft.VC80" #endif
