VC + + Interface Programming--a detailed implementation of the Shadow window

Source: Internet
Author: User

Reprint: http://blog.csdn.net/rmxming/article/details/11661365

For those of us control freaks, window shading is also an essential implementation requirement. Although it is not much use, but for the increase in the window three-dimensional sense, that is very helpful.

I implemented a shadow effect similar to the 360 interface, which supports normal windows and translucent windows.

Is the shadow window different for normal and translucent windows? And let me write slowly:)

The implementation of the Shadow window is simple: When the main window is created, a child window is created and adsorbed at the bottom of the main window. Then make a depiction with a translucent shadow effect on the subwindow.

The following code is the creation code of the Shadow window in the parent window, isn't it simple?

[HTML]View PlainCopy
    1. LRESULT OnInitDialog (UINT/*umsg*/, WPARAM/*wparam*/, LPARAM/*lparam*/, bool&/*bhandled*/)
    2. {
    3. M_shadow.create (m_hwnd);
    4. M_shadow.setshadowsize (8);
    5. return TRUE;
    6. }

Here are the steps to implement the Shadow window:

1. When the shadow window is created, only the style of the shadow window is set to ws_visible, we cannot use ws_child here, otherwise the shadow window will run into the main window.

[CPP]View PlainCopy
    1. // create shadow window.   
    2. hwnd create (const hwnd wndparent)   
    3. {  
    4.     atlassert ( ::iswindow (wndparent)  );   
    5.     M_HPARENTWND = WNDPARENT;  
    6.     CRECT RC (1, 1, 1, 1);   
    7.     return cwindowimpl<cthemedshadowwnd,  cwindow, ccontrolwintraits>::create (0, rc, null, ws_visible, null);   
    8. }  


2. When the WM_CREATE message is executed in the shadow window, modify its style to Ws_ex_layered | Ws_ex_transparent, note that both styles are needed. Ws_ex_transparent is to let the window cannot receive click messages, you always do not want your window shadow can be clicked and activated by the user:)

[CPP]View PlainCopy
    1. SetWindowLong (Gwl_exstyle, GetWindowLong (Gwl_exstyle) | ws_ex_layered | Ws_ex_transparent);
    2. ModifyStyleEx (Ws_ex_topmost, ws_ex_noactivate);

3. At the same time, the shadow window registers the parent window's message-handling callback function in order to obtain important messages such as moving, redrawing, and hiding the parent window. Because the shadow window changes as the state of the parent window changes.

[CPP]View PlainCopy
    1. Set parent window Original processing.
    2. M_oriparentproc =:: GetWindowLong (M_hparentwnd, GWL_WNDPROC);
    3. :: SetWindowLong (M_hparentwnd, GWL_WNDPROC, (LONG) parentproc);

The callback function is simple to do, cling to the parent window, like a small tail:

[CPP]View PlainCopy
  1. Get parent message.
  2. Static LRESULT CALLBACK Parentproc (hwnd hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM)
  3. {
  4. //Find the shadow window pointer via parent window handle.
  5. Atlassert (M_szshadowwindows.find (hwnd)! = M_szshadowwindows.end ());
  6. Cthemedshadowwnd *pthis = M_szshadowwindows[hwnd];
  7. WNDPROC Pdefproc = (WNDPROC) pthis->m_oriparentproc;
  8. switch (umsg)
  9. {
  10. Case WM_ERASEBKGND:
  11. Case WM_PAINT:
  12. Case Wm_move:
  13. Case Wm_activate:
  14. Case Wm_ncactivate:
  15. {
  16. if (:: IsWindowVisible (hwnd))
  17. {
  18. Pthis->adjustwindowpos ();
  19. }
  20. Break ;
  21. }
  22. Case Wm_destroy:
  23. {
  24. //Destroy the Shadow window.
  25. Pthis->destroywindow ();
  26. Break ;
  27. }
  28. Case Wm_ncdestroy:
  29. {
  30. //Remove Shadow window from map.
  31. M_szshadowwindows.erase (HWND);
  32. Break ;
  33. }
  34. Case Wm_showwindow:
  35. {
  36. //The window is being hidden
  37. if (!wparam)
  38. {
  39. Pthis->showwindow (Sw_hide);
  40. }
  41. Else
  42. {
  43. Pthis->showwindow (Sw_show);
  44. }
  45. Break ;
  46. }
  47. Default:
  48. {
  49. Break ;
  50. }
  51. }
  52. return Pdefproc (hwnd, umsg, WParam, LParam);
  53. }

OK, window message mechanism processing finished, we have to deal with the shadow painting, I use the drawing of GDI +, if there are children shoes feel good enough, you can try to change the parameter configuration to achieve the desired effect:

[CPP]View PlainCopy
  1. Create Shadow Brush.
  2. PathGradientBrush Brshadow (M_shadowpath.m_ppath);
  3. Color Clrshadow[3] = {color::transparent, color (255, 0, 0, 0), color (255, 0, 0, 0)};
  4. int ncount = 3;
  5. REAL Szpos[3] = {0.0F, 0.05F, 1.0F};
  6. Brshadow.setinterpolationcolors (Clrshadow, Szpos, ncount);
  7. Draw Shadow.
  8. Rcshadow.width = Rcshadow.width-m_nshadowsize-m_nblankarea;
  9. Rcshadow.height = Rcshadow.height-m_nshadowsize-m_nblankarea;
  10. Graphics. Excludeclip (Rcshadow);
  11. Graphics. FillPath (&brshadow, M_shadowpath.m_ppath);

Notice that I've ruled out a part of the shaded section, which is made for transparent windows, excluded as follows, the shadow window appears only in the lower-right corner of the rectangle, and the rest is transparent.


If I do not rule out part of the shaded area, then the transparent window effect will become very difficult to see, for example, the transparent background is obscured by shadows, which is obviously not in line with the aesthetic requirements.

If the corner of your window is elliptical, you may also need to widen the display area of the shadow, and you can use the following function to increase the width of the shadow:

[CPP]View PlainCopy
    1. Set blank area right position.
    2. void Setrightoffsetarea (const int nrightpos)
    3. {
    4. M_nblankarea = Nrightpos;
    5. if (Nrightpos < 0)
    6. {
    7. M_nblankarea = 1;
    8. }
    9. }


Shadow window Free instance code download: http://download.csdn.net/detail/renstarone/6267677

VC + + Interface Programming--a detailed implementation of the shadow window

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.