📄 platwin.cxx
字号:
return TRUE;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hDC = ::BeginPaint(hWnd, &ps);
ListBoxX *lbx = reinterpret_cast<ListBoxX *>(PointerFromWindow(::GetParent(hWnd)));
if (lbx)
lbx->Paint(hDC);
::EndPaint(hWnd, &ps);
}
return 0;
case WM_MOUSEACTIVATE:
// This prevents the view activating when the scrollbar is clicked
return MA_NOACTIVATE;
case WM_LBUTTONDOWN: {
// We must take control of selection to prevent the ListBox activating
// the popup
LRESULT lResult = ::SendMessage(hWnd, LB_ITEMFROMPOINT, 0, lParam);
int item = LOWORD(lResult);
if (HIWORD(lResult) == 0 && item >= 0) {
::SendMessage(hWnd, LB_SETCURSEL, item, 0);
}
}
return 0;
case WM_LBUTTONUP:
return 0;
case WM_LBUTTONDBLCLK: {
ListBoxX *lbx = reinterpret_cast<ListBoxX *>(PointerFromWindow(::GetParent(hWnd)));
if (lbx) {
lbx->OnDoubleClick();
}
}
return 0;
}
WNDPROC prevWndProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
if (prevWndProc) {
return ::CallWindowProc(prevWndProc, hWnd, uMsg, wParam, lParam);
} else {
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
LRESULT ListBoxX::WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) {
switch (iMessage) {
case WM_CREATE: {
HINSTANCE hinstanceParent = GetWindowInstance(reinterpret_cast<HWND>(parent->GetID()));
// Note that LBS_NOINTEGRALHEIGHT is specified to fix cosmetic issue when resizing the list
// but has useful side effect of speeding up list population significantly
lb = ::CreateWindowEx(
0, TEXT("listbox"), TEXT(""),
WS_CHILD | WS_VSCROLL | WS_VISIBLE |
LBS_OWNERDRAWFIXED | LBS_NODATA | LBS_NOINTEGRALHEIGHT,
0, 0, 150,80, hWnd,
reinterpret_cast<HMENU>(ctrlID),
hinstanceParent,
0);
WNDPROC prevWndProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(lb, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(ControlWndProc)));
::SetWindowLongPtr(lb, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(prevWndProc));
}
break;
case WM_SIZE:
if (lb) {
SetRedraw(false);
::SetWindowPos(lb, 0, 0,0, LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE);
// Ensure the selection remains visible
CentreItem(GetSelection());
SetRedraw(true);
}
break;
case WM_PAINT: {
PAINTSTRUCT ps;
::BeginPaint(hWnd, &ps);
::EndPaint(hWnd, &ps);
}
break;
case WM_COMMAND:
// This is not actually needed now - the registered double click action is used
// directly to action a choice from the list.
::SendMessage(reinterpret_cast<HWND>(parent->GetID()), iMessage, wParam, lParam);
break;
case WM_MEASUREITEM: {
MEASUREITEMSTRUCT *pMeasureItem = reinterpret_cast<MEASUREITEMSTRUCT *>(lParam);
pMeasureItem->itemHeight = static_cast<unsigned int>(ItemHeight());
}
break;
case WM_DRAWITEM:
Draw(reinterpret_cast<DRAWITEMSTRUCT *>(lParam));
break;
case WM_DESTROY:
lb = 0;
::SetWindowLong(hWnd, 0, 0);
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
case WM_ERASEBKGND:
// To reduce flicker we can elide background erasure since this window is
// completely covered by its child.
return TRUE;
case WM_GETMINMAXINFO: {
MINMAXINFO *minMax = reinterpret_cast<MINMAXINFO*>(lParam);
*reinterpret_cast<Point*>(&minMax->ptMaxTrackSize) = MaxTrackSize();
*reinterpret_cast<Point*>(&minMax->ptMinTrackSize) = MinTrackSize();
}
break;
case WM_MOUSEACTIVATE:
return MA_NOACTIVATE;
case WM_NCHITTEST:
return NcHitTest(wParam, lParam);
case WM_NCLBUTTONDOWN:
// We have to implement our own window resizing because the DefWindowProc
// implementation insists on activating the resized window
StartResize(wParam);
return 0;
case WM_MOUSEMOVE: {
if (resizeHit == 0) {
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
} else {
ResizeToCursor();
}
}
break;
case WM_LBUTTONUP:
case WM_CANCELMODE:
if (resizeHit != 0) {
resizeHit = 0;
::ReleaseCapture();
}
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
default:
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
}
return 0;
}
LRESULT PASCAL ListBoxX::StaticWndProc(
HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) {
if (iMessage == WM_CREATE) {
CREATESTRUCT *pCreate = reinterpret_cast<CREATESTRUCT *>(lParam);
SetWindowPointer(hWnd, pCreate->lpCreateParams);
}
// Find C++ object associated with window.
ListBoxX *lbx = reinterpret_cast<ListBoxX *>(PointerFromWindow(hWnd));
if (lbx) {
return lbx->WndProc(hWnd, iMessage, wParam, lParam);
} else {
return ::DefWindowProc(hWnd, iMessage, wParam, lParam);
}
}
static bool ListBoxX_Register() {
WNDCLASSEX wndclassc;
wndclassc.cbSize = sizeof(wndclassc);
// We need CS_HREDRAW and CS_VREDRAW because of the ellipsis that might be drawn for
// truncated items in the list and the appearance/disappearance of the vertical scroll bar.
// The list repaint is double-buffered to avoid the flicker this would otherwise cause.
wndclassc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
wndclassc.cbClsExtra = 0;
wndclassc.cbWndExtra = sizeof(ListBoxX *);
wndclassc.hInstance = hinstPlatformRes;
wndclassc.hIcon = NULL;
wndclassc.hbrBackground = NULL;
wndclassc.lpszMenuName = NULL;
wndclassc.lpfnWndProc = ListBoxX::StaticWndProc;
wndclassc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wndclassc.lpszClassName = ListBoxX_ClassName;
wndclassc.hIconSm = 0;
return ::RegisterClassEx(&wndclassc) != 0;
}
bool ListBoxX_Unregister() {
return ::UnregisterClass(ListBoxX_ClassName, hinstPlatformRes) != 0;
}
Menu::Menu() : id(0) {
}
void Menu::CreatePopUp() {
Destroy();
id = ::CreatePopupMenu();
}
void Menu::Destroy() {
if (id)
::DestroyMenu(reinterpret_cast<HMENU>(id));
id = 0;
}
void Menu::Show(Point pt, Window &w) {
::TrackPopupMenu(reinterpret_cast<HMENU>(id),
0, pt.x - 4, pt.y, 0,
reinterpret_cast<HWND>(w.GetID()), NULL);
Destroy();
}
static bool initialisedET = false;
static bool usePerformanceCounter = false;
static LARGE_INTEGER frequency;
ElapsedTime::ElapsedTime() {
if (!initialisedET) {
usePerformanceCounter = ::QueryPerformanceFrequency(&frequency) != 0;
initialisedET = true;
}
if (usePerformanceCounter) {
LARGE_INTEGER timeVal;
::QueryPerformanceCounter(&timeVal);
bigBit = timeVal.HighPart;
littleBit = timeVal.LowPart;
} else {
bigBit = clock();
}
}
double ElapsedTime::Duration(bool reset) {
double result;
long endBigBit;
long endLittleBit;
if (usePerformanceCounter) {
LARGE_INTEGER lEnd;
::QueryPerformanceCounter(&lEnd);
endBigBit = lEnd.HighPart;
endLittleBit = lEnd.LowPart;
LARGE_INTEGER lBegin;
lBegin.HighPart = bigBit;
lBegin.LowPart = littleBit;
double elapsed = lEnd.QuadPart - lBegin.QuadPart;
result = elapsed / static_cast<double>(frequency.QuadPart);
} else {
endBigBit = clock();
endLittleBit = 0;
double elapsed = endBigBit - bigBit;
result = elapsed / CLOCKS_PER_SEC;
}
if (reset) {
bigBit = endBigBit;
littleBit = endLittleBit;
}
return result;
}
class DynamicLibraryImpl : public DynamicLibrary {
protected:
HMODULE h;
public:
DynamicLibraryImpl(const char *modulePath) {
h = ::LoadLibrary(modulePath);
}
virtual ~DynamicLibraryImpl() {
if (h != NULL)
::FreeLibrary(h);
}
// Use GetProcAddress to get a pointer to the relevant function.
virtual Function FindFunction(const char *name) {
if (h != NULL) {
return static_cast<Function>(
(void *)(::GetProcAddress(h, name)));
} else
return NULL;
}
virtual bool IsValid() {
return h != NULL;
}
};
DynamicLibrary *DynamicLibrary::Load(const char *modulePath) {
return static_cast<DynamicLibrary *>( new DynamicLibraryImpl(modulePath) );
}
ColourDesired Platform::Chrome() {
return ::GetSysColor(COLOR_3DFACE);
}
ColourDesired Platform::ChromeHighlight() {
return ::GetSysColor(COLOR_3DHIGHLIGHT);
}
const char *Platform::DefaultFont() {
return "Verdana";
}
int Platform::DefaultFontSize() {
return 8;
}
unsigned int Platform::DoubleClickTime() {
return ::GetDoubleClickTime();
}
bool Platform::MouseButtonBounce() {
return false;
}
void Platform::DebugDisplay(const char *s) {
::OutputDebugString(s);
}
bool Platform::IsKeyDown(int key) {
return (::GetKeyState(key) & 0x80000000) != 0;
}
long Platform::SendScintilla(WindowID w, unsigned int msg, unsigned long wParam, long lParam) {
return ::SendMessage(reinterpret_cast<HWND>(w), msg, wParam, lParam);
}
long Platform::SendScintillaPointer(WindowID w, unsigned int msg, unsigned long wParam, void *lParam) {
return ::SendMessage(reinterpret_cast<HWND>(w), msg, wParam,
reinterpret_cast<LPARAM>(lParam));
}
bool Platform::IsDBCSLeadByte(int codePage, char ch) {
return ::IsDBCSLeadByteEx(codePage, ch) != 0;
}
int Platform::DBCSCharLength(int codePage, const char *s) {
return (::IsDBCSLeadByteEx(codePage, s[0]) != 0) ? 2 : 1;
}
int Platform::DBCSCharMaxLength() {
return 2;
}
// These are utility functions not really tied to a platform
int Platform::Minimum(int a, int b) {
if (a < b)
return a;
else
return b;
}
int Platform::Maximum(int a, int b) {
if (a > b)
return a;
else
return b;
}
//#define TRACE
#ifdef TRACE
void Platform::DebugPrintf(const char *format, ...) {
char buffer[2000];
va_list pArguments;
va_start(pArguments, format);
vsprintf(buffer,format,pArguments);
va_end(pArguments);
Platform::DebugDisplay(buffer);
}
#else
void Platform::DebugPrintf(const char *, ...) {
}
#endif
static bool assertionPopUps = true;
bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
bool ret = assertionPopUps;
assertionPopUps = assertionPopUps_;
return ret;
}
void Platform::Assert(const char *c, const char *file, int line) {
char buffer[2000];
sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
if (assertionPopUps) {
int idButton = ::MessageBox(0, buffer, "Assertion failure",
MB_ABORTRETRYIGNORE|MB_ICONHAND|MB_SETFOREGROUND|MB_TASKMODAL);
if (idButton == IDRETRY) {
::DebugBreak();
} else if (idButton == IDIGNORE) {
// all OK
} else {
abort();
}
} else {
strcat(buffer, "\r\n");
Platform::DebugDisplay(buffer);
::DebugBreak();
abort();
}
}
int Platform::Clamp(int val, int minVal, int maxVal) {
if (val > maxVal)
val = maxVal;
if (val < minVal)
val = minVal;
return val;
}
void Platform_Initialise(void *hInstance) {
OSVERSIONINFO osv = {sizeof(OSVERSIONINFO),0,0,0,0,TEXT("")};
::GetVersionEx(&osv);
onNT = osv.dwPlatformId == VER_PLATFORM_WIN32_NT;
::InitializeCriticalSection(&crPlatformLock);
hinstPlatformRes = reinterpret_cast<HINSTANCE>(hInstance);
if (!hDLLImage) {
hDLLImage = ::LoadLibrary(TEXT("Msimg32"));
}
if (hDLLImage) {
AlphaBlendFn = (AlphaBlendSig)::GetProcAddress(hDLLImage, "AlphaBlend");
}
ListBoxX_Register();
}
void Platform_Finalise() {
ListBoxX_Unregister();
::DeleteCriticalSection(&crPlatformLock);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -