把原来是在 WndProc里处理WM_HOTKEY的代码独立放到一个函数里调用,居然出错,原来的代码如下:#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <malloc.h>#define ID_TIMER 1LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ) ;
void mbox( LPSTR buff );static TCHAR buff[1024] ;int WINAPI
WinMain( HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 PSTR szCmdLine,
 int iCmdShow )
{
。。
while ( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
return msg.wParam ;
}LRESULT CALLBACK
WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
static COLORREF cr, crLast ;
static COLORREF cr_aj;
static int id_t = 0xaa;
static int VK_T = 0x54;
static HDC hdcScreen ;
static HDC hdcCompatible;
static HBITMAP hbmScreen;
static HDC hdc ;
static HBITMAP hbmHDC;
static BITMAPINFO bminfo; int x = 0;
int y = 0;
int i;
static byte destBits[800 * 600 * 4]; switch ( message )
{
  case   WM_CREATE:
hdcScreen = CreateDC( TEXT( "DISPLAY" ), NULL, NULL, NULL ) ;
hdc = GetDC( hwnd );
SetTimer( hwnd, ID_TIMER, 100, NULL ) ;
RegisterHotKey( hwnd, id_t, MOD_CONTROL, VK_T );
return 0 ;   case WM_HOTKEY:
hdcCompatible = CreateCompatibleDC( hdcScreen ); 
// Create a compatible bitmap for hdcScreen. 
hbmScreen = CreateCompatibleBitmap( hdcScreen,
GetDeviceCaps( hdcScreen, HORZRES ),
GetDeviceCaps( hdcScreen, VERTRES ) ); 
SelectObject( hdcCompatible, hbmScreen ); 
BitBlt( hdcCompatible, 0, 0, 800, 600, hdcScreen, 0, 0, SRCCOPY );//复制屏幕图像到内存DC bminfo.bmiHeader. biSize = sizeof( BITMAPINFOHEADER ); // Size Of The BitmapInfoHeader
bminfo.bmiHeader. biPlanes = 1; // Bitplanes
bminfo.bmiHeader.biBitCount = 32; // Bits Format We Want (32 Bit, 4 Bytes)
bminfo.bmiHeader.biWidth = 800; // Width We Want (800 Pixels)
bminfo.bmiHeader.biHeight = 600; // Height We Want (600 Pixels)
bminfo.bmiHeader.biCompression = BI_RGB; // Requested Mode = RGB i = GetDIBits( hdcCompatible,
   hbmScreen,
   0,
   600,
   destBits,
   &bminfo,
   DIB_RGB_COLORS );
//draw to my program
hbmHDC = CreateCompatibleBitmap( hdc,
 GetDeviceCaps( hdc, HORZRES ),
 GetDeviceCaps( hdc, VERTRES ) );
SelectObject( hdc, hbmHDC ); 
SetDIBitsToDevice( hdc,
   0,
   0,
   800,
   600,
   0,
   0,
   0,
   600,
   destBits,
   &bminfo,
   DIB_RGB_COLORS ); DeleteDC( hdcCompatible ); 
DeleteObject( hbmScreen ); 
return 0;   case   WM_DESTROY:
DeleteDC( hdcScreen ) ;
KillTimer( hwnd, ID_TIMER ) ;
UnregisterHotKey( hwnd, id_t );
PostQuitMessage( 0 ) ;
return 0 ;
}
return DefWindowProc( hwnd, message, wParam, lParam ) ;
}
void
mbox( LPSTR buff )
{
MessageBox( NULL, buff, TEXT( "aj_MessageBox" ), MB_OK );
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
修改后的代码如下:#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <malloc.h>#define ID_TIMER 1LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ) ;
void mbox( LPSTR buff );
void CopyScreen( HDC );static TCHAR buff[1024] ;
static COLORREF Color_to_Find;
HWND hwnd_myapp;int WINAPI
WinMain( HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 PSTR szCmdLine,
 int iCmdShow )
{
。。
while ( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
return msg.wParam ;
}LRESULT CALLBACK
WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
static int id_t = 0xaa;
static int VK_T = 0x54;
HDC hdc;
switch ( message )
{
  case   WM_CREATE:
hwnd_myapp = hwnd;
SetTimer( hwnd, ID_TIMER, 100, NULL ) ;
RegisterHotKey( hwnd, id_t, MOD_CONTROL, VK_T );
return 0 ;   case WM_HOTKEY:
hdc = GetDC( hwnd );
CopyScreen( hdc );
//   hdc = GetDC(hwnd);
//   draw(hdc);
return 0;   case WM_LBUTTONDOWN:
KillTimer( hwnd, ID_TIMER ) ;
UnregisterHotKey( hwnd, id_t );
PostQuitMessage( 0 ) ;
  case   WM_DESTROY:
KillTimer( hwnd, ID_TIMER ) ;
UnregisterHotKey( hwnd, id_t );
PostQuitMessage( 0 ) ;
return 0 ;
} return DefWindowProc( hwnd, message, wParam, lParam ) ;
}
void
mbox( LPSTR buff )
{
MessageBox( NULL, buff, TEXT( "" ), MB_OK );
}
void
CopyScreen( HDC hdc )
{
static byte destBits[800 * 600 * 4];
static HDC hdcScreen ;
static HDC hdcCompatible;
static HBITMAP hbmScreen;
static HBITMAP hbmHDC;
static BITMAPINFO bminfo;
int x = 0;
int y = 0;
int i; hdcScreen = CreateIC( "DISPLAY", NULL, NULL, NULL );
hdcCompatible = CreateCompatibleDC( hdcScreen ); 
// Create a compatible bitmap for hdcScreen. 
hbmScreen = CreateCompatibleBitmap( hdcScreen,
GetDeviceCaps( hdcScreen, HORZRES ),
GetDeviceCaps( hdcScreen, VERTRES ) ); 
SelectObject( hdcCompatible, hbmScreen );  BitBlt( hdcCompatible, 0, 0, 800, 600, hdcScreen, 0, 0, SRCCOPY );//复制屏幕图像到内存DC bminfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); // Size Of The BitmapInfoHeader
bminfo.bmiHeader.biPlanes = 1; // Bitplanes
bminfo.bmiHeader.biBitCount = 32; // Bits Format We Want (32 Bit, 4 Bytes)
bminfo.bmiHeader.biWidth = 800; // Width We Want (800 Pixels)
bminfo.bmiHeader.biHeight = 600; // Height We Want (600 Pixels)
bminfo.bmiHeader.biCompression = BI_RGB; // Requested Mode = RGB i = GetDIBits( hdcCompatible,
   hbmScreen,
   0,
   600,
   destBits,
   &bminfo,
   DIB_RGB_COLORS );
if ( i == 0 )
{
mbox( "getdibits failed" );

//draw to my program
hbmHDC = CreateCompatibleBitmap( hdc,
 GetDeviceCaps( hdc, HORZRES ),
 GetDeviceCaps( hdc, VERTRES ) );
SelectObject( hdc, hbmHDC ); 
SetDIBitsToDevice( hdc,
   0,
   0,
   800,
   600,
   0,
   0,
   0,
   600,
   destBits,
   &bminfo,
   DIB_RGB_COLORS ); DeleteDC( hdcScreen );
DeleteDC( hdcCompatible ); 
DeleteObject( hbmScreen );
}就把原来的代码放到一个函数里,然后调用这个函数,结果显示出来是一片黑色。