📄 wndclass.h
字号:
/*
s_p_oneil@hotmail.com
Copyright (c) 2000, Sean O'Neil
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of this project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __WndClass_h__
#define __WndClass_h__
#include "Resource.h"
// Assertion and trace declarations
#include <assert.h>
#ifdef _DEBUG
#define ASSERT assert
#define TRACE Trace
#else
#define ASSERT ((void)0)
#define TRACE ((void)0)
#endif
#define RELEASE(a) if(a) { a->Release(); a = NULL; }
inline void Trace(const char *pszFormat, ...)
{
char szBuffer[8192];
va_list va;
va_start(va, pszFormat);
vsprintf(szBuffer, pszFormat, va);
va_end(va);
strcat(szBuffer, "\n");
OutputDebugString(szBuffer);
}
// Window and control wrapper classes
class CRect : public RECT
{
public:
CRect() {}
CRect(int x1, int y1, int x2, int y2) { left = x1; top = y1; right = x2; bottom = y2; }
CRect(POINT tl, POINT br) { left = tl.x; top = tl.y; right = br.x; bottom = br.y; }
int Width() { return right - left; }
int Height() { return bottom - top; }
void SetRect(int x1, int y1, int x2, int y2) { left = x1; top = y1; right = x2; bottom = y2; }
void SetRect(POINT tl, POINT br) { left = tl.x; top = tl.y; right = br.x; bottom = br.y; }
void SetRectEmpty() { left = top = right = bottom = 0; }
};
class CWnd
{
public:
HWND m_hWnd;
CWnd(HWND hWnd=NULL) { m_hWnd = hWnd; }
virtual ~CWnd() {}
operator HWND() { return m_hWnd; }
bool operator==(HWND hWnd) { return hWnd == m_hWnd; }
bool operator!=(HWND hWnd) { return hWnd != m_hWnd; }
void operator=(HWND hWnd) { m_hWnd = hWnd; }
BOOL CreateEx(HINSTANCE hInstance, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwExStyle, DWORD dwStyle, LPCRECT pRect, HWND hParent=NULL, HMENU hMenu=NULL, LPVOID lpParam=NULL)
{
m_hWnd = ::CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, pRect->left, pRect->top, pRect->right-pRect->left, pRect->bottom-pRect->top, hParent, hMenu, hInstance, lpParam);
return (m_hWnd != NULL);
}
BOOL DestroyWindow() { return ::DestroyWindow(m_hWnd); }
long SetWindowLong(int nIndex, long nValue) { return ::SetWindowLong(m_hWnd, nIndex, nValue); }
long GetWindowLong(int nIndex) { return ::GetWindowLong(m_hWnd, nIndex); }
DWORD GetStyle() { return (DWORD)::GetWindowLong(m_hWnd, GWL_STYLE); }
void SetStyle(DWORD dw) { ::SetWindowLong(m_hWnd, GWL_STYLE, dw); }
DWORD GetExStyle() { return (DWORD)::GetWindowLong(m_hWnd, GWL_EXSTYLE); }
void SetExStyle(DWORD dw) { ::SetWindowLong(m_hWnd, GWL_EXSTYLE, dw); }
LRESULT SendMessage(UINT n, WPARAM w=0, LPARAM l=0) { return ::SendMessage(m_hWnd, n, w, l); }
BOOL PostMessage(UINT n, WPARAM w=0, LPARAM l=0){ return ::PostMessage(m_hWnd, n, w, l); }
void SetWindowText(LPCTSTR psz) { ::SetWindowText(m_hWnd, psz); }
int GetWindowText(LPTSTR psz, int n) { return ::GetWindowText(m_hWnd, psz, n); }
int GetWindowTextLength() { return ::GetWindowTextLength(m_hWnd); }
void UpdateWindow() { ::UpdateWindow(m_hWnd); }
BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); }
BOOL EnableWindow(BOOL bEnable) { return ::EnableWindow(m_hWnd, bEnable); }
BOOL IsWindowEnabled() { return ::IsWindowEnabled(m_hWnd); }
BOOL IsWindowVisible() { return ::IsWindowVisible(m_hWnd); }
BOOL IsIconic() { return ::IsIconic(m_hWnd); }
BOOL IsZoomed() { return ::IsZoomed(m_hWnd); }
void MoveWindow(LPCRECT pRect, BOOL b=TRUE) { MoveWindow(pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top, b); }
void MoveWindow(int x, int y, int w, int h, BOOL b=TRUE) { ::MoveWindow(m_hWnd, x, y, w, h, b); }
BOOL SetWindowPos(HWND hWnd, LPCRECT pRect, UINT n=0) { return ::SetWindowPos(m_hWnd, hWnd, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top, n); }
BOOL SetWindowPos(HWND hWnd, int x, int y, int w, int h, UINT n=0) { return ::SetWindowPos(m_hWnd, hWnd, x, y, w, h, n); }
void BringWindowToTop() { ::BringWindowToTop(m_hWnd); }
void GetWindowRect(LPRECT pRect) { ::GetWindowRect(m_hWnd, pRect); }
void GetClientRect(LPRECT pRect) { ::GetClientRect(m_hWnd, pRect); }
void ClientToScreen(LPPOINT pPoint) { ::ClientToScreen(m_hWnd, pPoint); }
void ClientToScreen(LPRECT pRect) { ::ClientToScreen(m_hWnd, (LPPOINT)pRect); ::ClientToScreen(m_hWnd, ((LPPOINT)pRect)+1); }
void ScreenToClient(LPPOINT pPoint) { ::ScreenToClient(m_hWnd, pPoint); }
void ScreenToClient(LPRECT pRect) { ::ScreenToClient(m_hWnd, (LPPOINT)pRect); ::ScreenToClient(m_hWnd, ((LPPOINT)pRect)+1); }
void CalcWindowRect(LPRECT pRect, UINT n=0) { ::AdjustWindowRect(pRect, GetStyle(), FALSE); }
HWND GetActiveWindow() { return ::GetActiveWindow(); }
HWND SetActiveWindow() { return ::SetActiveWindow(m_hWnd); }
HWND GetCapture() { return ::GetCapture(); }
HWND SetCapture() { return ::SetCapture(m_hWnd); }
HWND GetFocus() { return ::GetFocus(); }
HWND SetFocus() { return ::SetFocus(m_hWnd); }
HWND GetParent() { return ::GetParent(m_hWnd); }
HWND SetParent(HWND hWnd) { return ::SetParent(m_hWnd, hWnd); }
HICON SetIcon(HICON h, BOOL b) { return (HICON)::SendMessage(m_hWnd, WM_SETICON, b, (LPARAM)h); }
HICON GetIcon(BOOL b) { return (HICON)::SendMessage(m_hWnd, WM_GETICON, b, 0); }
HMENU GetMenu() { return ::GetMenu(m_hWnd); }
BOOL SetMenu(HMENU hMenu) { return ::SetMenu(m_hWnd, hMenu); }
void DrawMenuBar() { ::DrawMenuBar(m_hWnd); }
void RedrawWindow(LPCRECT pRect, UINT n) { ::RedrawWindow(m_hWnd, pRect, NULL, n); }
int GetDlgCtrlID() { return ::GetDlgCtrlID(m_hWnd); }
void SetRedraw(BOOL b) { ::SendMessage(m_hWnd, WM_SETREDRAW, b, 0); }
void Invalidate(BOOL b=TRUE, LPCRECT pRect=NULL){ ::InvalidateRect(m_hWnd, pRect, b); }
void Validate(LPCRECT pRect=NULL) { ::ValidateRect(m_hWnd, pRect); }
UINT SetTimer(UINT nID, UINT nElapse) { return ::SetTimer(m_hWnd, nID, nElapse, NULL); }
BOOL KillTimer(int nID) { return ::KillTimer(m_hWnd, nID); }
};
class CDialog : public CWnd
{
protected:
int m_nID;
HINSTANCE m_hInstance;
HWND m_hWndParent;
public:
CDialog(int nID=0, HINSTANCE hInstance=NULL, HWND hWndParent=NULL)
{
m_nID = nID;
m_hInstance = hInstance;
m_hWndParent = hWndParent;
}
static BOOL CALLBACK DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CDialog *pDlg;
switch(uMsg)
{
case WM_INITDIALOG:
{
pDlg = (CDialog *)lParam;
::SetWindowLong(hWnd, GWL_USERDATA, (long)pDlg);
pDlg->m_hWnd = hWnd;
pDlg->CenterDialog();
return pDlg->OnInitDialog(hWnd);
}
case WM_COMMAND:
{
pDlg = (CDialog *)::GetWindowLong(hWnd, GWL_USERDATA);
if(wParam == IDOK)
return pDlg->OnOK();
if(wParam == IDCANCEL)
return pDlg->OnCancel();
return pDlg->OnCommand(wParam, lParam);
}
}
return FALSE;
}
int DoModal() { return ::DialogBoxParam(m_hInstance, MAKEINTRESOURCE(m_nID), m_hWndParent, DlgProc, (LPARAM)this); }
virtual bool OnInitDialog(HWND hWnd) { return true; }
virtual bool OnOK() { ::EndDialog(m_hWnd, IDOK); return false; }
virtual bool OnCancel() { ::EndDialog(m_hWnd, IDCANCEL); return false; }
virtual bool OnCommand(WPARAM w, LPARAM l) { return false; }
void CenterDialog()
{
RECT rect1, rect2;
GetWindowRect(&rect1);
CWnd(m_hWndParent ? m_hWndParent : GetDesktopWindow()).GetWindowRect(&rect2);
rect1.left = rect2.left + ((rect2.right-rect2.left)-(rect1.right-rect1.left))/2;
rect1.top = rect2.top + ((rect2.bottom-rect2.top)-(rect1.bottom-rect1.top))/2;
SetWindowPos(NULL, &rect1, SWP_NOSIZE | SWP_NOZORDER);
}
};
class CStatic : public CWnd
{
CStatic(HWND hWndParent, int nComboID)
{
m_hWnd = ::GetDlgItem(hWndParent, nComboID);
ASSERT(::IsWindow(m_hWnd));
}
HICON SetIcon(HICON hIcon) { return (HICON)::SendMessage(m_hWnd, STM_SETICON, (WPARAM)hIcon, 0L); }
HICON GetIcon() const { return (HICON)::SendMessage(m_hWnd, STM_GETICON, 0, 0L); }
HENHMETAFILE SetEnhMetaFile(HENHMETAFILE h) { return (HENHMETAFILE)::SendMessage(m_hWnd, STM_SETIMAGE, IMAGE_ENHMETAFILE, (LPARAM)h); }
HENHMETAFILE GetEnhMetaFile() const { return (HENHMETAFILE)::SendMessage(m_hWnd, STM_GETIMAGE, IMAGE_ENHMETAFILE, 0L); }
HBITMAP SetBitmap(HBITMAP hBitmap) { return (HBITMAP)::SendMessage(m_hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap); }
HBITMAP GetBitmap() const { return (HBITMAP)::SendMessage(m_hWnd, STM_GETIMAGE, IMAGE_BITMAP, 0L); }
HCURSOR SetCursor(HCURSOR hCursor) { return (HCURSOR)::SendMessage(m_hWnd, STM_SETIMAGE, IMAGE_CURSOR, (LPARAM)hCursor); }
HCURSOR GetCursor() { return (HCURSOR)::SendMessage(m_hWnd, STM_GETIMAGE, IMAGE_CURSOR, 0L); }
};
class CEdit : public CWnd
{
public:
CEdit(HWND hWndParent, int nComboID)
{
m_hWnd = ::GetDlgItem(hWndParent, nComboID);
ASSERT(::IsWindow(m_hWnd));
}
void GetSel(int& nStartChar, int& nEndChar) { ::SendMessage(m_hWnd, EM_GETSEL, (WPARAM)&nStartChar,(LPARAM)&nEndChar); }
DWORD GetSel() { return ::SendMessage(m_hWnd, EM_GETSEL, 0, 0); }
void SetSel(DWORD dwSelection) { ::SendMessage(m_hWnd, EM_SETSEL, LOWORD(dwSelection), HIWORD(dwSelection)); }
void SetSel(int nStartChar, int nEndChar) { ::SendMessage(m_hWnd, EM_SETSEL, nStartChar, nEndChar); }
void SetLimitText(UINT nMax) { ::SendMessage(m_hWnd, EM_SETLIMITTEXT, nMax, 0); }
void Clear() { ::SendMessage(m_hWnd, WM_CLEAR, 0, 0); }
void Copy() { ::SendMessage(m_hWnd, WM_COPY, 0, 0); }
void Cut() { ::SendMessage(m_hWnd, WM_CUT, 0, 0); }
void Paste() { ::SendMessage(m_hWnd, WM_PASTE, 0, 0); }
BOOL SetReadOnly(BOOL bReadOnly) { return (BOOL)::SendMessage(m_hWnd, EM_SETREADONLY, bReadOnly, 0L); }
};
class CButton : public CWnd
{
public:
CButton(HWND hWndParent, int nComboID)
{
m_hWnd = ::GetDlgItem(hWndParent, nComboID);
ASSERT(::IsWindow(m_hWnd));
}
UINT GetState() { return (UINT)::SendMessage(m_hWnd, BM_GETSTATE, 0, 0); }
void SetState(BOOL b) { ::SendMessage(m_hWnd, BM_SETSTATE, b, 0); }
int GetCheck() { return (int)::SendMessage(m_hWnd, BM_GETCHECK, 0, 0); }
void SetCheck(int n) { ::SendMessage(m_hWnd, BM_SETCHECK, n, 0); }
UINT GetButtonStyle() { return (UINT)::GetWindowLong(m_hWnd, GWL_STYLE) & 0xff; }
void SetButtonStyle(UINT n, BOOL b) { ::SendMessage(m_hWnd, BM_SETSTYLE, n, (LPARAM)b); }
};
class CComboBox : public CWnd
{
public:
CComboBox(HWND hWndParent, int nComboID)
{
m_hWnd = ::GetDlgItem(hWndParent, nComboID);
ASSERT(::IsWindow(m_hWnd));
}
int GetCount() { return (int)::SendMessage(m_hWnd, CB_GETCOUNT, 0, 0); }
int GetCurSel() { return (int)::SendMessage(m_hWnd, CB_GETCURSEL, 0, 0); }
int SetCurSel(int n) { return (int)::SendMessage(m_hWnd, CB_SETCURSEL, n, 0); }
DWORD GetEditSel() { return (DWORD)::SendMessage(m_hWnd, CB_GETEDITSEL, 0, 0); }
BOOL LimitText(int n) { return (BOOL)::SendMessage(m_hWnd, CB_LIMITTEXT, n, 0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -