?? viewerlib.cpp
字號(hào):
/****************************************************************************
ViewerLib.cpp : Defines the Image Viewer DLL application.
written by PJ Arends
pja@telus.net
For updates check http://www.codeproject.com/tools/imageviewer.asp
-----------------------------------------------------------------------------
This code is provided as is, with no warranty as to it's suitability or usefulness
in any application in which it may be used.
This code may be used in any way you desire. This file may be redistributed by any
means as long as it is not sold for profit, and providing that this notice and the
author's name are included. Any modifications not made by the original author should
be clearly marked as such to remove any confusion between the original version and
any other versions.
If any bugs are found and fixed, a note to the author explaining the problem and
fix would be nice.
-----------------------------------------------------------------------------
****************************************************************************/
#define _WIN32_WINNT 0x0501 // Windows XP
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <commctrl.h> // for HIMAGELIST
#include <tchar.h>
#include <sstream>
#include <string>
#include <iomanip>
#include <atlbase.h> // for CA2W string conversion function and ATLTRACE macro
#include <sys\timeb.h>
#include <math.h>
#include "resource.h"
#include "..\common files\pja_dc.h"
#include "..\common files\pja_bitmap.h"
#include "..\common files\pja_format.h"
#include "..\common files\tstl.h"
#include "IVDllWindow.h"
CIVDllWindow Window;
HMODULE DllModule = NULL;
/////////////////////////////////////////////////////////////////////////////
//
// DllMain
// The dll's entry point
//
// Parameters :
// None
//
// Returns :
// TRUE
//
/////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain(HMODULE hModule, DWORD, LPVOID)
{
DllModule = hModule;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
//
// CurrentTime
// Retrieves the current system time
//
// Parameters :
// None
//
// Returns :
// A wide string containing the current system time
//
/////////////////////////////////////////////////////////////////////////////
std::wstring CurrentTime()
{
_timeb TimeB;
_ftime_s(&TimeB);
std::wstring String = _wctime(&TimeB.time);
std::wostringstream str;
str << String.substr(0, 10) << L',' << String.substr(19, 5);
str << String.substr(10, 9) << L'.' << std::setw(3) << std::setfill(L'0') << TimeB.millitm << std::ends;
return str.str().c_str();
}
/////////////////////////////////////////////////////////////////////////////
//
// class BGBrush
// This class is used to retrieve a HBRUSH that is the same colour as that saved
// as the background colour by the user of the Image Viewer utility.
//
/////////////////////////////////////////////////////////////////////////////
class BGBrush
{
HBRUSH m_Brush;
COLORREF m_Colour;
public:
BGBrush()
{
// default brush colour
m_Colour = RGB(192, 192, 192);
HKEY hKey = NULL;
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
_T("Software\\PJ Arends\\Image Viewer\\Settings"),
0,
KEY_READ,
&hKey))
{
DWORD Size = sizeof(COLORREF);
RegQueryValueEx(hKey,
_T("Background Colour"),
NULL,
NULL,
(LPBYTE)&m_Colour,
&Size);
RegCloseKey(hKey);
}
m_Brush = CreateSolidBrush(m_Colour);
}
~BGBrush()
{
DeleteObject(m_Brush);
}
operator HBRUSH()
{
return m_Brush;
}
operator COLORREF()
{
// returns the ideal text colour that will contrast with this brush
// technique by John Simmons http://www.codeproject.com/useritems/IdealTextColor.asp
int Grey = (GetRValue(m_Colour) * 299 + GetGValue(m_Colour) * 587 + GetBValue(m_Colour) * 114) / 1000;
return Grey > 105 ? RGB(0, 0, 0) : RGB(255, 255, 255);
}
};
/////////////////////////////////////////////////////////////////////////////
//
// AreWindowsReady
// Creates the dll's hidden window and checks if the app's hidden window
// is also present. If these two windows are not present and properly
// setup the dll will fail to show any output.
//
// Parameters :
// None
//
// Returns :
// true if the windows are properly setup
// false if the windows are not setup
//
/////////////////////////////////////////////////////////////////////////////
bool AreWindowsReady()
{
if (!Window.Create())
{
ATLTRACE(_T("ImageViewer.dll : ImageViewer dll window not created"));
return false;
}
if (!IsWindow(Window))
{
ATLTRACE(_T("ImageViewer.dll : Image Viewer application not running\n"));
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
//
// SendImageToViewer
// Convert the supplied HBITMAP to a DIBSection, and pass it on
// to the Image Viewer application.
//
// Parameters :
// TheBitmap [in] - The bitmap to send
// File [in] - The complete path to the source file that called this function
// Line [in] - The line number in the source file from where the call was made
// Function [in] - The name of the function from where the call was made
// Text [in] - User defined text
// pRegionRect [in] - For regions only, the region's bounding rectangle
//
// Returns :
// ERROR_SUCCESS if the image was sent to the viewer, a Win32 error code otherwise
//
// Note :
// The Image Viewer application has to be able to read the data that is sent to it.
// If you change this function to either add data to, remove data from, or otherwise
// alter it, then you will also have to change the CImageData class in the Viewer
// Application project to reflect the changes made here. You may also have to change
// the CRecieverWindow::OnCopyData() function.
//
/////////////////////////////////////////////////////////////////////////////
LRESULT SendImageToViewer (HBITMAP TheBitmap,
LPCWSTR File,
UINT Line,
LPCWSTR Function,
LPCWSTR Text,
LPCRECT pRegionRect)
{
// Check that the windows are properly setup
if (!AreWindowsReady())
{
return ERROR_NOT_READY;
}
// Validate the parameters
if (!TheBitmap)
{
ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - A NULL HBITMAP handle was passed in\n"));
return ERROR_INVALID_HANDLE;
}
if (NULL == File)
{
File = _T("");
}
if (NULL == Function)
{
Function = _T("");
}
if (NULL == Text)
{
Text = _T("");
}
// Create the bitmap that gets displayed in the Image Viewer application
// by making a copy of the supplied bitmap 'TheBitmap'
pja::CBitmap Image(TheBitmap, true);
if (NULL == Image)
{
ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - Failed to copy the supplied image\n"));
return ERROR_FUNCTION_FAILED;
}
// Create the memory dc needed to extract the bitmap's bits
pja::CCompatibleDC ImageDC(NULL);
if (NULL == ImageDC)
{
ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - Failed to create the memory device context\n"));
return ERROR_FUNCTION_FAILED;
}
SelectObject(ImageDC, Image);
// Calculate the size of the memory needed for the data buffer
size_t ColourTableSize = 0;
if (Image.BitsPixel() < 16)
{
ColourTableSize = sizeof(RGBQUAD) * 256;
}
// size of the bitmaps bit table
DWORD BitsSize = ((Image.Width() * Image.BitsPixel() + 31) & (~31)) / 8 * Image.Height();
DWORD InfoSize = sizeof(BITMAPINFO);
// Build the information string
std::wostringstream stream;
stream << File << std::endl;
stream << Line << std::endl;
stream << Function << std::endl;
stream << CurrentTime() << std::endl;
stream << Text << std::ends;
size_t len = wcslen(stream.str().c_str()) + 1;
// Create the data buffer that will be sent via the WM_COPYDATA message
DWORD DataBufferSize = InfoSize + ColourTableSize + BitsSize + len * sizeof(wchar_t) + sizeof(DWORD) + sizeof(RECT);
BYTE *DataBuffer = new BYTE[DataBufferSize];
if (NULL == DataBuffer)
{
ATLTRACE(_T("ImageViewer.dll : SendImageToViewer - Failed to allocate memory for the data buffer\n"));
return ERROR_FUNCTION_FAILED;
}
// Assume success for the rest of the operation
LRESULT ReturnCode = ERROR_SUCCESS;
// Fill the data buffer with data
memset(DataBuffer, 0, DataBufferSize);
BYTE *DataInsertionPointer = DataBuffer;
// The ID of the current process goes into the data buffer first
DWORD ProcessID = GetCurrentProcessId();
*reinterpret_cast<DWORD *>(DataInsertionPointer) = ProcessID;
// The region rectangle goes in second
DataInsertionPointer += sizeof(DWORD);
if (NULL != pRegionRect)
{
memcpy_s(DataInsertionPointer, sizeof(RECT), pRegionRect, sizeof(RECT));
}
else
{
memset(DataInsertionPointer, 0, sizeof(RECT));
}
DataInsertionPointer += sizeof(RECT);
// The descriptive text goes into the data buffer third
wcscpy_s(reinterpret_cast<wchar_t *>(DataInsertionPointer), len, stream.str().c_str());
DataInsertionPointer += len * sizeof(wchar_t);
// The BITMAPINFO structure goes into the DataBuffer fourth
BITMAPINFO *pBitmapInfo = reinterpret_cast<BITMAPINFO *>(DataInsertionPointer);
pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBitmapInfo->bmiHeader.biHeight = Image.Height();
pBitmapInfo->bmiHeader.biWidth = Image.Width();
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -