if thewindow being removed is the last window in the chain
// 【MoreWindows工作筆記12】WM_DRAWCLIPBOARD 監視剪下板// http://blog.csdn.net/morewindows/article/details/17655429// By MoreWindows( http://blog.csdn.net/MoreWindows )BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){ static HWND s_hwnd_new_clipboard_viewer = NULL; static HWND s_hwnd_edit_clipboard_info = NULL;switch (message){case WM_INITDIALOG: // 4-1 Add the window to the clipboard viewer chain. s_hwnd_new_clipboard_viewer = SetClipboardViewer(hDlg); // 把hwnd加入監視鏈,返回上一個加入的視窗控制代碼,如果是第一個,則傳回值為NULL。新加的視窗在鏈條頭部,成為“當前監視器”CenterWindow(hDlg); s_hwnd_edit_clipboard_info = GetDlgItem(hDlg, IDC_EDIT_CLIPBOARD_INFO);return FALSE; // 4-2 Process the WM_CHANGECBCHAIN message. case WM_CHANGECBCHAIN: if ((HWND)wParam == s_hwnd_new_clipboard_viewer) s_hwnd_new_clipboard_viewer = (HWND)lParam; else SendMessage(s_hwnd_new_clipboard_viewer, message, wParam, lParam); // 4-3 Process the WM_DRAWCLIPBOARD message. case WM_DRAWCLIPBOARD: //剪下板內容發生變化 if (OpenClipboard(hDlg)) { UINT clipboard_format = EnumClipboardFormats(0); HGLOBAL global_memory = GetClipboardData(clipboard_format); DWORD data_size = GlobalSize(global_memory); CloseClipboard(); WCHAR clipboard_info[1024]; swprintf(clipboard_info, L"Clipboard\r\n Data Format = %x\r\n Data Address = 0x%x\r\n Data Size = %d", clipboard_format, global_memory, data_size); if (clipboard_format == CF_UNICODETEXT) { LPCWSTR clipboard_data = (LPCWSTR)GlobalLock(global_memory); if (clipboard_data != NULL) { wcscat(clipboard_info, L"\r\nData: \r\n"); WCHAR buffer[1024]; DWORD data_size = GlobalSize(global_memory); for (size_t i = 0; i < data_size; i++) buffer[i] = clipboard_data[i]; buffer[data_size] = L'\0'; wcscat(clipboard_info, buffer); } } SetWindowTextW(s_hwnd_edit_clipboard_info, clipboard_info); } return FALSE;case WM_COMMAND:switch (LOWORD(wParam)){case IDOK:case IDCANCEL: // 4-4 Remove the window from the clipboard viewer chain before it is destroyed. ChangeClipboardChain(hDlg, s_hwnd_new_clipboard_viewer);EndDialog(hDlg, FALSE);return TRUE;}break;}return FALSE;}