📄 notedlg.cpp
字号:
SendMessage(WM_SYSCOMMAND, SC_SIZE|nHitCode);
}
else if (nHitCode == HT_BAR)
{
// Move the dialog
SendMessage(WM_SYSCOMMAND, SC_MOVE|0x0002);
}
return 0;
}
LRESULT CNoteDlg::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CNoteDlg::OnSize()\n"));
int xPos = (int)LOWORD(lParam); // new width of the client area
int yPos = (int)HIWORD(lParam); // new height of the client area
// Move the static control which holds the 'Close' icon
CRect rectClose(xPos - 17, 1, xPos - 1 , 17);
m_wndStaticClose.MoveWindow(&rectClose, false);
// Move the static control which displays the date and time
m_wndStaticDateTime.MoveWindow(5, yPos - 14, xPos / 2, 14, false);
// Move the static bar control
m_wndStaticBar.MoveWindow(20, 2, xPos - 41, 16, false);
// Move the edit control
m_wndEdit.MoveWindow(5, 20, xPos - 10, yPos - 40, false);
// Invalidate the dialog's client area
Invalidate();
return 0;
}
LRESULT CNoteDlg::OnSizing(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CNoteDlg::OnSizing()\n"));
// The lParam contains the window's position
CRect * prc = (CRect*)lParam;
// Limit the size of the dialog to some minimum to avoid being able to resize
// the dialog to a line or a dot.
CRect rectClose;
m_wndStaticClose.GetWindowRect(&rectClose);
int nWidthMin = rectClose.Width() * 2;
int nHeightMin = rectClose.Width() * 2;
if (prc->Width() < nWidthMin || prc->Height() < nHeightMin)
{
// Adjust the rectangle's dimensions
// wParam specifies which edge of the dialog is being sized
switch (wParam)
{
case WMSZ_LEFT:
prc->left = prc->right - nWidthMin;
break;
case WMSZ_TOP:
prc->top = prc->bottom - nHeightMin;
break;
case WMSZ_RIGHT:
prc->right = prc->left + nWidthMin;
break;
case WMSZ_BOTTOM:
prc->bottom = prc->top + nHeightMin;
break;
case WMSZ_BOTTOMLEFT:
if (prc->Height() < nHeightMin)
prc->bottom = prc->top + nHeightMin;
if (prc->Width() < nWidthMin)
prc->left = prc->right - nWidthMin;
break;
case WMSZ_BOTTOMRIGHT:
if (prc->Height() < nHeightMin)
prc->bottom = prc->top + nHeightMin;
if (prc->Width() < nWidthMin)
prc->right = prc->left + nWidthMin;
break;
case WMSZ_TOPLEFT:
if (prc->Height() < nHeightMin)
prc->top = prc->bottom - nHeightMin;
if (prc->Width() < nWidthMin)
prc->left = prc->right - nWidthMin;
break;
case WMSZ_TOPRIGHT:
if (prc->Height() < nHeightMin)
prc->top = prc->bottom - nHeightMin;
if (prc->Width() < nWidthMin)
prc->right = prc->left + nWidthMin;
break;
default:
break;
}
}
return TRUE;
}
// Handles clicking on 'New Note' menu item.
void CNoteDlg::OnNewNote()
{
ATLTRACE(_T("CNoteDlg::OnNewNote()\n"));
// Send a message to the parent to create a new Note dialog
::SendMessage(m_pWndParent->m_hWnd, WM_COMMAND, (WPARAM)ID_POPUP_NEWNOTE, 0);
}
// Handles clicking on 'Always on top' menu item.
// If the menu item is not checked then place the dialog above all windows,
// if it's checked then do the opposite.
// Also reset the flag.
void CNoteDlg::OnAlwaysOnTopState()
{
ATLTRACE(_T("CNoteDlg::OnAlwaysOnTopState()\n"));
if (m_wndStaticNote.GetAlwaysOnTopState())
{
SetWindowPos(HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
m_wndStaticNote.SetAlwaysOnTopState(false);
}
else
{
SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
m_wndStaticNote.SetAlwaysOnTopState(true);
}
}
// Handles clicking on different colors on the menu
void CNoteDlg::OnChangeBkgndColor(COLORREF clrColor)
{
ATLTRACE(_T("CNoteDlg::ChangeBkgndColor()\n"));
ATLASSERT(m_brBkgnd.m_hBrush);
// Change the dialog's background color
m_clrDlg = clrColor;
m_brBkgnd.DeleteObject();
m_brBkgnd.CreateSolidBrush(m_clrDlg);
Invalidate();
// Change the background color of the control displaying date and time
m_wndStaticDateTime.ChangeBkgndColor(clrColor);
// Change the background color of the edit control
m_wndEdit.ChangeBkgndColor(clrColor);
// Change the background color of the 'Note' bitmap
HBITMAP hBmpOldNote = m_wndStaticNote.GetBitmap();
m_hBmpNote = NULL;
m_hBmpNote = ReplaceColor(hBmpOldNote, m_Note.m_clrBkgnd, clrColor);
// Change the background color of the 'Close' bitmap
HBITMAP hBmpOldClose = m_wndStaticClose.GetBitmap();
m_hBmpClose = NULL;
m_hBmpClose = ReplaceColor(hBmpOldClose, m_Note.m_clrBkgnd, clrColor);
// Draw the new 'Note' bitmap in the static control
m_wndStaticNote.SetBitmap(m_hBmpNote);
// Draw the new 'Close' bitmap in the static control
m_wndStaticClose.SetBitmap(m_hBmpClose);
// Delete the old bitmaps
DeleteObject(hBmpOldNote);
DeleteObject(hBmpOldClose);
// Reflect this change back to CNote object
// (change it's background color attribute)
m_Note.m_clrBkgnd = clrColor;
}
// Handles clicking on 'Set font' menu item
void CNoteDlg::OnSetFont()
{
ATLTRACE(_T("CNoteDlg::OnSetFont()\n"));
LOGFONT lf;
// Change font in the edit control
m_wndEdit.ChangeFont(&lf);
// Reflect this change back to CNote object
m_Note.m_strFontName = lf.lfFaceName;
m_Note.m_lFontHeight = lf.lfHeight;
m_Note.m_lWeight = lf.lfWeight;
m_Note.m_bItalic = lf.lfItalic;
}
// Handles clicking on 'Save' menu item
void CNoteDlg::OnSave()
{
ATLTRACE(_T("CNoteDlg::OnSave()\n"));
vector<CNote>::iterator iter;
bool bFound = false;
// ID of the note being saved
int nID = m_Note.m_nID;
// Let's see if this note was saved already
for (iter = (m_pWndParent->m_vecNotes).begin(); iter != (m_pWndParent->m_vecNotes).end(); iter++)
{
if ((*iter).m_nID == nID) // note is in the collection
{
bFound = true;
break;
}
}
// At this point the only thing CNote object does not know is the note's text
// So let's retrieve it
// Obtain the length of the text within the edit control
int nLen = m_wndEdit.GetWindowTextLength();
// Create the buffer that will receive the text
TCHAR * pszText = new TCHAR[(nLen + 1) * sizeof(TCHAR)];
if (!pszText)
ATLTRACE(_T("Not enough memory to allocate for the buffer\n"));
pszText[nLen * sizeof(TCHAR)] = '\0';
// Copy the text of the edit control into the buffer
m_wndEdit.GetWindowText(pszText, nLen + 1);
if (nLen == FALSE)
ATLTRACE(_T("GetWindowText() failed\n"));
// Set the CNote object's text attribute
m_Note.m_strNoteText = pszText;
delete[] pszText;
// Save the note
if (bFound) // note already exists
{
*iter = m_Note;
}
else // it's a new note
{
// Insert a CNote object at the end of the vector
(m_pWndParent->m_vecNotes).push_back(m_Note);
}
}
// Handles clicking on 'Delete' menu item
void CNoteDlg::OnDelete()
{
ATLTRACE(_T("CNoteDlg::OnDelete()\n"));
vector<CNote>::iterator iter;
// ID of the note being deleted
int nID = m_Note.m_nID;
// If the note is found remove it from the collection
for (iter = (m_pWndParent->m_vecNotes).begin(); iter != (m_pWndParent->m_vecNotes).end(); iter++)
{
if ((*iter).m_nID == nID)
{
(m_pWndParent->m_vecNotes).erase(iter);
break;
}
}
// Delete all of the text in the edit control
m_wndEdit.SetSel(0, -1);
m_wndEdit.Clear();
m_wndEdit.SetModify(FALSE);
}
// Handles a menu item selection
LRESULT CNoteDlg::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CNoteDlg::OnCommand()\n"));
switch (LOWORD(wParam))
{
case IDR_MENU_NEWNOTE:
OnNewNote();
break;
case IDR_MENU_SAVE:
OnSave();
break;
case IDR_MENU_DELETE:
OnDelete();
break;
case IDR_MENU_ONTOP:
OnAlwaysOnTopState();
break;
case IDR_MENU_SETFONT:
OnSetFont();
break;
case IDR_MENU_CLOSE:
CloseDialog(0);
break;
case IDR_MENU_BLUE:
OnChangeBkgndColor(BLUE);
break;
case IDR_MENU_GREEN:
OnChangeBkgndColor(GREEN);
break;
case IDR_MENU_PINK:
OnChangeBkgndColor(PINK);
break;
case IDR_MENU_YELLOW:
OnChangeBkgndColor(YELLOW);
break;
case IDR_MENU_WHITE:
OnChangeBkgndColor(WHITE);
break;
default:
break;
}
return 0;
}
// When the previously saved note is displayed the whole text inside the
// richedit control is initially selected. So this function fixes it.
void CNoteDlg::UnselectText()
{
ATLTRACE(_T("CNoteDlg::UnselectText()\n"));
long lStartChar, lEndChar;
// Retrieve the bounds of the current selection
m_wndEdit.GetSel(lStartChar, lEndChar);
// Set the selection to be all characters after the current selection
// and place the caret at the end of the selection
m_wndEdit.SetSel(lEndChar, -1);
}
//-----------------------------------------------------------------------------------
// ReplaceColor
//
// Author : Dimitri Rochette drochette@ltezone.net
// Article : "How to replace a color in a HBITMAP" (www.codeproject.com)
//
// hBmp : Source Bitmap
// cOldColor: Color to replace in hBmp
// cNewColor: Color used for replacement
// hBmpDC : DC of hBmp ( default NULL ) could be NULL if hBmp is not selected
//
// Retcode : HBITMAP of the modified bitmap or NULL for errors
//
//-----------------------------------------------------------------------------------
#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) | ((Color << 16) & 0xff0000)
HBITMAP CNoteDlg::ReplaceColor(HBITMAP hBmp, COLORREF cOldColor, COLORREF cNewColor, HDC hBmpDC)
{
ATLTRACE(_T("CNoteDlg::ReplaceColor()\n"));
HBITMAP RetBmp=NULL;
if (hBmp)
{
HDC BufferDC=CreateCompatibleDC(NULL); // DC for Source Bitmap
if (BufferDC)
{
HBITMAP hTmpBitmap = (HBITMAP) NULL;
if (hBmpDC)
if (hBmp == (HBITMAP)GetCurrentObject(hBmpDC, OBJ_BITMAP))
{
hTmpBitmap = CreateBitmap(1, 1, 1, 1, NULL);
SelectObject(hBmpDC, hTmpBitmap);
}
HGDIOBJ PreviousBufferObject=SelectObject(BufferDC,hBmp);
// Here BufferDC contains the bitmap
HDC DirectDC=CreateCompatibleDC(NULL); // DC for working
if (DirectDC)
{
// Get bitmap size
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
// Create a BITMAPINFO with minimal initilisation
// for the CreateDIBSection
BITMAPINFO RGB32BitsBITMAPINFO;
ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
// Pointer used for direct Bitmap pixels access
UINT * ptPixels;
HBITMAP DirectBitmap = CreateDIBSection(DirectDC,
(BITMAPINFO *)&RGB32BitsBITMAPINFO,
DIB_RGB_COLORS,
(void **)&ptPixels,
NULL, 0);
if (DirectBitmap)
{
// Here DirectBitmap!=NULL so ptPixels!=NULL no need to test
HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);
BitBlt(DirectDC,0,0,
bm.bmWidth,bm.bmHeight,
BufferDC,0,0,SRCCOPY);
// Here the DirectDC contains the bitmap
// Convert COLORREF to RGB (Invert RED and BLUE)
cOldColor=COLORREF2RGB(cOldColor);
cNewColor=COLORREF2RGB(cNewColor);
// After all the inits we can do the job : Replace Color
for (int i=((bm.bmWidth*bm.bmHeight)-1);i>=0;i--)
{
if (ptPixels[i]==cOldColor) ptPixels[i]=cNewColor;
}
// Little clean up
// Don't delete the result of SelectObject because it's
// our modified bitmap (DirectBitmap)
SelectObject(DirectDC,PreviousObject);
// Finish
RetBmp=DirectBitmap;
}
// Clean up
DeleteDC(DirectDC);
}
if (hTmpBitmap)
{
SelectObject(hBmpDC, hBmp);
DeleteObject(hTmpBitmap);
}
SelectObject(BufferDC,PreviousBufferObject);
// BufferDC is now useless
DeleteDC(BufferDC);
}
}
return RetBmp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -