📄 wndclass.h
字号:
BOOL SetEditSel(int nStart, int nEnd) { return (BOOL)::SendMessage(m_hWnd, CB_SETEDITSEL, 0, MAKELONG(nStart, nEnd)); }
DWORD GetItemData(int n) { return ::SendMessage(m_hWnd, CB_GETITEMDATA, n, 0); }
int SetItemData(int n, DWORD dw) { return (int)::SendMessage(m_hWnd, CB_SETITEMDATA, n, (LPARAM)dw); }
void *GetItemDataPtr(int n) { return (LPVOID)GetItemData(n); }
int SetItemDataPtr(int n, void *pData) { return SetItemData(n, (DWORD)(LPVOID)pData); }
int GetLBText(int n, LPTSTR psz) { return (int)::SendMessage(m_hWnd, CB_GETLBTEXT, n, (LPARAM)psz); }
int GetLBTextLen(int n) { return (int)::SendMessage(m_hWnd, CB_GETLBTEXTLEN, n, 0); }
int AddString(LPCTSTR psz) { return (int)::SendMessage(m_hWnd, CB_ADDSTRING, 0, (LPARAM)psz); }
int DeleteString(UINT n) { return (int)::SendMessage(m_hWnd, CB_DELETESTRING, n, 0);}
int InsertString(int n, LPCTSTR psz) { return (int)::SendMessage(m_hWnd, CB_INSERTSTRING, n, (LPARAM)psz); }
void ResetContent() { ::SendMessage(m_hWnd, CB_RESETCONTENT, 0, 0); }
int FindString(int n, LPCTSTR psz) { return (int)::SendMessage(m_hWnd, CB_FINDSTRING, n, (LPARAM)psz); }
int SelectString(int n, LPCTSTR psz) { return (int)::SendMessage(m_hWnd, CB_SELECTSTRING, n, (LPARAM)psz); }
};
class CListBox : public CWnd
{
CListBox(HWND hWndParent, int nComboID)
{
m_hWnd = ::GetDlgItem(hWndParent, nComboID);
ASSERT(::IsWindow(m_hWnd));
}
int GetCount() const { return (int)::SendMessage(m_hWnd, LB_GETCOUNT, 0, 0); }
int GetCurSel() const { return (int)::SendMessage(m_hWnd, LB_GETCURSEL, 0, 0); }
int SetCurSel(int n) { return (int)::SendMessage(m_hWnd, LB_SETCURSEL, n, 0); }
int GetHorizontalExtent() const { return (int)::SendMessage(m_hWnd, LB_GETHORIZONTALEXTENT, 0, 0); }
void SetHorizontalExtent(int n) { ::SendMessage(m_hWnd, LB_SETHORIZONTALEXTENT, n, 0); }
int GetSelCount() const { return (int)::SendMessage(m_hWnd, LB_GETSELCOUNT, 0, 0); }
int GetSelItems(int n, LPINT rg) const { return (int)::SendMessage(m_hWnd, LB_GETSELITEMS, n, (LPARAM)rg); }
int GetTopIndex() const { return (int)::SendMessage(m_hWnd, LB_GETTOPINDEX, 0, 0); }
int SetTopIndex(int n) { return (int)::SendMessage(m_hWnd, LB_SETTOPINDEX, n, 0);}
DWORD GetItemData(int n) const { return ::SendMessage(m_hWnd, LB_GETITEMDATA, n, 0); }
int SetItemData(int n, DWORD dw) { return (int)::SendMessage(m_hWnd, LB_SETITEMDATA, n, (LPARAM)dw); }
void* GetItemDataPtr(int n) const { return (LPVOID)::SendMessage(m_hWnd, LB_GETITEMDATA, n, 0); }
int SetItemDataPtr(int n, void *pData) { return SetItemData(n, (DWORD)(LPVOID)pData); }
int GetItemRect(int n, LPRECT p) const { return (int)::SendMessage(m_hWnd, LB_GETITEMRECT, n, (LPARAM)p); }
int GetSel(int n) const { return (int)::SendMessage(m_hWnd, LB_GETSEL, n, 0); }
int SetSel(int n, BOOL b) { return (int)::SendMessage(m_hWnd, LB_SETSEL, b, n); }
int GetText(int n, LPTSTR psz) const { return (int)::SendMessage(m_hWnd, LB_GETTEXT, n, (LPARAM)psz); }
int GetTextLen(int n) const { return (int)::SendMessage(m_hWnd, LB_GETTEXTLEN, n, 0); }
void SetColumnWidth(int n) { ::SendMessage(m_hWnd, LB_SETCOLUMNWIDTH, n, 0); }
BOOL SetTabStops(int n, LPINT rg) { return (BOOL)::SendMessage(m_hWnd, LB_SETTABSTOPS, n, (LPARAM)rg); }
void SetTabStops() { ::SendMessage(m_hWnd, LB_SETTABSTOPS, 0, 0); }
BOOL SetTabStops(const int& cx) { return (BOOL)::SendMessage(m_hWnd, LB_SETTABSTOPS, 1, (LPARAM)(LPINT)&cx); }
int SetItemHeight(int n, UINT cy) { return (int)::SendMessage(m_hWnd, LB_SETITEMHEIGHT, n, MAKELONG(cy, 0)); }
int GetItemHeight(int n) const { return (int)::SendMessage(m_hWnd, LB_GETITEMHEIGHT, n, 0L); }
int FindStringExact(int n, LPCTSTR psz) const { return (int)::SendMessage(m_hWnd, LB_FINDSTRINGEXACT, n, (LPARAM)psz); }
int GetCaretIndex() const { return (int)::SendMessage(m_hWnd, LB_GETCARETINDEX, 0, 0L); }
int SetCaretIndex(int n, BOOL bScroll) { return (int)::SendMessage(m_hWnd, LB_SETCARETINDEX, n, MAKELONG(bScroll, 0)); }
int AddString(LPCTSTR lpszItem) { return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }
int DeleteString(UINT n) { return (int)::SendMessage(m_hWnd, LB_DELETESTRING, n, 0); }
int InsertString(int n, LPCTSTR psz) { return (int)::SendMessage(m_hWnd, LB_INSERTSTRING, n, (LPARAM)psz); }
void ResetContent() { ::SendMessage(m_hWnd, LB_RESETCONTENT, 0, 0); }
int Dir(UINT attr, LPCTSTR psz) { return (int)::SendMessage(m_hWnd, LB_DIR, attr, (LPARAM)psz); }
int FindString(int n, LPCTSTR psz) const{ return (int)::SendMessage(m_hWnd, LB_FINDSTRING, n, (LPARAM)psz); }
int SelectString(int n, LPCTSTR psz) { return (int)::SendMessage(m_hWnd, LB_SELECTSTRING, n, (LPARAM)psz); }
int SelItemRange(BOOL b, int f, int l) { return b ? (int)::SendMessage(m_hWnd, LB_SELITEMRANGEEX, f, l) : (int)::SendMessage(m_hWnd, LB_SELITEMRANGEEX, l, f); }
void SetAnchorIndex(int n) { ::SendMessage(m_hWnd, LB_SETANCHORINDEX, n, 0); }
int GetAnchorIndex() const { return (int)::SendMessage(m_hWnd, LB_GETANCHORINDEX, 0, 0); }
LCID GetLocale() const { return (LCID)::SendMessage(m_hWnd, LB_GETLOCALE, 0, 0); }
LCID SetLocale(LCID n) { return (LCID)::SendMessage(m_hWnd, LB_SETLOCALE, (WPARAM)n, 0); }
};
// Device context and drawing classes
class CDC
{
public:
HDC m_hDC;
HWND m_hWnd;
public:
CDC(HDC hDC=NULL, HWND hWnd=NULL) { m_hDC = hDC; m_hWnd = hWnd; }
operator HDC() { return m_hDC; }
bool operator==(HDC hDC) { return hDC == m_hDC; }
bool operator!=(HDC hDC) { return hDC != m_hDC; }
void operator=(HDC hDC) { m_hDC = hDC; }
COLORREF SetBkColor(COLORREF c) { return ::SetBkColor(m_hDC, c); }
int SetBkMode(int nMode) { return ::SetBkMode(m_hDC, nMode); }
COLORREF SetTextColor(COLORREF c) { return ::SetTextColor(m_hDC, c); }
BOOL TextOut(int x, int y, const char *psz) { return ::TextOut(m_hDC, x, y, psz, strlen(psz)); }
HGDIOBJ SelectObject(HGDIOBJ hGDI) { return ::SelectObject(m_hDC, hGDI); }
BOOL BitBlt(HDC hDC, int x, int y, int w, int h, int xSrc=0, int ySrc=0, DWORD dwROP=SRCCOPY)
{
return ::BitBlt(hDC, x, y, w, h, m_hDC, xSrc, ySrc, dwROP);
}
BOOL CenteredTextOut(const char *psz)
{
CRect rect;
SIZE size;
int nLength = strlen(psz);
::GetClientRect(m_hWnd, &rect);
::GetTextExtentPoint32(m_hDC, psz, nLength, &size);
return ::TextOut(m_hDC, (rect.Width()-size.cx)/2, (rect.Height()-size.cy)/2, psz, nLength);
}
};
class CClientDC : public CDC
{
public:
CClientDC(HWND hWnd=NULL) { if(hWnd) Init(hWnd); }
void Init(HWND hWnd) { m_hWnd = hWnd; m_hDC = ::GetDC(m_hWnd); }
void Release() { if(m_hDC) { ::ReleaseDC(m_hWnd, m_hDC); m_hDC = NULL; } }
~CClientDC() { Release(); }
};
class CWindowDC : public CDC
{
public:
CWindowDC(HWND hWnd=NULL) { if(hWnd) Init(hWnd); }
void Init(HWND hWnd) { m_hWnd = hWnd; m_hDC = ::GetWindowDC(m_hWnd); }
void Release() { if(m_hDC) { ::ReleaseDC(m_hWnd, m_hDC); m_hDC = NULL; } }
~CWindowDC() { Release(); }
};
class CPaintDC : public CDC
{
public:
PAINTSTRUCT m_ps;
public:
CPaintDC(HWND hWnd) { m_hWnd = hWnd; ::BeginPaint(m_hWnd, &m_ps); m_hDC = m_ps.hdc; }
~CPaintDC() { ::EndPaint(m_hWnd, &m_ps); }
};
class CCompatibleDC : public CDC
{
public:
CCompatibleDC(HDC hDC=NULL) { m_hDC = ::CreateCompatibleDC(hDC); }
~CCompatibleDC() { if(m_hDC) ::DeleteDC(m_hDC); }
};
class CWinApp : public CWnd
{
// Attributes
protected:
HINSTANCE m_hInstance;
HINSTANCE m_hPrevInstance;
const char *m_pszCmdLine;
int m_nShowCmd;
char m_szAppName[_MAX_PATH];
char m_szAppPath[_MAX_PATH];
char m_szStartupPath[_MAX_PATH];
char m_szRegistryKey[_MAX_PATH];
public:
static CWinApp *m_pMainApp;
// Operations
protected:
public:
CWinApp(HINSTANCE hInstance, HINSTANCE hPrevInstance=NULL, char *pszCmdLine="", int nShowCmd=SW_SHOWNORMAL)
{
m_pMainApp = this;
m_hInstance = hInstance;
m_hPrevInstance = hPrevInstance;
m_pszCmdLine = pszCmdLine;
m_nShowCmd = nShowCmd;
LoadString(IDS_APPLICATION, m_szAppName);
::GetModuleFileName(NULL, m_szAppPath, _MAX_PATH);
::GetCurrentDirectory(_MAX_PATH, m_szStartupPath);
sprintf(m_szRegistryKey, "Software\\%s", m_szAppName);
}
~CWinApp()
{
m_pMainApp = NULL;
}
virtual bool InitInstance() { return false; }
virtual int ExitInstance() { return 0; }
virtual bool OnIdle() { return false; }
virtual bool PreTranslateMessage(MSG *pMsg) { return false; }
virtual int Run()
{
MSG msg;
msg.message = 0;
while(msg.message != WM_QUIT)
{
OnIdle();
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
if(!PreTranslateMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return 0;
}
HINSTANCE GetInstanceHandle() { return m_hInstance; }
const char *GetAppName() { return m_szAppName; }
const char *GetAppPath() { return m_szAppPath; }
const char *GetStartupPath() { return m_szStartupPath; }
const char *GetRegistryKey() { return m_szRegistryKey; }
void SetAppName(const char *psz) { strcpy(m_szAppName, psz); }
void SetAppPath(const char *psz) { strcpy(m_szAppPath, psz); }
void SetStartupPath(const char *psz) { strcpy(m_szStartupPath, psz); }
void SetRegistryKey(const char *psz) { strcpy(m_szRegistryKey, psz); }
int MessageBox(const char *psz, UINT uType=MB_OK) { return ::MessageBox(NULL, psz, m_szAppName, uType); }
int LoadString(int nID, char *psz, int nMax=_MAX_PATH) { return ::LoadString(m_hInstance, nID, psz, nMax); }
int GetProfileInt(const char *pszSection, const char *pszEntry, int nDefault=0)
{
HKEY hKey;
DWORD dw, dwType, dwValue;
char szBuffer[_MAX_PATH];
sprintf(szBuffer, "%s\\%s", m_szRegistryKey, pszSection);
if(RegCreateKeyEx(HKEY_CURRENT_USER, szBuffer, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw) == ERROR_SUCCESS)
{
dw = sizeof(DWORD);
if(RegQueryValueEx(hKey, pszEntry, NULL, &dwType, (unsigned char *)&dwValue, &dw) == ERROR_SUCCESS && dwType == REG_DWORD)
nDefault = dwValue;
RegCloseKey(hKey);
}
return nDefault;
}
bool WriteProfileInt(const char *pszSection, const char *pszEntry, int nValue)
{
HKEY hKey;
DWORD dw;
char szBuffer[_MAX_PATH];
sprintf(szBuffer, "%s\\%s", m_szRegistryKey, pszSection);
bool bSuccess = false;
if(RegCreateKeyEx(HKEY_CURRENT_USER, szBuffer, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw) == ERROR_SUCCESS)
{
bSuccess = (RegSetValueEx(hKey, pszEntry, 0, REG_DWORD, (unsigned char *)&nValue, sizeof(DWORD)) == ERROR_SUCCESS);
RegCloseKey(hKey);
}
return bSuccess;
}
const char *GetProfileString(const char *pszSection, const char *pszEntry, const char *pszDefault="")
{
HKEY hKey;
DWORD dw, dwType;
static char szBuffer[_MAX_PATH];
sprintf(szBuffer, "%s\\%s", m_szRegistryKey, pszSection);
if(RegCreateKeyEx(HKEY_CURRENT_USER, szBuffer, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw) == ERROR_SUCCESS)
{
dw = _MAX_PATH;
if(RegQueryValueEx(hKey, pszEntry, NULL, &dwType, (unsigned char *)szBuffer, &dw) == ERROR_SUCCESS && dwType == REG_SZ)
pszDefault = szBuffer;
RegCloseKey(hKey);
}
return pszDefault;
}
bool WriteProfileString(const char *pszSection, const char *pszEntry, const char *pszValue)
{
HKEY hKey;
DWORD dw;
char szBuffer[_MAX_PATH];
sprintf(szBuffer, "%s\\%s", m_szRegistryKey, pszSection);
bool bSuccess = false;
if(RegCreateKeyEx(HKEY_CURRENT_USER, szBuffer, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw) == ERROR_SUCCESS)
{
bSuccess = pszValue ? (RegSetValueEx(hKey, pszEntry, 0, REG_SZ, (unsigned char *)pszValue, strlen(pszValue)+1) == ERROR_SUCCESS) :
(RegDeleteValue(hKey, pszEntry) == ERROR_SUCCESS);
RegCloseKey(hKey);
}
return bSuccess;
}
};
inline CWinApp *GetApp() { return CWinApp::m_pMainApp; }
#endif // __WndClass_h__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -