📄 util.c
字号:
//
// (w)ritten by Chuan-Liang Teng 2006, mailto:clteng@ms6.hinet.net
//
#include "util.h"
//
extern HINSTANCE _hInst;
extern HWND _hDlg;
//
void ShowErrorMsg(HWND hWnd, const DWORD dwErrorCode, const char *szFunctionName)
{
void* lpMsgBuf;
//
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
0L,
dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char*)&lpMsgBuf,
0,
0L))
return;
MessageBox(hWnd, (const char*)lpMsgBuf, szFunctionName, MB_ICONSTOP | MB_OK);
LocalFree(lpMsgBuf);
};
//
void WaitCursor(const char bIsWait)
{
HCURSOR hCur = 0L;
//
hCur = (HCURSOR)LoadImage(_hInst, (bIsWait)?IDC_WAIT:IDC_ARROW,
IMAGE_CURSOR,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
if (hCur)
SetCursor(hCur);
};
//
void DisableCloseBox(HWND hDlg)
{
DWORD dwStyle = GetClassLongPtr(hDlg, GCL_STYLE);
dwStyle |= CS_NOCLOSE;
SetClassLongPtr(hDlg, GCL_STYLE, dwStyle);
};
//
/*********************************************************************/
/* ListView */
/*********************************************************************/
void ListViewDeleteItem(HWND hList, const short wItem)
{
if (wItem == -1)
SendMessage(hList, LVM_DELETEALLITEMS, 0, 0);
else
SendMessage(hList, LVM_DELETEITEM, (WPARAM)wItem, 0);
};
//
void ListViewSetExtStyle(HWND hListView, const DWORD dwStyle)
{
DWORD style;
//
style = (dwStyle) ? dwStyle :
LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_FLATSB;
SendMessage(hListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)style);
};
//
void ListViewSetSelectItem(HWND hListView, const short iItem)
{
LVITEM lvItem;
short i;
//
lvItem.iItem = iItem;
lvItem.mask = LVIF_STATE;
lvItem.state = LVIS_SELECTED;
i = (short)SendMessage(hListView, LVM_SETITEMSTATE, iItem, (LPARAM)&lvItem);
};
//
void ListViewInsertColumnText(HWND hListView, const short wIdx,
const int wFmt, char *pszText, const BOOL bFinal)
{
LVCOLUMN column;
TEXTMETRIC text;
int nWidth;
HDC hDC = GetDC(_hDlg);
int fmt;
//
GetTextMetrics(hDC, &text);
nWidth = text.tmAveCharWidth*20;
ReleaseDC(_hDlg, hDC);
//
if (!wFmt)
fmt = LVCFMT_LEFT;
column.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH;
column.pszText = pszText;
column.fmt = fmt;
if (!bFinal)
column.cx = nWidth;
else
column.cx = nWidth*100;
if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1)
ShowErrorMsg(_hDlg, GetLastError(), "InitialListView");
};
//
void ListViewAddColumnImageText(HWND hWnd, const UINT nID, const short wIdx,
const int wFmt, char *pszText, const BOOL bFinal)
{
LVCOLUMN column;
TEXTMETRIC text;
int nWidth;
HWND hListView = GetDlgItem(hWnd, nID);
HDC hDC = GetDC(hWnd);
int fmt;
//
GetTextMetrics(hDC, &text);
nWidth = text.tmAveCharWidth*20;
ReleaseDC(hWnd, hDC);
//
if (!wFmt)
fmt = LVCFMT_LEFT;
column.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH|LVCF_IMAGE;
column.pszText = pszText;
column.fmt = fmt;
if (!bFinal)
column.cx = nWidth;
else
column.cx = 700;
if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1)
ShowErrorMsg(hWnd, GetLastError(), "InitialListView");
};
//
void ListViewInsertItemText(HWND hListView, const int iItem,
const int iSubItem, char *pszText)
{
LVITEM item;
//
item.mask = LVIF_TEXT;
item.iItem = iItem;
item.iSubItem = iSubItem;
item.state = 0;
item.stateMask = 0;
item.iImage = 0;
item.lParam = 0;
item.pszText = pszText;
item.cchTextMax = (int)strlen(pszText);
if (!iSubItem)
SendMessage(hListView, LVM_INSERTITEM, 0, (LPARAM)&item);
else
SendMessage(hListView, LVM_SETITEMTEXT, iItem, (LPARAM)&item);
};
//
int ListViewGetItemSelect(HWND hListView)
{
return (int)SendMessage(hListView, LVM_GETNEXTITEM, -1, MAKELPARAM((UINT)LVNI_SELECTED, 0));
};
//
int ListViewGetItemCount(HWND hListView)
{
return (int)SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
};
//
void ListViewGetItemText(HWND hListView, const int iSubItem, char *pszText)
{
LVITEM item;
int idx = ListViewGetItemSelect(hListView);
//
if (idx == -1)
return;
//
item.iSubItem = iSubItem;
item.pszText = pszText;
item.cchTextMax = 255;
SendMessage(hListView, LVM_GETITEMTEXT, idx, (LPARAM)&item);
};
//
void ListViewGetSpecItem(HWND hListView, const int iItem, const int iSubItem, char *pszText)
{
LVITEM item;
//
if (iItem == -1)// || iItem == 0)
return;
//
item.iSubItem = iSubItem;
item.pszText = pszText;
item.cchTextMax = 255;
SendMessage(hListView, LVM_GETITEMTEXT, iItem, (LPARAM)&item);
};
//
UINT IsListViewDBClkEvent(const UINT nID, NMHDR* pnmh)
{
if (pnmh->idFrom == nID)
{
if (pnmh->code == NM_DBLCLK)
return nID;
};
return 0;
};
//
UINT IsListViewClkEvent(const UINT nID, NMHDR* pnmh)
{
if (pnmh->idFrom == nID)
{
if (pnmh->code == NM_CLICK)
return NM_CLICK;
else if (pnmh->code == NM_RCLICK)
return NM_RCLICK;
};
return 0;
};
//
void ListViewRemoveAllItems(HWND hListView)
{
short wCnt = (short)SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
short wLoop;
//
for (wLoop = 0; wLoop < wCnt; wLoop++)
SendMessage(hListView, LVM_DELETEITEM, 0, 0);
SendMessage(hListView, LVM_DELETEALLITEMS, 0, 0);
};
//
void ListViewRemoveItem(HWND hDlg, const UINT nID, const int iItem)
{
SendMessage(GetDlgItem(hDlg, nID), LVM_DELETEITEM, (WPARAM)iItem, 0);
};
//
void ListViewRemoveColumn(HWND hDlg, const UINT nID, const int iCol)
{
SendMessage(GetDlgItem(hDlg, nID), LVM_DELETECOLUMN, (WPARAM)iCol, 0);
};
//
BOOL UnicodeToAnsi(const wchar_t *Source, const short wLen, char *Destination, const short sLen)
{
return WideCharToMultiByte(CP_ACP,
WC_COMPOSITECHECK,
Source,
wLen,
Destination,
sLen,
0L, 0L);
};
//
/*********************************************************************/
/* Unicode */
/*********************************************************************/
BOOL AnsiToUnicode(const char *Source, const short sLen, wchar_t *Destination, const short wLen)
{
return MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,
Source,
sLen,
Destination,
wLen);
};
//
/*********************************************************************/
/* TreeView */
/*********************************************************************/
HTREEITEM TreeViewInsertRootText(HWND hWnd, UINT nID,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -