📄 common.cpp
字号:
//LVM_GETITEMTEXT
LV_ITEM lvItem = { LVIF_TEXT, iItem, iSubItem, 0, 0, szName, ulMaxSize, 0, 0, 0};
return SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)iItem, (LPARAM)&lvItem);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_GetItemState
//
//////////////////////////////////////////////////////////////////
LONG LV_GetItemState(HWND hWnd, LONG iItem, LONG iMask)
{
//LVM_GETITEMSTATE
return SendMessage(hWnd, LVM_GETITEMSTATE, (WPARAM)iItem, (LPARAM)iMask);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_GetItemImage
//
//////////////////////////////////////////////////////////////////
LONG LV_GetItemImage(HWND hWnd, LONG iItem, LONG iSubItem)
{
//LVM_GETITEM
LV_ITEM lvItem = { LVIF_IMAGE, iItem, iSubItem, 0, 0, 0, 0, 0, 0, 0};
SendMessage(hWnd, LVM_GETITEM, 0, (LPARAM)&lvItem);
return lvItem.iImage;
}
//////////////////////////////////////////////////////////////////
// LONG LV_GetItemParam
//
//////////////////////////////////////////////////////////////////
LONG LV_GetItemParam(HWND hWnd, LONG iItem, LONG iSubItem)
{
//LVM_GETITEM
LV_ITEM lvItem = { LVIF_PARAM, iItem, iSubItem, 0, 0, 0, 0, 0, 0, 0};
SendMessage(hWnd, LVM_GETITEM, 0, (LPARAM)&lvItem);
return lvItem.lParam;
}
//////////////////////////////////////////////////////////////////
// LONG LV_GetSelItems
//
//////////////////////////////////////////////////////////////////
LONG LV_GetSelItems(HWND hWnd, ULONG* pcItems, LONG** prgSelItems, LONG** prgSelParams)
{
//Get the total Selected Items
LONG i,iSelItem =0;
LONG cItems = SendMessage(hWnd, LVM_GETSELECTEDCOUNT, 0, 0);
//Alloc Output Array
if(prgSelItems)
SAFE_ALLOC(*prgSelItems, LONG, cItems);
if(prgSelParams)
SAFE_ALLOC(*prgSelParams, LONG, cItems);
//Find all params of Selected Items
iSelItem = SendMessage(hWnd, LVM_GETNEXTITEM, (WPARAM)-1, (LPARAM)LVNI_SELECTED);
for(i=0; i<cItems; i++)
{
if(prgSelItems)
(*prgSelItems)[i] = iSelItem;
if(prgSelParams)
(*prgSelParams)[i] = LV_GetItemParam(hWnd, iSelItem, 0);
iSelItem = SendMessage(hWnd, LVM_GETNEXTITEM, (WPARAM)iSelItem, (LPARAM)LVNI_SELECTED);
}
CLEANUP:
if(pcItems)
*pcItems = cItems;
return cItems;
}
//////////////////////////////////////////////////////////////////
// LONG LV_GetAllItems
//
//////////////////////////////////////////////////////////////////
LONG LV_GetAllItems(HWND hWnd, ULONG* pcItems, LONG** prgItems, LONG** prgParams)
{
//Get the total Items
LONG i=0;
LONG cItems = SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0);
//Alloc Output Array
if(prgItems)
SAFE_ALLOC(*prgItems, LONG, cItems);
if(prgParams)
SAFE_ALLOC(*prgParams, LONG, cItems);
//Find all Items
for(i=0; i<cItems; i++)
{
if(prgItems)
(*prgItems)[i] = i;
if(prgParams)
(*prgParams)[i] = LV_GetItemParam(hWnd, i, 0);
}
CLEANUP:
if(pcItems)
*pcItems = cItems;
return cItems;
}
//////////////////////////////////////////////////////////////////
// BOOL LV_FindItem
//
//////////////////////////////////////////////////////////////////
LONG LV_FindItem(HWND hWnd, CHAR* szName, LONG iStart)
{
//LVM_FINDITEM
LV_FINDINFO lvFindInfo = { LVFI_STRING, szName, 0, 0, 0};
return SendMessage(hWnd, LVM_FINDITEM, (WPARAM)iStart, (LPARAM)&lvFindInfo);
}
////////////////////////////////////////////////////////////////
// LONG LV_FindItem
//
/////////////////////////////////////////////////////////////////
LONG LV_FindItem(HWND hWnd, LONG lParam, LONG iStart)
{
//LVM_FINDITEM
LV_FINDINFO lvFindInfo = { LVIF_PARAM, 0, lParam, 0, 0};
return SendMessage(hWnd, LVM_FINDITEM, (WPARAM)iStart, (LPARAM)&lvFindInfo);
}
//////////////////////////////////////////////////////////////////
// BOOL TV_InsertItem
//
//////////////////////////////////////////////////////////////////
HTREEITEM TV_InsertItem(HWND hWnd, HTREEITEM hParent, HTREEITEM hInsAfter, CHAR* szName, LONG iParam, LONG iImage, LONG iSelectedImage)
{
TV_INSERTSTRUCT tvInsertStruct = { hParent, hInsAfter, { TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, 0, 0, 0, szName, 0, iImage, iSelectedImage, 0, iParam} };
//TVM_INSERTITEM
return (HTREEITEM)SendMessage(hWnd, TVM_INSERTITEM, 0, (LPARAM)&tvInsertStruct);
}
//////////////////////////////////////////////////////////////////
// BOOL TV_GetItemText
//
//////////////////////////////////////////////////////////////////
LONG TV_GetItemText(HWND hWnd, HTREEITEM hItem, CHAR* szBuffer, LONG ulMaxSize)
{
TVITEM tvItem = { TVIF_TEXT, hItem, 0, 0, szBuffer, ulMaxSize, 0, 0, 0, 0};
//GetItem Text
return SendMessage(hWnd, TVM_GETITEM, 0, (LPARAM)&tvItem);
}
//////////////////////////////////////////////////////////////////
// BOOL TV_GetItemParam
//
//////////////////////////////////////////////////////////////////
LONG TV_GetItemParam(HWND hWnd, HTREEITEM hItem)
{
//no-op
if(hItem == NULL)
return LVM_ERR;
TVITEM tvItem = { TVIF_PARAM, hItem, 0, 0, NULL, 0, 0, 0, 0, 0};
//GetItem
SendMessage(hWnd, TVM_GETITEM, 0, (LPARAM)&tvItem);
//return the lParam
return tvItem.lParam;
}
//////////////////////////////////////////////////////////////////
// BOOL TV_FindItem
//
//////////////////////////////////////////////////////////////////
HTREEITEM TV_FindItem(HWND hWnd, HTREEITEM hParent, CHAR* szName)
{
ASSERT(hWnd);
ASSERT(szName);
CHAR szBuffer[MAX_NAME_LEN];
TVITEM tvItem = { TVIF_TEXT, 0, 0, 0, szBuffer, MAX_NAME_LEN, 0, 0, 0, 0};
tvItem.hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hParent);
while(tvItem.hItem)
{
//Try to find this string in the Tree
if(SendMessage(hWnd, TVM_GETITEM, 0, (LPARAM)&tvItem))
{
if(strcmp(szName, tvItem.pszText)==0)
return tvItem.hItem;
}
//Otherwise get the NextItem and continue...
tvItem.hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)tvItem.hItem);
}
return NULL;
}
//////////////////////////////////////////////////////////////////
// BOOL SB_SetScrollInfo
//
//////////////////////////////////////////////////////////////////
LONG SB_SetScrollInfo(HWND hWnd, INT dwFlags, INT iPos, INT iRangeSize, INT iPageSize, BOOL fRedraw)
{
SCROLLINFO ScrollInfo = { sizeof(SCROLLINFO), SIF_ALL, 0, iRangeSize, iPageSize, iPos, 0};
//SetScrollInfo
return SetScrollInfo(hWnd, dwFlags, &ScrollInfo, fRedraw);
}
//////////////////////////////////////////////////////////////////
// BOOL SB_GetScrollInfo
//
//////////////////////////////////////////////////////////////////
LONG SB_GetScrollInfo(HWND hWnd, INT dwFlags, INT* piPos, INT* piRangeSize, INT* piPageSize)
{
INT iPos = 0;
INT iRangeSize = 0;
INT iPageSize = 0;
SCROLLINFO ScrollInfo = { sizeof(SCROLLINFO), SIF_ALL, 0, iRangeSize, iPageSize, iPos, 0};
//SetScrollInfo
INT iResult = GetScrollInfo(hWnd, dwFlags, &ScrollInfo);
//Fill in arguments
if(piPos)
*piPos = iPos;
if(piRangeSize)
*piRangeSize = iRangeSize;
if(piPageSize)
*piPageSize = iPageSize;
return iResult;
}
//////////////////////////////////////////////////////////////////
// HRESULT ReplaceString
//
//////////////////////////////////////////////////////////////////
HRESULT ReplaceString(CHAR* pszBuffer, CHAR* pszTokens, CHAR* pszReplace)
{
//no-op
if(pszBuffer == NULL || pszTokens == NULL || pszReplace == NULL)
return E_FAIL;
//Make sure we have the same replacement as the search string
//IE: (this function doesn't handle growing/shrinking the string)
ULONG ulStrLen = strlen(pszTokens);
if(ulStrLen != strlen(pszReplace) || ulStrLen > strlen(pszBuffer))
return E_FAIL;
//Repleace all occurrences of pszTokens with pszReplace
CHAR* pszFound = strstr(pszBuffer, pszTokens);
while(pszFound)
{
memcpy(pszFound, pszReplace, sizeof(CHAR)*ulStrLen);
//Try to find the next occurrence
pszFound += ulStrLen;
pszFound = strstr(pszFound, pszTokens);
}
return S_OK;
}
//////////////////////////////////////////////////////////////////
// HRESULT GetEditBoxSelection
//
//////////////////////////////////////////////////////////////////
HRESULT GetEditBoxSelection(HWND hWndEdit, CHAR* pszBuffer, ULONG ulMaxSize)
{
ASSERT(hWndEdit);
ASSERT(pszBuffer);
ASSERT(ulMaxSize);
LONG dwStartPos = 0;
LONG dwEndPos = 0;
LONG iCharIndex = -1;
LONG iLineLength = 0;
CHAR* psz = pszBuffer;
//Obtain the EditBox text (rules below).
// 1. First try to obtain whatever text is highlighted (even multiline highlight)
// 2. Or if nothing is highlighted take the entire line of the cursor
//Obtain the Selected Text Start and End Positions
SendMessage(hWndEdit, EM_GETSEL, (WPARAM)&dwStartPos, (LPARAM)&dwEndPos);
//If there is no selected Text
//Just get the entire line, as if it were highlighted...
if(dwStartPos == dwEndPos)
{
dwStartPos = SendMessage(hWndEdit, EM_LINEINDEX, (WPARAM)-1, (LPARAM)0);
dwEndPos = dwStartPos + SendMessage(hWndEdit, EM_LINELENGTH, dwStartPos, 0);
}
//We actually may not have enough buffer for the selected text
if(dwEndPos-dwStartPos > (LONG)ulMaxSize)
dwEndPos = dwStartPos + ulMaxSize -1;
//Get Line for the Current Position
LONG iLine = SendMessage(hWndEdit, EM_LINEFROMCHAR, dwStartPos, 0);
LONG iEndLine = SendMessage(hWndEdit, EM_LINEFROMCHAR, dwEndPos, 0);
while(iLine <= iEndLine)
{
//Get the Text for this line
//The first word of the buffer represents the MaxSize of the Buffer
((DWORD*)psz)[0] = ulMaxSize-1;
iLineLength = SendMessage(hWndEdit, EM_GETLINE, iLine, (LPARAM)psz);
//Special Character Post Processing...
ReplaceString(psz, "\t", " ");
ReplaceString(psz, "\n", " ");
ReplaceString(psz, "\r", " ");
//The first time through there are fixups that need to occur
//This is due to the fact that the starting hightlighted char may not
//be the first char on the line.
if(iCharIndex == -1)
{
//Obtain the CharIndex of the first char of the first line
iCharIndex = SendMessage(hWndEdit, EM_LINEINDEX, (WPARAM)iLine, (LPARAM)0);
iLineLength = iLineLength - (dwStartPos - iCharIndex);
memmove(psz, psz + (dwStartPos - iCharIndex), iLineLength);
}
//Null Terminator
if(iLine == iEndLine)
{
//Obtain the CharIndex of the first char of the last line
iCharIndex = SendMessage(hWndEdit, EM_LINEINDEX, (WPARAM)iLine, (LPARAM)0);
//Also have the other case where the startpos may be the same line
//as the endpos
if(dwStartPos > iCharIndex)
psz[dwEndPos-dwStartPos] = wEOL;
else
psz[dwEndPos-iCharIndex] = wEOL;
}
else
{
psz += iLineLength;
//Since were dealing with MultiLine edit here
//add an extra space in between statements, to make it more friendly...
psz[0] = ' ';
psz += 1;
}
//Get TheNext Line from the current position
iLine++;
}
//Highlight the text we used...
SendMessage(hWndEdit, EM_SETSEL, (WPARAM)dwStartPos, (LPARAM)dwEndPos);
return S_OK;
}
//////////////////////////////////////////////////////////////////
// HRESULT ReplaceEditBoxSelection
//
//////////////////////////////////////////////////////////////////
HRESULT ReplaceEditBoxSelection(HWND hWndEdit, CHAR* pszBuffer)
{
ASSERT(hWndEdit);
ASSERT(pszBuffer);
LONG dwStartPos = 0;
LONG dwStartPos2 = 0;
LONG dwEndPos = 0;
LONG dwEndPos2 = 0;
//Obtain the Selected Text Start and End Positions
SendMessage(hWndEdit, EM_GETSEL, (WPARAM)&dwStartPos, (LPARAM)&dwEndPos);
//Replace the Selection
SendMessage(hWndEdit, EM_REPLACESEL, TRUE, (LPARAM)pszBuffer);
//Highlight the text we used...
SendMessage(hWndEdit, EM_GETSEL, (WPARAM)&dwStartPos2, (LPARAM)&dwEndPos2);
SendMessage(hWndEdit, EM_SETSEL, (WPARAM)dwStartPos, (LPARAM)dwEndPos2);
return S_OK;
}
////////////////////////////////////////////////////////////////
// BOOL DisplayContextMenu
//
/////////////////////////////////////////////////////////////////
BOOL DisplayContextMenu(HINSTANCE hInst, HWND hWnd, WORD iID, INT xPos, INT yPos, HWND hWndParent, BOOL fRelCords)
{
//Load the SubMenu
HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(iID));
HMENU hSubMenu = GetSubMenu(hMenu, 0);
RECT rect;
GetWindowRect(hWnd, &rect);
//Coordinates might be Screen Coordinates or Relative
if(fRelCords)
{
rect.top += yPos;
rect.bottom += yPos;
rect.left += xPos;
rect.right += xPos;
}
//This message may have come from the keyboard
//Just display at upper left corner
if(xPos < rect.left || xPos > rect.right || yPos < rect.top || yPos > rect.bottom)
{
xPos = rect.left + 5;
yPos = rect.top + 5;
}
//Display SubMenu
//Wants (x,y) in Screen Coordinates
BOOL bResult = TrackPopupMenu(hSubMenu,
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
xPos,
yPos,
0,
hWndParent,
NULL);
DestroyMenu(hSubMenu);
DestroyMenu(hMenu);
return bResult;
}
///////////////////////////////////////////////////////////////
// BrowseOpenFileName
//
///////////////////////////////////////////////////////////////
HRESULT BrowseOpenFileName(HINSTANCE hInstance, HWND hWnd, CHAR* pszTitle, CHAR* pszFileName, ULONG ulMaxSize)
{
ASSERT(pszFileName);
static ULONG ulFilterIndex = 5; //Default to *.*
HRESULT hr = E_FAIL;
CHAR szCustFilter[MAX_NAME_LEN];
strcpy(szCustFilter, "User Defined");
//Setup OPENFILENAME struct...
OPENFILENAME ofn;
memset( &ofn, 0, sizeof( ofn ));
ofn.lStructSize = sizeof( OPENFILENAME );
ofn.hwndOwner = hWnd;
ofn.hInstance = hInstance;
ofn.lpstrFile = pszFileName;
ofn.nMaxFile = ulMaxSize;
ofn.lpstrTitle = pszTitle;
ofn.lpstrFilter = "DataSource Files (.dsn;.kag;.sav)\0*.dsn;*.kag;*.sav;\0DataBase Files (.mdb;.db;.dbf)\0*.mdb;*.db;*.dbf;\0Program Files (.xls;.clb)\0*.xls;*.clb;\0Text Files (.txt;.csv)\0*.txt;*.csv\0All Files (*.*)\0*.*\0\0";
ofn.lpstrCustomFilter = szCustFilter;
ofn.nMaxCustFilter = MAX_NAME_LEN;
ofn.nFilterIndex = ulFilterIndex;
ofn.Flags = OFN_CREATEPROMPT |
OFN_EXPLORER |
OFN_FILEMUSTEXIST |
OFN_PATHMUSTEXIST |
OFN_HIDEREADONLY |
OFN_OVERWRITEP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -