我想实现窗口全屏抖动下面的WIN32程序是实现另外一种效果的,如何改进,使其变成窗口抖动呢?能否帮忙解释一下程序
/*------------------------------------------------
   SCRAMBLE.C -- Scramble (and Unscramble) Screen
                 (c) Charles Petzold, 1998
  ------------------------------------------------*/
#include <windows.h>#define NUM 30//LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static int iKeep [NUM][4] ;
     HDC        hdcScr, hdcMem ;
     int        cx, cy ;
     HBITMAP    hBitmap ;
     HWND       hwnd ;
     int        i, j, x1, y1, x2, y2 ;     if (LockWindowUpdate (hwnd = GetDesktopWindow ()))
     {
          hdcScr  = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
          hdcMem  = CreateCompatibleDC (hdcScr) ;
          cx      = GetSystemMetrics (SM_CXSCREEN) / 10 ;
          cy      = GetSystemMetrics (SM_CYSCREEN) / 10 ;
          hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;
         
          SelectObject (hdcMem, hBitmap) ;          srand ((int) GetCurrentTime ()) ;
          
          for (i = 0 ; i < 2   ; i++)
          for (j = 0 ; j < NUM ; j++)
          {
               if (i == 0)
               {
                    iKeep [j] [0] = x1 = cx * (rand () % 10) ;
                    iKeep [j] [1] = y1 = cy * (rand () % 10) ;
                    iKeep [j] [2] = x2 = cx * (rand () % 10) ;
                    iKeep [j] [3] = y2 = cy * (rand () % 10) ;
               }
               else
               {
                    x1 = iKeep [NUM - 1 - j] [0] ;
                    y1 = iKeep [NUM - 1 - j] [1] ;
                    x2 = iKeep [NUM - 1 - j] [2] ;
                    y2 = iKeep [NUM - 1 - j] [3] ;
               }
               BitBlt (hdcMem,  0,  0, cx, cy, hdcScr, x1, y1, SRCCOPY) ;
               BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ;
               BitBlt (hdcScr, x2, y2, cx, cy, hdcMem,  0,  0, SRCCOPY) ;
                    
               Sleep (10) ;
          }
               
          DeleteDC (hdcMem) ;
          ReleaseDC (hwnd, hdcScr) ;
          DeleteObject (hBitmap) ;
             
          LockWindowUpdate (NULL) ;
     }
     return FALSE ;
}

解决方案 »

  1.   

    MoveWindow就可以实现最简单的抖动
      

  2.   

    1.解释
    /*------------------------------------------------
      SCRAMBLE.C -- Scramble (and Unscramble) Screen
      (c) Charles Petzold, 1998
      ------------------------------------------------*/
    #include <windows.h>#define NUM 30int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
      PSTR szCmdLine, int iCmdShow)
    {
      static int iKeep [NUM][4] ;
      HDC hdcScr, hdcMem ;
      int cx, cy ;
      HBITMAP hBitmap ;
      HWND hwnd ;
      int i, j, x1, y1, x2, y2 ;  if (LockWindowUpdate (hwnd = GetDesktopWindow ()))//禁止窗口更新
      {
      hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;//得到屏幕dc
      hdcMem = CreateCompatibleDC (hdcScr) ;//得到与屏幕dc兼容的内存dc
      cx = GetSystemMetrics (SM_CXSCREEN) / 10 ;//得到窗口尺寸(x)的十分之一
      cy = GetSystemMetrics (SM_CYSCREEN) / 10 ;//得到窗口尺寸(y)的十分之一
      hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;//创建与屏幕dc兼容的彩色比特图画布用于在内存里面作图
        
      SelectObject (hdcMem, hBitmap) ;//设置此比特图为内存画布  srand ((int) GetCurrentTime ()) ;//随机数重排列
        
      for (i = 0 ; i < 2 ; i++)
      for (j = 0 ; j < NUM ; j++)
      {
      if (i == 0)
      {
      iKeep [j] [0] = x1 = cx * (rand () % 10) ;
      iKeep [j] [1] = y1 = cy * (rand () % 10) ;
      iKeep [j] [2] = x2 = cx * (rand () % 10) ;
      iKeep [j] [3] = y2 = cy * (rand () % 10) ;
      }
      else
      {
      x1 = iKeep [NUM - 1 - j] [0] ;
      y1 = iKeep [NUM - 1 - j] [1] ;
      x2 = iKeep [NUM - 1 - j] [2] ;
      y2 = iKeep [NUM - 1 - j] [3] ;
      }
      BitBlt (hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY) ;
      BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ;
      BitBlt (hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY) ;
    //这三句BitBle效果类似c=a;a=b;b=c;也就是两个尺寸{cx,cy}的屏幕图块内容进行交换;
        
      Sleep (10) ;//延迟10毫秒
      }//循环交换直到全屏幕图块交换过一遍
        
      DeleteDC (hdcMem) ;//SDK的dc用完要记得删除
      ReleaseDC (hwnd, hdcScr) ;//内存dc用DeleteDC,窗口dc用ReleaseDC
      DeleteObject (hBitmap) ;//比特图也要删除
        
      LockWindowUpdate (NULL) ;//解除锁定使窗口又可以正常更新
      }
      return FALSE ;
    }2.如果要窗口抖动,可以使用MoveWindow
    BOOL MoveWindow(
      HWND hWnd,      // handle to window
      int X,          // horizontal position
      int Y,          // vertical position
      int nWidth,     // width
      int nHeight,    // height
      BOOL bRepaint   // repaint option
    );3.如果需要窗口禁止而窗口内部的图像抖动/*------------------------------------------------
      SCRAMBLE.C -- Scramble (and Unscramble) Screen
      (c) Charles Petzold, 1998
      ------------------------------------------------*/
    #include <windows.h>#define NUM 30//LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
      PSTR szCmdLine, int iCmdShow)
    {
      static int iKeep [NUM][4] ;
      HDC hdcScr, hdcMem ;
      int cx, cy ;
      HBITMAP hBitmap ;
      HWND hwnd ;
      int j, x1, y1, x2, y2 ;  if (LockWindowUpdate (hwnd = GetDesktopWindow ()))
      {
      hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
      hdcMem = CreateCompatibleDC (hdcScr) ;
      cx = GetSystemMetrics (SM_CXSCREEN);
      cy = GetSystemMetrics (SM_CYSCREEN);
      hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;
        
      SelectObject (hdcMem, hBitmap) ;  srand ((int) GetCurrentTime ()) ;
        
      for (j = 0 ; j < NUM ; j++)
      {
      BitBlt (hdcScr, 0, 10, cx, cy, hdcScr, 0, 0, SRCCOPY) ;
      Sleep (10) ;
      BitBlt (hdcScr, 0, 0, cx, cy, hdcScr, 0, 10, SRCCOPY) ;//上下抖动;则把0,10,0,0,0,0,0,10,改成10,0,0,0,0,0,10,0;
      Sleep (10) ;
      }
        
      DeleteDC (hdcMem) ;
      ReleaseDC (hwnd, hdcScr) ;
      DeleteObject (hBitmap) ;
        
      LockWindowUpdate (NULL) ;
      }
      return FALSE ;
    }
      

  3.   

    刚出炉的,参考一下吧void Move()
    {
    srand((unsigned)time(NULL));
    HWND hWnd = ::AfxGetMainWnd()->GetSafeHwnd();
    RECT rcOld, rcNew;
    ::GetWindowRect(hWnd, &rcOld);
    POINT pt;
    pt.x = rcOld.left;
    pt.y = rcOld.top;
    ::ScreenToClient(hWnd, &pt);
    ::memcpy(&rcNew, &rcOld, sizeof(RECT)); int iWidth = rcOld.right-rcOld.left;
    int iHeight = rcOld.bottom-rcOld.top;
    int x = 0, y = 0;
    for (int i =0; i<80; i++)
    {
    x = rand()%80-40;
    y = rand()%80-40;
    ::MoveWindow(hWnd, rcNew.left+x, rcNew.top+y, iWidth, iHeight, TRUE);
    }
    ::MoveWindow(hWnd, rcOld.left, rcOld.top, iWidth, iHeight, TRUE);
    }
      

  4.   

    以下代码可以去掉,没用.
    //POINT pt;
    //pt.x = rcOld.left;
    //pt.y = rcOld.top;
    //::ScreenToClient(hWnd, &pt);
      

  5.   

    MoveWindow是最简单的,这个网上还有其它例子
      

  6.   

    俺还有更简单的  int top = this.Top;
                int left = this.Left;
                for (int i = 1; i <= 65; i += 5)
                {
                    this.Top= Convert.ToInt32(top + Math.Sin(i) * 5);
                    this.Left = Convert.ToInt32(left + Math.Cos(i) * 5);
                    System.Threading.Thread.Sleep(50);
                }