📄 common.cpp
字号:
// Get frame window client rect in screen coordinates
hWndParent = GetParent(hWnd);
if(hWndParent == NULL || hWndParent == GetDesktopWindow())
{
rcParent.top = rcParent.left = 0;
rcParent.right = GetSystemMetrics(SM_CXFULLSCREEN);
rcParent.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
}
else
GetWindowRect(hWndParent, &rcParent);
// Determine the top-left point for the dialog to be centered
GetWindowRect(hWnd, &rcDlg);
cWidth = rcDlg.right - rcDlg.left;
cHeight = rcDlg.bottom - rcDlg.top;
nLeft = rcParent.left + x;
nTop = rcParent.top + y;
// Place the dialog
return MoveWindow(hWnd, nLeft, nTop, cWidth, cHeight, TRUE);
}
//////////////////////////////////////////////////////////////////
// BOOL GetWindowSize
//
//////////////////////////////////////////////////////////////////
SIZE GetWindowSize(HWND hWnd)
{
RECT rect;
SIZE size;
//Obtain window cordinates.
GetWindowRect(hWnd, &rect);
//Fillin SIZE struct
size.cx = rect.right - rect.left;
size.cy = rect.bottom - rect.top;
return size;
}
//////////////////////////////////////////////////////////////////
// ULONG wMessageBox
//
//////////////////////////////////////////////////////////////////
INT wMessageBox(
HWND hwnd, // Parent window for message display
UINT uiStyle, // Style of message box
WCHAR* pwszTitle, // Title for message
WCHAR* pwszFmt, // Format string
... // Substitution parameters
)
{
va_list marker;
WCHAR wszBuffer[MAX_QUERY_LEN];
// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pwszFmt);
_vsnwprintf(wszBuffer, MAX_QUERY_LEN, pwszFmt, marker);
va_end(marker);
//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_QUERY_LEN
wszBuffer[MAX_QUERY_LEN-1] = wEOL;
//Unicode version is supported on both Win95 / WinNT do need to convert
return MessageBoxW(hwnd, wszBuffer, pwszTitle, uiStyle);
}
//////////////////////////////////////////////////////////////////
// INT MessageBox
//
//////////////////////////////////////////////////////////////////
INT MessageBox(
HWND hwnd, // Parent window for message display
UINT uiStyle, // Style of message box
CHAR* pszTitle, // Title for message
CHAR* pszFmt, // Format string
... // Substitution parameters
)
{
va_list marker;
CHAR szBuffer[MAX_QUERY_LEN];
// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pszFmt);
_vsnprintf(szBuffer, MAX_QUERY_LEN, pszFmt, marker);
va_end(marker);
//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_QUERY_LEN
szBuffer[MAX_QUERY_LEN-1] = EOL;
//Delegate
return MessageBoxA(hwnd, szBuffer, pszTitle, uiStyle);
}
//////////////////////////////////////////////////////////////////
// LRESULT wSendMessage
//
//////////////////////////////////////////////////////////////////
LRESULT wSendMessage(HWND hWnd, UINT Msg, WPARAM wParam, WCHAR* pwszBuffer)
{
CHAR szBuffer[MAX_QUERY_LEN];
szBuffer[0] = EOL;
if(pwszBuffer && Msg != WM_GETTEXT && Msg != CB_GETLBTEXT)
{
//Convert to ANSI before sending, since we don't know if this was a GET/SET message
ConvertToMBCS(pwszBuffer, szBuffer, MAX_QUERY_LEN);
}
//Send the message with an ANSI Buffer
LRESULT lResult = SendMessageA(hWnd, Msg, (WPARAM)wParam, (LPARAM)szBuffer);
if(pwszBuffer && Msg == WM_GETTEXT || Msg == CB_GETLBTEXT)
{
//Now convert the result into the users WCHAR buffer
ConvertToWCHAR(szBuffer, pwszBuffer, Msg == WM_GETTEXT ? wParam : MAX_QUERY_LEN);
}
return lResult;
}
//////////////////////////////////////////////////////////////////
// LRESULT wSendMessageFmt
//
//////////////////////////////////////////////////////////////////
LRESULT wSendMessageFmt(HWND hWnd, UINT Msg, WPARAM wParam, WCHAR* pwszFmt, ...)
{
ASSERT(pwszFmt);
ASSERT(wParam != WM_GETTEXT);
va_list marker;
WCHAR wszBuffer[MAX_QUERY_LEN];
// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pwszFmt);
_vsnwprintf(wszBuffer, MAX_QUERY_LEN, pwszFmt, marker);
va_end(marker);
//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_QUERY_LEN
wszBuffer[MAX_QUERY_LEN-1] = wEOL;
//Delegate
return wSendMessage(hWnd, Msg, wParam, wszBuffer);
}
//////////////////////////////////////////////////////////////////
// LRESULT SendMessage
//
//////////////////////////////////////////////////////////////////
LRESULT SendMessageFmt(HWND hWnd, UINT Msg, WPARAM wParam, CHAR* pszFmt, ...)
{
ASSERT(pszFmt);
ASSERT(wParam != WM_GETTEXT);
va_list marker;
CHAR szBuffer[MAX_QUERY_LEN];
// Use format and arguements as input
//This version will not overwrite the stack, since it only copies
//upto the max size of the array
va_start(marker, pszFmt);
_vsnprintf(szBuffer, MAX_QUERY_LEN, pszFmt, marker);
va_end(marker);
//Make sure there is a NULL Terminator, vsnwprintf will not copy
//the terminator if length==MAX_QUERY_LEN
szBuffer[MAX_QUERY_LEN-1] = EOL;
//Delegate
return SendMessageA(hWnd, Msg, (WPARAM)wParam, (LPARAM)szBuffer);
}
//////////////////////////////////////////////////////////////////
// BOOL GetEditBoxValue
//
//////////////////////////////////////////////////////////////////
BOOL GetEditBoxValue(HWND hEditWnd, LONG lMin, LONG lMax, LONG* plValue, BOOL fAllowEmpty)
{
ASSERT(hEditWnd);
ASSERT(plValue);
LONG lValue = 0;
WCHAR wszBuffer[MAX_QUERY_LEN];
wszBuffer[0] = wEOL;
WCHAR* pwszEnd = NULL;
//Get the EditText
wSendMessage(hEditWnd, WM_GETTEXT, MAX_QUERY_LEN, wszBuffer);
if(wszBuffer[0] || !fAllowEmpty)
{
//Convert to LONG
lValue = wcstol(wszBuffer, &pwszEnd, 0);
if(!wszBuffer[0] || lValue<lMin || lValue>lMax || pwszEnd==NULL || pwszEnd[0]!=wEOL)
{
wMessageBox(hEditWnd, MB_TASKMODAL | MB_ICONERROR | MB_OK, wsz_ERROR,
wsz_INVALID_VALUE_, wszBuffer, lMin, lMax);
SetFocus(hEditWnd);
return FALSE;
}
*plValue = lValue;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////
// LONG CB_GetSelectedText
//
//////////////////////////////////////////////////////////////////
LONG CB_GetSelectedText(HWND hWndCombo, CHAR* pszBuffer, ULONG ulMaxSize)
{
ASSERT(pszBuffer);
ASSERT(ulMaxSize);
//Try to obtain the Current Selection
LONG iSel = SendMessage(hWndCombo, CB_GETCURSEL, 0, 0);
//This may fail, if the current selection is entered (DropDown instead of DropList)
if(iSel == CB_ERR)
{
SendMessage(hWndCombo, WM_GETTEXT, ulMaxSize, (LPARAM)pszBuffer);
}
else
{
//Should be limiting the text if this ASSERT is hit!
//Length does not include the NULL Terminator
ULONG ulLength = SendMessage(hWndCombo, CB_GETLBTEXTLEN, iSel, 0);
ASSERT(ulLength < ulMaxSize);
//Obtain the text...
if(ulLength < ulMaxSize)
SendMessage(hWndCombo, CB_GETLBTEXT, iSel, (LPARAM)pszBuffer);
}
return iSel;
}
//////////////////////////////////////////////////////////////////
// LONG CB_GetSelectedText
//
//////////////////////////////////////////////////////////////////
LONG CB_GetSelectedText(HWND hWndCombo, WCHAR* pwszBuffer, ULONG ulMaxSize)
{
ASSERT(pwszBuffer);
ASSERT(ulMaxSize);
//Try to obtain the Current Selection
LONG iSel = SendMessage(hWndCombo, CB_GETCURSEL, 0, 0);
//This may fail, if the current selection is entered (DropDown instead of DropList)
if(iSel == CB_ERR)
{
wSendMessage(hWndCombo, WM_GETTEXT, ulMaxSize, pwszBuffer);
}
else
{
//Should be limiting the text if this ASSERT is hit!
//Length does not include the NULL Terminator
ULONG ulLength = SendMessage(hWndCombo, CB_GETLBTEXTLEN, iSel, 0);
ASSERT(ulLength < ulMaxSize);
//Obtain the text...
if(ulLength < ulMaxSize)
wSendMessage(hWndCombo, CB_GETLBTEXT, iSel, pwszBuffer);
}
return iSel;
}
//////////////////////////////////////////////////////////////////
// LONG CB_SelectText
//
//////////////////////////////////////////////////////////////////
LONG CB_SelectText(HWND hWndCombo, WCHAR* pwszBuffer, BOOL fAddItem)
{
ASSERT(pwszBuffer);
//Try to find the Indicated Text
LONG iSel = wSendMessage(hWndCombo, CB_FINDSTRINGEXACT, -1, pwszBuffer);
//If not found, just add it to the list (if desired)
if(iSel == CB_ERR)
{
if(fAddItem)
{
iSel = wSendMessage(hWndCombo, CB_ADDSTRING, 0, pwszBuffer);
}
else
{
SendMessage(hWndCombo, CB_SETCURSEL, iSel, 0);
wSendMessage(hWndCombo, WM_SETTEXT, 0, pwszBuffer);
}
}
if(iSel != CB_ERR)
iSel = SendMessage(hWndCombo, CB_SETCURSEL, iSel, 0);
return iSel;
}
//////////////////////////////////////////////////////////////////
// LONG CB_SelectText
//
//////////////////////////////////////////////////////////////////
LONG CB_SelectText(HWND hWndCombo, CHAR* pszBuffer, BOOL fAddItem)
{
ASSERT(pszBuffer);
//Try to find the Indicated Text
LONG iSel = SendMessage(hWndCombo, CB_FINDSTRINGEXACT, -1, (LPARAM)pszBuffer);
//If not found, just add it to the list (if desired)
if(iSel == CB_ERR)
{
if(fAddItem)
{
iSel = SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)pszBuffer);
}
else
{
SendMessage(hWndCombo, CB_SETCURSEL, iSel, 0);
SendMessage(hWndCombo, WM_SETTEXT, 0, (LPARAM)pszBuffer);
}
}
if(iSel != CB_ERR)
iSel = SendMessage(hWndCombo, CB_SETCURSEL, iSel, 0);
return iSel;
}
////////////////////////////////////////////////////////////////
// CB_SelectItemValue
//
/////////////////////////////////////////////////////////////////
LONG CB_SelectItemValue(HWND hWndCombo, LONG lParam)
{
//Loop through all Combo Item Values and Select specified one...
LONG iCount = SendMessage(hWndCombo, CB_GETCOUNT, 0, 0);
for(LONG i=0; i<iCount; i++)
{
if(lParam == SendMessage(hWndCombo, CB_GETITEMDATA, i, 0))
{
SendMessage(hWndCombo, CB_SETCURSEL, i, 0);
return i;
}
}
return CB_ERR;
}
//////////////////////////////////////////////////////////////////
// BOOL LV_InsertColumn
//
//////////////////////////////////////////////////////////////////
LONG LV_InsertColumn(HWND hWnd, LONG iColumn, CHAR* szName, LONG iImage)
{
ULONG dwMask = LVCF_TEXT | LVCF_FMT | LVCF_SUBITEM;
INT dwFmt = LVCFMT_LEFT;
if(iImage != IMAGE_NONE)
{
dwMask |= LVCF_IMAGE;
dwFmt |= LVCFMT_IMAGE;
}
//Setup LV_COLUMNINFO
LV_COLUMN lvColumnHeader = { dwMask, dwFmt, 0, szName, 0, 0, iImage, 0};
//LVM_INSERTCOLUMN
return SendMessage(hWnd, LVM_INSERTCOLUMN, (WPARAM)iColumn, (LPARAM)&lvColumnHeader);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_InsertItem
//
//////////////////////////////////////////////////////////////////
LONG LV_InsertItem(HWND hWnd, LONG iItem, LONG iSubItem, CHAR* szName, LONG iParam, LONG iImage)
{
//Calculate the Mask/flags
ULONG dwMask = 0;
if(szName)
dwMask |= LVIF_TEXT;
if(iImage != IMAGE_NONE)
dwMask |= LVIF_IMAGE;
if(iSubItem == 0)
dwMask |= LVIF_PARAM;
//LVM_INSERTITEM
if(iSubItem==0)
{
LV_ITEM lvItem = { dwMask, iItem, iSubItem, 0, 0, szName, 0, iImage, iParam, 0};
return SendMessage(hWnd, LVM_INSERTITEM, 0, (LPARAM)&lvItem);
}
//LVM_SETITEM
else
{
LV_ITEM lvItem = { dwMask, iItem, iSubItem, 0, 0, szName, 0, iImage, iParam, 0};
return SendMessage(hWnd, LVM_SETITEM, 0, (LPARAM)&lvItem);
}
}
//////////////////////////////////////////////////////////////////
// BOOL LV_SetItemText
//
//////////////////////////////////////////////////////////////////
LONG LV_SetItemText(HWND hWnd, LONG iItem, LONG iSubItem, CHAR* szName)
{
//LVM_SETITEM
LV_ITEM lvItem = { LVIF_TEXT, iItem, iSubItem, 0, 0, szName, 0, 0, 0, 0};
return SendMessage(hWnd, LVM_SETITEMTEXT, (WPARAM)iItem, (LPARAM)&lvItem);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_SetItemState
//
//////////////////////////////////////////////////////////////////
LONG LV_SetItemState(HWND hWnd, LONG iItem, LONG iSubItem, LONG lState, LONG lStateMask)
{
//LVM_SETITEM
LV_ITEM lvItem = { LVIF_STATE, iItem, iSubItem, lState, lStateMask, NULL, 0, 0, 0, 0};
return SendMessage(hWnd, LVM_SETITEMSTATE, (WPARAM)iItem, (LPARAM)&lvItem);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_SetItemImage
//
//////////////////////////////////////////////////////////////////
LONG LV_SetItemImage(HWND hWnd, LONG iItem, LONG iSubItem, LONG iImage)
{
//LVM_SETITEM (With IMAGE mask)
LV_ITEM lvItem = { LVIF_IMAGE, iItem, iSubItem, 0, 0, NULL, 0, iImage, 0, 0};
return SendMessage(hWnd, LVM_SETITEM, 0, (LPARAM)&lvItem);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_SetItemParam
//
//////////////////////////////////////////////////////////////////
LONG LV_SetItemParam(HWND hWnd, LONG iItem, LONG iSubItem, LONG lParam)
{
//LVM_SETITEM (With IMAGE mask)
LV_ITEM lvItem = { LVIF_PARAM, iItem, iSubItem, 0, 0, NULL, 0, 0, lParam, 0};
return SendMessage(hWnd, LVM_SETITEM, 0, (LPARAM)&lvItem);
}
//////////////////////////////////////////////////////////////////
// BOOL LV_GetItemText
//
//////////////////////////////////////////////////////////////////
LONG LV_GetItemText(HWND hWnd, LONG iItem, LONG iSubItem, CHAR* szName, ULONG ulMaxSize)
{
ASSERT(szName);
szName[0] = EOL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -