📄 win32window.cpp
字号:
if (m_bMindMeldInProgress)
return;
hDc = BeginPaint(m_hWnd, &ps);
oRect.x1 = ps.rcPaint.left;
oRect.y1 = ps.rcPaint.top;
oRect.x2 = ps.rcPaint.right;
oRect.y2 = ps.rcPaint.bottom;
((Win32Canvas *)(GetCanvas()))->Paint(hDc, oRect);
EndPaint(m_hWnd, &ps);
}
void Win32Window::TimerEvent(void)
{
if (!m_bTimerEnabled)
return;
if (m_bMindMeldInProgress)
return;
Window::TimerEvent();
}
void Win32Window::SaveWindowPos(Pos &oPos)
{
m_oWindowPos = oPos;
}
Error Win32Window::Close(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
SendMessage(m_hWnd, WM_CLOSE, 0, 0);
return kError_NoErr;
}
Error Win32Window::Enable(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
return EnableWindow(m_hWnd, false) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::Disable(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
return EnableWindow(m_hWnd, true) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::Show(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
return ShowWindow(m_hWnd, SW_SHOWNORMAL) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::Hide(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
return ShowWindow(m_hWnd, SW_HIDE) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::SetTitle(string &oTitle)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
return SetWindowText(m_hWnd, oTitle.c_str()) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::CaptureMouse(bool bCapture)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
if (bCapture)
{
SetCapture(m_hWnd);
return kError_NoErr;
}
else
return ReleaseCapture() ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::HideMouse(bool bHide)
{
return ShowCursor(!bHide) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::SetMousePos(Pos &oPos)
{
return SetCursorPos(oPos.x, oPos.y) ? kError_NoErr : kError_InvalidParam;
}
Error Win32Window::GetMousePos(Pos &oPos)
{
POINT oPoint;
BOOL bRet;
bRet = GetCursorPos(&oPoint);
oPos.x = oPoint.x;
oPos.y = oPoint.y;
return bRet ? kError_NoErr : kError_InvalidParam;
}
HWND Win32Window::GetWindowHandle(void)
{
return m_hWnd;
}
Error Win32Window::SetWindowPosition(Rect &oWindowRect)
{
if (!m_hWnd)
return kError_YouScrewedUp;
MoveWindow(m_hWnd, oWindowRect.x1, oWindowRect.y1,
oWindowRect.Width(), oWindowRect.Height(),
true);
return kError_NoErr;
}
Error Win32Window::GetWindowPosition(Rect &oWindowRect)
{
RECT sRect;
if (!m_hWnd)
return kError_YouScrewedUp;
GetWindowRect(m_hWnd, &sRect);
oWindowRect.x1 = sRect.left;
oWindowRect.x2 = sRect.right;
oWindowRect.y1 = sRect.top;
oWindowRect.y2 = sRect.bottom;
return kError_NoErr;
}
Error Win32Window::Minimize(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
ShowWindow(m_hWnd, SW_MINIMIZE);
if (m_bLiveInToolbar)
ShowWindow(m_hWnd, SW_HIDE);
return kError_NoErr;
}
Error Win32Window::Restore(void)
{
if (!m_hWnd || m_bMindMeldInProgress)
return kError_YouScrewedUp;
ShowWindow(m_hWnd, SW_RESTORE);
return kError_NoErr;
}
void Win32Window::DropFiles(HDROP dropHandle)
{
int32 count;
char file[MAX_PATH + 1];
vector<string> oFileList;
char* extension = NULL;
if (m_bMindMeldInProgress)
{
DragFinish(dropHandle);
return;
}
count = DragQueryFile( dropHandle,
-1L,
file,
sizeof(file));
for(int32 i = 0; i < count; i++)
{
DragQueryFile( dropHandle,
i,
file,
sizeof(file));
extension = strrchr(file, '.');
if(extension && strcasecmp(extension, ".lnk") == 0)
{
string link = string(file);
ResolveLink(link);
strcpy(file, link.c_str());
}
oFileList.push_back(string(file));
}
DragFinish(dropHandle);
m_pTheme->DropFiles(&oFileList);
}
void
Win32Window::
Notify(int32 command, LPNMHDR notifyMsgHdr)
{
vector<pair<Rect, string> > oList;
if (m_bMindMeldInProgress)
return;
if(notifyMsgHdr->code == TTN_NEEDTEXT)
{
int32 idCtrl = notifyMsgHdr->idFrom;
LPTOOLTIPTEXT lpttt = (LPTOOLTIPTEXT) notifyMsgHdr;
string strTip;
//
// and feed it's tip
//
GetControlToolTips(oList);
strTip = oList[idCtrl].second;
if(strTip.length())
{
if(strTip.length()>79)
{
// to avoid buffer overruns
lpttt->szText[79]=0;
strncpy(lpttt->szText,strTip.c_str(),78);
}
else
strcpy(lpttt->szText,strTip.c_str()); // if tip is there
}
}
}
void
Win32Window::
CreateTooltips()
{
vector<pair<Rect, string> > oList;
// tooltip support
static HWND hwndTooltip = NULL;
static uint32 uTooltipCount = 0;
HINSTANCE hinst = (HINSTANCE)GetWindowLong( m_hWnd,
GWL_HINSTANCE);
TOOLINFO ti;
uint32 uCtr;
//
// check if we have been here
//
if(hwndTooltip)
{
// remove old tooltips, then
for(uCtr=0; uCtr<uTooltipCount; uCtr++)
{
ti.cbSize = sizeof(TOOLINFO);
ti.hwnd = m_hWnd;
ti.uId = uCtr;
SendMessage(hwndTooltip,
TTM_DELTOOL,
0,
(LPARAM) &ti);
}
}
if(!hwndTooltip)
{
// for some reason if mindmeld, destroying and
// re-creating window does not work. we can
// create it only once.
hwndTooltip = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
m_hWnd,
(HMENU) NULL,
hinst,
NULL);
}
//
// now go adding tooltip regions
//
GetControlToolTips(oList);
for(uCtr = 0; uCtr < oList.size(); uCtr++)
{
Rect rect;
string strTip;
strTip = oList[uCtr].second;
rect = oList[uCtr].first;
if (strTip.length() == 0)
continue;
// add a tool tip
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = m_hWnd;
ti.hinst = hinst;
ti.uId = uCtr;
ti.lpszText = (LPSTR) LPSTR_TEXTCALLBACK;
ti.rect.left = rect.x1;
ti.rect.top = rect.y1;
ti.rect.right = rect.x2;
ti.rect.bottom = rect.y2;
SendMessage(hwndTooltip,
TTM_ADDTOOL,
0,
(LPARAM) &ti);
}
uTooltipCount = uCtr; // save value for next mindmeld
}
void Win32Window::SetStayOnTop(bool bStay)
{
Window::SetStayOnTop(bStay);
if (m_hWnd == NULL || m_bMindMeldInProgress)
return;
if (m_bStayOnTop)
SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
else
SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
}
void Win32Window::SetLiveInToolbar(bool bLive)
{
int iExtStyle, iStyle;
Window::SetLiveInToolbar(bLive);
if (m_hWnd == NULL || m_bMindMeldInProgress)
return;
ShowWindow(m_hWnd, FALSE);
iStyle = GetWindowLong(m_hWnd, GWL_STYLE);
iExtStyle = GetWindowLong(m_hWnd, GWL_EXSTYLE);
if (m_bLiveInToolbar)
iExtStyle |= WS_EX_TOOLWINDOW;
else
iExtStyle &= ~WS_EX_TOOLWINDOW;
SetWindowLong(m_hWnd, GWL_STYLE, WS_VISIBLE);
SetWindowLong(m_hWnd, GWL_STYLE, iStyle);
SetWindowLong(m_hWnd, GWL_EXSTYLE, iExtStyle);
ShowWindow(m_hWnd, TRUE);
}
bool Win32Window::LButtonDown(void)
{
if (m_bMindMeldInProgress)
return false;
return GetAsyncKeyState(VK_LBUTTON) < 0;
}
void Win32Window::MouseLeaveCheck(void)
{
Rect oRect;
POINT sPos;
Pos oPos;
HDC hDc;
if (m_bMindMeldInProgress)
return;
GetWindowPosition(oRect);
GetCursorPos(&sPos);
oPos.x = sPos.x;
oPos.y = sPos.y;
ScreenToClient(m_hWnd, &sPos);
hDc = GetDC(m_hWnd);
if (!oRect.IsPosInRect(oPos) || !PtVisible(hDc, sPos.x, sPos.y))
{
if (m_bMouseInWindow)
MouseHasLeftWindow();
m_bMouseInWindow = false;
}
else
{
if (!m_bMouseInWindow)
MouseHasEnteredWindow();
m_bMouseInWindow = true;
}
ReleaseDC(m_hWnd, hDc);
}
Error Win32Window::GetDesktopSize(int32 &iX, int32 &iY)
{
RECT sRect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &sRect, 0);
iX = sRect.right;
iY = sRect.bottom;
return kError_NoErr;
}
void Win32Window::BringWindowToFront(void)
{
if (m_bMindMeldInProgress)
return;
ShowWindow(m_hWnd, SW_RESTORE);
SetForegroundWindow(m_hWnd);
}
void Win32Window::AddToSystemMenu(HWND hWnd)
{
HMENU hMenu, hPopup, hMain;
MENUITEMINFO sInfo;
BOOL bRet;
if (m_bMindMeldInProgress)
return;
hMenu = GetSystemMenu(hWnd, false);
hMain = LoadMenu(g_hinst, MAKEINTRESOURCE(IDM_TRAY));
hPopup = GetSubMenu(hMain, 0);
sInfo.cbSize = sizeof(sInfo);
sInfo.fMask = MIIM_SUBMENU | MIIM_TYPE;
sInfo.hSubMenu = hPopup;
sInfo.fType = MFT_STRING;
sInfo.dwTypeData = BRANDING;
sInfo.cch = strlen(BRANDING);
sInfo.fState = 0;
bRet = InsertMenuItem(hMenu, 0, true, &sInfo);
sInfo.cbSize = sizeof(sInfo);
sInfo.fMask = MIIM_TYPE;
sInfo.fType = MFT_SEPARATOR;
sInfo.fState = 0;
bRet = InsertMenuItem(hMenu, 1, true, &sInfo);
DestroyMenu(hMain);
}
void Win32Window::ConvertTo256Color(vector<Bitmap *> *pList)
{
vector<Bitmap *>::iterator i;
Win32Bitmap *pBitmap;
WORD *pHist;
BYTE pColorMap[iNumColorsInPalette][3];
RGBQUAD pWinColorMap[iNumColorsTotal];
HDC hDC;
hDC = GetDC(NULL);
if (!(GetDeviceCaps(hDC, RASTERCAPS) & RC_PALETTE))
{
ReleaseDC(NULL, hDC);
return;
}
ReleaseDC(NULL, hDC);
pHist = new WORD[iNumColorsInHist];
memset(pHist, 0, sizeof(WORD) * iNumColorsInHist);
memset(pColorMap, 0, sizeof(BYTE) * 3 * iNumColorsInPalette);
for(i = pList->begin(); i != pList->end(); i++)
{
pBitmap = (Win32Bitmap *)(*i);
pBitmap->UpdateHistogram(pHist);
}
MedianCut(pHist, pColorMap, iNumColorsInPalette);
Create256ColorPalette(pColorMap, pWinColorMap);
for(i = pList->begin(); i != pList->end(); i++)
{
pBitmap = (Win32Bitmap *)(*i);
pBitmap->SetPalette(m_hPal);
pBitmap->ConvertTo256Color(pHist, pWinColorMap);
}
delete pHist;
}
void Win32Window::Create256ColorPalette(BYTE pColorMap[236][3],
RGBQUAD pWinColorMap[256])
{
HDC hRootDC;
LOGPALETTE *pLog;
int i;
pLog = (LOGPALETTE *)new unsigned char[sizeof(LOGPALETTE) +
sizeof(PALETTEENTRY) * 256];
pLog->palVersion = 0x300;
pLog->palNumEntries = 256;
hRootDC = GetDC(NULL);
GetSystemPaletteEntries(hRootDC, 0, 10, pLog->palPalEntry);
GetSystemPaletteEntries(hRootDC, 245, 10, &pLog->palPalEntry[245]);
ReleaseDC(NULL, hRootDC);
for(i = 0; i < 236; i++)
{
pLog->palPalEntry[i + 10].peRed = pColorMap[i][0];
pLog->palPalEntry[i + 10].peGreen = pColorMap[i][1];
pLog->palPalEntry[i + 10].peBlue = pColorMap[i][2];
pLog->palPalEntry[i + 10].peFlags = PC_NOCOLLAPSE;
}
if (m_hPal)
DeleteObject(m_hPal);
m_hPal = CreatePalette(pLog);
for(i = 0; i < 256; i++, pWinColorMap++)
{
pWinColorMap->rgbRed = pLog->palPalEntry[i].peRed;
pWinColorMap->rgbGreen = pLog->palPalEntry[i].peGreen;
pWinColorMap->rgbBlue = pLog->palPalEntry[i].peBlue;
}
delete pLog;
}
void Win32Window::PanelStateChanged(void)
{
Rect oRect, oWindowRect;
RECT sRect;
HRGN hRgn;
GetCanvas()->SetNoScreenUpdate(true);
Window::PanelStateChanged();
GetCanvas()->GetBackgroundRect(oRect);
hRgn = ((Win32Canvas *)m_pCanvas)->GetMaskRgn();
if (hRgn)
SetWindowRgn(m_hWnd, hRgn, true);
GetWindowPosition(oWindowRect);
m_pCanvas->GetBackgroundRect(oRect);
oWindowRect.x2 = oWindowRect.x1 + oRect.Width();
oWindowRect.y2 = oWindowRect.y1 + oRect.Height();
SetWindowPosition(oWindowRect);
sRect.left = oRect.x1;
sRect.right = oRect.x2;
sRect.top = oRect.y1;
sRect.bottom = oRect.y2;
InvalidateRect(m_hWnd, &sRect, true);
GetCanvas()->SetNoScreenUpdate(false);
UpdateWindow(m_hWnd);
CreateTooltips();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -