【问题标题】:Drawing a child window on top of another child window?在另一个子窗口之上绘制一个子窗口?
【发布时间】:2013-06-16 10:30:31
【问题描述】:

我的 win32 应用中有不同的屏幕用于不同的目的,每个屏幕都有不同的背景。主窗口有自己的静态背景。 我正在使用以下代码在自定义窗口上绘制屏幕背景。

panelBackground = LoadBitmap ( hInstance, MAKEINTRESOURCE ( bitmap ) );

WNDCLASSEX wincl;

wincl.hInstance         = hInstance;
wincl.lpszClassName     = "jPanel";
wincl.lpfnWndProc       = WndProc;
wincl.style             = CS_BYTEALIGNWINDOW;// | CS_HREDRAW | CS_VREDRAW;
wincl.cbSize            = sizeof (WNDCLASSEX);
wincl.hIcon             = 0;
wincl.hIconSm           = 0;
wincl.hCursor           = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName      = NULL;
wincl.cbClsExtra        = 0;
wincl.cbWndExtra        = 4;
wincl.hbrBackground     = ( HBRUSH ) GetStockObject( BLACK_BRUSH );

RegisterClassEx (&wincl);

hwnd = CreateWindowEx ( 0, "jPanel", txt.c_str(), WS_CLIPSIBLINGS | WS_CHILD, x, y, width, height, parent, 0, hInstance, 0) ;

SetWindowLong( hwnd , 0 , ( LONG ) this ) ;

SetWindowPos ( hwnd , HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE ) ;

然后在 WndProc 中:

view::jPanel* panel = ( view::jPanel* ) GetWindowLong( hwnd , 0 ) ;

case WM_PAINT:
  {
     if ( ! panel )
        return 0 ;

     HDC hdc ;
     PAINTSTRUCT ps ;
     RECT rect ;

     GetClientRect( hwnd , &rect ) ;

      hdc = BeginPaint( hwnd , &ps ) ;


         HDC dcSkin = CreateCompatibleDC ( hdc );                               // memory dc for skin

         HDC hMemDc = CreateCompatibleDC ( hdc );                               // memory dc for painting


         HBITMAP hmemBmp = CreateCompatibleBitmap ( hdc, rect.right - rect.left, rect.bottom - rect.top  );     // Create bitmap to draw on


         HBITMAP hOldMemBmp = (HBITMAP)SelectObject ( hMemDc, hmemBmp );        // select memory bitmap in memory dc

         HBITMAP hOldSkinBmp = (HBITMAP)SelectObject ( dcSkin, panel->panelBackground );    //select skin bitmap in skin memory dc


            BitBlt ( hMemDc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, dcSkin, 0, 0, SRCCOPY );     // Paint Skin on Memory DC
            BitBlt ( hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, hMemDc, 0, 0, SRCCOPY );            // Paint Skin on Window DC


         //<<<... DeleteDC will leak memory if it holds a resource, so lets select the old bitmap back in the memory DCs
         SelectObject ( hMemDc, hOldMemBmp );                                   // select old bitmaps back to their respective DCs before deleting
         SelectObject ( dcSkin, hOldSkinBmp );                                  // select old bitmaps back to their respective DCs before deleting


         DeleteObject ( hOldSkinBmp );
         DeleteObject ( hOldMemBmp );
         DeleteObject(  hmemBmp );
         DeleteDC ( hMemDc );
         DeleteDC ( dcSkin );


      EndPaint( hwnd , &ps ) ;
  }
  break ;

问题: 属于该屏幕的子窗口(按钮、编辑控件)不显示,我猜 它们位于屏幕背景窗口下方。使用 WS_EX_TOPMOST 样式将控件向上移动 z 顺序不起作用,也无法使用 SetWindowPos ( hwnd , HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE ) 移动 z 顺序底部的屏幕背景窗口;有效。

是的,按钮是 BS_BITMAP 样式的。

【问题讨论】:

    标签: c++ winapi


    【解决方案1】:

    在创建父窗口时添加WS_CLIPCHILDREN 样式。

    【讨论】:

    • 是的,我做到了,屏幕背景窗口不是按钮和编辑的父级,而是它们的兄弟,它们的父级都具有 WS_CLIPCHILDREN 样式。
    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多