// shuang.cpp : Defines the entry point for the application.
//#include "stdafx.h"
#include "resource.h"#define MAX_LOADSTRING 100// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);void InitBuffer(HWND);
void ShowBuffer(HWND);
void Show(HWND);HDC hfdc, hf1dc;
HDC mdc;//HBITMAP hbNull;
HBITMAP hbg,hbg1, hNull;int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
MSG msg;
HACCEL hAccelTable; // Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SHUANG, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); // Perform application initialization:
if (!InitInstance (hInstance, nCmdShow)) 
{
return FALSE;
} hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SHUANG); // Main message loop:
while (GetMessage(&msg, NULL, 0, 0)) 
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return msg.wParam;
}//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX);  wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SHUANG);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_SHUANG;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);
}//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
RECT rect;
HDC hdc; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd)
{
      return FALSE;
}
    MoveWindow(hWnd,10,10,480,640,true);
GetClientRect(hWnd, &rect);
   
// Init( hWnd );
InitBuffer( hWnd );

hdc = GetDC( hWnd );
// 加载要显示的两张位图
hfdc = CreateCompatibleDC(hdc);
hf1dc = CreateCompatibleDC(hdc);
hbg = (HBITMAP)LoadImage(hInstance,"bg1.bmp",IMAGE_BITMAP,170,170,LR_LOADFROMFILE); // 遮挡图
hbg1 = (HBITMAP)LoadImage(hInstance,"bg2.bmp",IMAGE_BITMAP,170,170,LR_LOADFROMFILE);// 前景图 
SelectObject(hfdc, hbg);
SelectObject(hf1dc, hbg1); ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetTimer(hWnd,1,30,NULL);
DeleteDC( hdc ); 
   return TRUE;
}//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) 
{

case WM_COMMAND:
wmId    = LOWORD(wParam); 
wmEvent = HIWORD(wParam); 
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
   break;
case IDM_EXIT:
   DestroyWindow(hWnd);
   break;
default:
   return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_TIMER:
ShowBuffer( hWnd );
Show( hWnd );
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
    return FALSE;
}void InitBuffer(HWND hWnd) // 不闪烁的初始化
{
HDC hdc;
hdc = GetDC( hWnd );

// 创建一个空位图放到mdc中
hNull = CreateCompatibleBitmap( hdc, 170, 170 );  
mdc = CreateCompatibleDC(hdc);
SelectObject( mdc, hNull );  // 贴背景图到mdc中,由于这里是白色背景,所以贴的WHITENESS,可根据自己的背景把背景图贴上去
BitBlt(mdc, 0,0,170,170,NULL,NULL,NULL,WHITENESS); DeleteDC( hdc );
}void ShowBuffer( HWND hWnd ) // 不会闪屏的贴图
{
HDC hdc;
hdc = GetDC(hWnd); // 所有的画图都贴到缓存中,这里缓存是mdc
BitBlt(mdc,0,0,170,170,hfdc,0,0,SRCAND);
BitBlt(mdc,0,0,170,170,hf1dc,0,0,SRCPAINT); // 再把缓存中的贴到屏幕上
BitBlt(hdc,0,0,170,170,mdc,0,0,SRCCOPY);
DeleteDC( hdc );
}void Show( HWND hWnd ) // 要闪屏的贴图
{
HDC hdc;
hdc = GetDC( hWnd );
BitBlt(hdc,0,150,170,170,hfdc,0,0,SRCAND);
BitBlt(hdc,0,150,170,170,hf1dc,0,0,SRCPAINT);
DeleteDC( hdc );
}
/************************************************
创建后备缓冲后
 1.清除后备缓冲,用背景色填充
 2.在后备缓冲中贴图
 3.将后备缓冲区的内容复制到前台缓冲区
************************************************/
第一个函数是双缓存,是不闪的,第二个函数是闪烁的,我想问但把它们放到WM_TIMER是以上我说的情况,但是如果把它放到WM_PAINT里,在WM_TIMER中用InvalidateRect();时两个都闪呀?怎么让他不再闪了

解决方案 »

  1.   

    需要在绘制背景的时候绘制, 而不是在OnPaint中绘制
      

  2.   

    双缓存的问题是说提高刷新率的,用两个缓存前后交替流水执行,可以有一部分重叠时间。后面的操作应该是因为间断刷屏造成的,那个InvalidateRect()会发送wm_paint命令,把时间减慢一些吧
      

  3.   

    http://download.csdn.net/source/1981522
    也许对你有用
      

  4.   

    你这两个函数里面都用了GetDC,放在WM_TIMER里面调用一个闪一个不闪是对的,但是放到WM_PAINT里面,只有在BeginPaint和EndPaint之间获得的HDC上绘制才不会闪,在此之外获得的HDC上画图都要闪。
      

  5.   

    放到WM_PAINT里面,只有在BeginPaint和EndPaint之间两个都闪
      

  6.   


    我说的是只有在BeginPaint和EndPaint之间获得的HDC上绘制才不会闪,放在WM_PAINT里面,你函数里的GetDC要拿掉,BeginPaint后调用你那两个函数,把HDC作为参数传进去,函数里在传入的HDC上绘制。void ShowBuffer( HWND hWnd,HDC hdc ) // 不会闪屏的贴图 

    // 所有的画图都贴到缓存中,这里缓存是mdc 
    BitBlt(mdc,0,0,170,170,hfdc,0,0,SRCAND); 
    BitBlt(mdc,0,0,170,170,hf1dc,0,0,SRCPAINT); // 再把缓存中的贴到屏幕上 
    BitBlt(hdc,0,0,170,170,mdc,0,0,SRCCOPY);