English 中文(简体)
如何制定减少浮油的法规? 你们需要法典帮助。 页: 1
原标题:how to code for reducing flicker? need your help with code. win32 VC++ flicker
  • 时间:2009-10-09 02:11:08
  •  标签:

我试图用比图把 cur子的位置与 mo花.鱼混为一谈。 但也存在问题。

I ve read about double buffering to reduce flicker but I m not sure how to ... this causes extreme flickering. I ve read about double buffering to reduce flicker but I m not sure how to implement it in this example. Please can you help? Thanks

页: 1 感谢你们的帮助!

// screen blinks.trying to use double buffer so solve this problem.
#include <windows.h>
HDC bufferDC = NULL;
HDC           hdc=GetWindowDC(NULL) ;
HDC hammerDC = NULL; 
HBITMAP hammer1BMP = NULL;
HBITMAP bufferBMP = NULL;
POINT cursorpoint;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("DigClock") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     hwnd = CreateWindow (szAppName, TEXT ("Digital Clock"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;


     bufferDC=CreateCompatibleDC(hdc);
     hammerDC=CreateCompatibleDC(hdc);
     hammer1BMP=(HBITMAP)LoadImage(NULL,"star.bmp",IMAGE_BITMAP,160,160,LR_LOADFROMFILE);
     SelectObject(hammerDC,hammer1BMP);

     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 BOOL   f24Hour, fSuppress ;
     static HBRUSH hBrushRed ;
     static int    cxClient, cyClient ;
     HDC           hdc ;
     PAINTSTRUCT   ps ;
     TCHAR         szBuffer [2] ;

     switch (message)
     {
     case WM_CREATE:
          hBrushRed = CreateSolidBrush (RGB (255, 0, 0)) ;
          SetTimer (hwnd, ID_TIMER, 1000/24,NULL) ;//1000

                                                  // fall through

     case WM_SETTINGCHANGE:

          InvalidateRect (hwnd, NULL, TRUE) ;
          return 0 ;

     case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
          return 0 ;

     case WM_TIMER:
          InvalidateRect (hwnd, NULL, TRUE) ;
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          bufferBMP=CreateCompatibleBitmap(hdc,cxClient,cyClient);
          SelectObject(bufferDC,bufferBMP);
          // SelectObject(bufferDC,hammer1BMP); 
          GetCursorPos(&cursorpoint);
          BitBlt(bufferDC,0,0,cxClient,cyClient,hammerDC,0,0,SRCCOPY);
          BitBlt(hdc,cursorpoint.x,cursorpoint.y,cxClient,cyClient,bufferDC,0,0,SRCCOPY);


          EndPaint (hwnd, &ps) ;
          return 0 ;
     case WM_LBUTTONDOWN:
         // GetCursorPos(&cursorpoint);
          //BitBlt(hdc,cursorpoint.x,cursorpoint.y,cxClient,cyClient,hammerDC,0,0,SRCCOPY);
          return 0;
     case WM_DESTROY:
          KillTimer (hwnd, ID_TIMER) ;
          DeleteDC(hammerDC);
          DeleteObject (hBrushRed) ;
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
问题回答

在这种情况下,我们看不出你真的需要双重缓冲——事实上,它可能赢得你们的帮助。

你的摇摇摇摇篮的主要事业是抹去背景,然后立即取而代之。 由于你显然把你窗户全部从WM_中抽走。 PAINT,只是为WM_ERASEBKGND增加一名手,这只是为了表明背景已经消失,才回到了TUE。

Edit(回应评论):

更完整地说,如果你用一个颜色描绘一个区域,则会很快以另一个颜色重新铺设一个区域,那么,浮标结果(几乎几乎)就会出现。 双重缓冲会有助于你/如果其原地有多种不同肤色的传承元素。 你(至少是)将过度地区带入后方缓冲地带,然后,只有当你有正确的颜色时,你才能将他们带上屏幕。 在这种情况下,原来的法典将翻一番,但它仍然引出背景,然后是地下,而且你仍在疲软。

另一项答复提到,将第二个参数变为无效。 这将有助于很多人,因为它在应对这一缺陷方面赢得了重选背景。 只有他才能走到场,这样就没有浮标。 不幸的是,当(至少一部分)温饱和状 rec因任何其他原因而失效时,你仍会感到疲软,因为它仍然背着地下的背景。

删除你的时间。 即使窗户没有变化,时间也使你丧失工作能力。 此外,你正在打破窗户,每次长途.。

从根本上说,你认为这一概念正确。 当你需要WM_PAINT时,就复制你背后的缓冲。 当你需要更新图象、更新你的后身缓冲,然后只有一次无效。

另一端是利用GetUpdateRect()在WM_获得更新的区域。 PAINT。 仅复制本区域的遗.。 这进一步优化了你的双重缓冲

浮油(和最终的方案坠毁)的起因多种多样:

  1. There is the background brush - set hbrBackground to inhibit the generation of WM_ERASEBKGND messages.
  2. You are creating (and leaking) a bufferBMP per WM_PAINT.
  3. You are not painting the window correctly - why are you painting to the cursor position? If you want to have the hammer track the mouse, you would paint the hammer onto the offscreen bitmap at the appropriate location, and then blit the offscreen bitmap to cover the client area, i.e. to 0,0,cx,cy

To avoid flickering use double buffering method. In this method we do painting procedure over off screen DC (hdcBuffer) and then copy the content of that DC to actual screen DC (hdc). Also avoid erasing of background by returning non zero to WM_ERASEBKGND message. Here is the algo:

hdc = BeginPaint(hwnd,&ps); // actual screen DC

hdcBuffer = CreateCompatibleDC (hdc)  // OFF screen DC

hBitmapBuffer = CreateCompatibleBitmap (hdc,BitmapWidth, BitmapHeight);  // create memory bitmap for that off screen DC

SelectObject(hdcBuffer,hBitmapBuffer); // Select the created memory bitmap into the OFF screen DC

/* Then do your painting job using hdcBuffer over off screen DC */

BitBlt(hdc,0,0,BitmapWidth,BitmapHeight,hdcBuffer,0,0,SRCCOPY); // copy the content of OFF screen DC to actual screen DC

DeleteDC (hdcBuffer); // Release the OFF screen DC

DeleteObject (hBitmapBuffer); // Free the memory for bitmap

EndPaint(hwnd,&ps); // Release the actual screen DC




相关问题
热门标签