Use of the Windows layer window

zhaozj2021-02-08  253

Windows starts from Windows2000, providing a window style called Layered, by using this window style, we can create transparent windows or translucent windows, here I will use a relatively simple example to explain how to use this New window style.

First we explain the support of the Windows API to the layer window. When you create a window, you can use CREATEWINDOWEX to create a layer window, or use setWindowlong () after creating a normal window. Windows layer window window style is WS_EX_LAYERED, and created a layer window You will also create a transparent window because the layer window will set the style of the transparent window at the same time.

After we created a layer window, we can call API setLayeredWindowAttributes () to set the properties of the layer window, this API function is like this.

BOOL SetLayeredWindowAttributes (HWND hwnd, // handle to the layered window COLORREF crKey, // specifies the color key BYTE bAlpha, // value for the blend function DWORD dwFlags // action); The main function of this function is to set the layer window missing The province's properties, the first parameter hwnd is the handle of the layer window we want to set, the second parameter crkey is a key value we want to specify, this value is valid when the 4th parameter is set to LWA_COLORKEY, this The main purpose of the parameters is to filter out the colors specified here. For example, we are set to white here, then when we draw on the window, all white will be filtered out, so drawing white places. Transparent. The third parameter Balpha is the use of the Blend function we specified, this parameter is valid when the fourth parameter is set to LWA_ALPHA. If this parameter is set, we can create a translucent window effect. When using the ColorKey method, we can get a transparent window, and in this case, all transparent places do not accept mouse messages, and the mouse message will be sent directly to the window below the layer window.

About the other more important API of the layer window is UpdateLayeredWindow (), the prototype of this function is as follows: BOOL UpdateLayeredWindow (hwnd hwnd, // handle to layered window hdc hdcdst, // handle to screen dc point * pPtdst, // new screen position sIZE * psize, // new size of the layered window HDC hdcSrc, // handle to surface DC POINT * pptSrc, // layer position COLORREF crKey, // color key BLENDFUNCTION * pblend, // blend function DWORD dwFlags // options) This function seems to have a lot of parameters, in fact, the Bitblt function is almost, but it is used on the layer window, where crKey and setLayeredWindowAttribute are the same, Pblelend is a structure, which is also very simple, here is not introduced

ColorKey and Alpha methods can only have a valid, so dwflags is used to determine this, and setlayeredWindowAttributes is not the same. This parameter has three options to use ULW_ALPHA, ULW_COLORKEY, ULW_OPAQUE, the first two functions, the third One is specified that this window is opaque.

Another difference between setlayeredWindatatributes and UpDateLayeredWindow is setlayeredWindowAttributes that is called once after the window is created, and UpdateLayeredWindow is used to handle Windows Paint events.

In this example, I created a clock program, which is transparent in addition to the scale and 3 pointers other than the dial, and in general, it will not affect your normal application operation. This example uses ATL, which needs to support the SDK of Windows2000 to compile.

#define WINVER 0x0500 # define _WIN32_WINNT 0x0500 # define _WIN32_IE 0x0501 # define _RICHEDIT_VER 0x0200 #include extern CComModule _Module; #include #include "stdatl.h" #include #include #include #define PI 3.1415926 # define ID_CLOCKTIMER 101CComModule _Module; class CClockWindow: public CWindowImpl > {BEGIN_MSG_MAP (CClockWindow) MESSAGE_HANDLER (WM_TIMER, OnTimer) MESSAGE_HANDLER (WM_CREATE, OnCreate) MESSAGE_HANDLER (WM_PAINT, OnPaint) MESSAGE_HANDLER (WM_DESTROY, OnDestroy) MESSAGE_HANDLER (WM_NCHITTEST, OnNcHitTest) END_MSG_MAP () LRESULT OnNcHitTest (UINT, WPARAM, LPARAM, BOOL &) {return HTCAPTION;} LRESULT OnPaint (UINT, WPARAM, LPARAM, BOOL &) {PAINTSTRUCT ps; HDC hDC = BeginPaint (& ps); RECT rc; GetWindowRect (& rc); SetMapMode (hDC, MM_ISOTROPIC); SetWindowExtEx (hDC, 1000, 1000, NULL); SetViewportOrgEx (hDC, (rc .right - rc.left / 2, (rc.bottom - Rc.top/2, null; setViewPortextex (HDC, RC.right - rc.left, - (rc.bottom - rc.top), null; DrawClock (HDC); Endpaint (& PS); Return 0;} LRESULT OnDestroy (UINT, WPARAM, LPARAM, BOOL &) {PostQuitMessage (0); return 0;} LRESULT OnCreate (UINT, WPARAM, LPARAM, BOOL &) {SetTimer (ID_CLOCKTIMER, 500); return 0;} LRESULT OnTimer (UINT, WPARAM , LParam, Bool & )-> getdc (); Rect Rc; GetWindowRect (& RC); SetMapMode (HDC, MM_ISOTROPIC); SetWindowExtex (HDC, 1000, 1000, Null); SetViewportorgex (HDC, (rg.right - rc.left) / 2, (rc.bottom - rc.top) / 2, null;

SetViewPortextex (HDC, RC.right - Rc.Left, - (Rc.Bottom - rc.top), NULL); DrawClock (HDC); ReleaseDC (HDC); Return 0;} Void DrawClocksurface (HDC HDC) {Hbrush HBKBRUSH = :: Createsolidbrush (RGB (255, 255, 255)); Hbrush Holdbrush = (Hbrush) :: SelectObject (HBkbrush); :: setBkmode (hdc, transparent); :: Ellipse (HDC, -510, -510, 510, 510); int nradius = 500 - 50; int x, y; hbrush hpointbrush = :: Createsolidbrush (RGB (0, 0, 0)) ;: SelectObject (HPOINTBRUSH); :: deleteObject (hbkbrush); For (int i = 0; i <60; I ) {x = (int) (NRADIUS * SIN (((2 * i * pi) / 60)); y = (int) (NRADIUS * COS ((2 * i * pi) / 60)) ;: Ellipse (HDC, X - 10, Y - 10, X 10, Y 10);} for (int i = 0; i <12; i ) { X = (int) ((2 * i * pi) / 12)); y = (int) (NRADIUS * COS ((2 * i * pi) / 12)); :: Ellipse (HDC, X - 20, Y - 20, X 20, Y 20);} int NHOUR, NMIN, NSEC; Getcurtime (NSEC); Char Stime [20]; ZeromeMory (STIME, SIGEOF (STIME)); Sprintf (Stime, "% 02D:% 02D:% 02D" , NHOUR, NMIN, NSEC); Size SZ; :: GetTextExtentPoint (HDC, Stime, (Int) Strlen (Stime), & SZ); :: Textout (HDC, -SZ.CX / 2 - 1, -300, STIME, (int); :: SelectObject (HDC, Holdbrush); :: deleteObject (HPointBrush);} Void DrawClockPointer (HDC HDC) {Int NHOUR, NMIN, NSEC; Getcurtime (NSEC); int X1, Y1, X2, Y2; // Draw Hour Pointer HPEN HHOURPEN = :: Createpen (PS_SOLID, 20, RGB (0, 0, 0)); HPEN HOLDPEN = (HPEN) :: SelectObject (HHOURPEN); int NRADIUS = 500 - 240; x1 = (int) (NRADIUS * COS (PI / 2 - (2 * NHOUR * 5 * PI NMIN * PI / 6) / 60)); Y1 = (int) (NRADIUS * SIN PI / 2 - (2 * nrour * 5 * pi

Nmin * pi / 6) / 60); NRADIUS = 40; x2 = (int) (NRADIUS * COS (3 * pi / 2 - (2 * NHOUR * 5 * PI NMIN * PI / 6) / 60)) Y2 = (int) (NRADIUS * SIN (3 * pi / 2 - (2 * NHOUR * 5 * PI NMIN * PI / 6) / 60)); :: MoveToex (HDC, X1, Y1, NULL); :: LINETO (HDC, X2, Y2); // Draw Minute Pointer HPEN HMINPEN = :: Createpen (PS_SOLID, 12, RGB (0, 0, 0)) ;: SelectObject (HDC, HMINPEN); :: DeleteObject HHOURPEN; NRADIUS = 500 - 140; x1 = (int) (NRADIUS * COS (PI / 2 - (2 * nmin * pi) / 60)); Y1 = (int) (NRADIUS * SIN (PI / 2 - 2 * nmin * pi) / 60)); nRADIUS = 80; x2 = (int) (NRADIUS * COS (3 * pi / 2 - (2 * nmin * pi) / 60)); Y2 = (int) (NRADIUS * SIN (3 * pi / 2 - (2 * nmin * pi) / 60)) ;: MoveToex (HDC, X1, Y1, NULL); :: LINETO (HDC, X2, Y2); // Draw Second Pointer :: SelectObject (HDC, Holdpen) ;: DeleteObject (Hminpen); NRADIUS = 500 - 75; x1 = (int) (NRADIUS * COS (PI / 2 - (2 * NSEC * PI) / 60)); Y1 = (int) (NRADIUS * SIN (PI / 2 - (2 * NSEC * pi) / 60)); x2 = 0; Y2 = 0; :: MoveToex (HDC, X1, Y1, NULL); :: LineTo (hDC, x2, y2);} void DrawClock (HDC hDC) {HDC hMemDC = :: CreateCompatibleDC (hDC); RECT rc; GetWindowRect (& rc); SetMapMode (hMemDC, MM_ISOTROPIC); SetWindowExtEx (hMemDC , 1000, 1000, NULL; SetViewPortorgex (HMEMDC, (rc.right - rc.LEFT) / 2, (rc.bottom - rc.top) / 2, null; setViewPortextex (HMEMDC, RC.right - rc.left (rc.bottom - rc.top), null; hbitmap hbitmap = :: createcompatiblebitmap (HDC, rc.right - rc.left, rc.bottom - rc.top); hbitmap holdbitmap = (hbitmap) :: selectObject (HMEMDC, HBitmap); DrawClockSurface (HMEMDC); DrawClockpointer (HMEMDC); :: Bitblt (HDC, -500, -500, 1000, 1000, HMEMDC, -500, -500, SRCCopy);

SelectObject (hMemDC, hOldBitmap); :: DeleteObject (hBitmap); :: DeleteDC (hMemDC); return;} void GetCurTime (int & nHour, int & nMin, int & nSec) {struct tm * tmCurrent; time_t t; t = time (NULL ); tmCurrent = localtime (& t); nHour = tmCurrent-> tm_hour; nMin = tmCurrent-> tm_min; nSec = tmCurrent-> tm_sec; return;}}; int APIENTRY WinMain (hINSTANCE hInstance, hINSTANCE, LPSTR, int) {_Module .Init (NULL, hInstance); CClockWindow wnd; wnd.Create (NULL, CWindow :: rcDefault, _T ( "ClockToy"), WS_POPUP); //wnd.ModifyStyleEx(0,WS_EX_TRANSPARENT / * | WS_EX_LAYERED * /); CRgn Rgn; RGN.CREATEELLIPTICRGN (0, 0, 180, 180); wnd.SetWindowRgn (rgn.m_hrgn, true); wnd.SetWindowPos (HWND_TOPMOST, 0, 0, 180, 180, SWP_NOMOVE); WND.MOVEWINDOW (100, 100 , 180, 180, true) ;: setLayeredWindowAttributes (wnd.m_hwnd, RGB (255, 255, 255), 0, LWA_COLORKEY); wnd.centerWindow (); wnd.showwindow (sw_show); wnd.UpdateWindow (); msg Msg; WHILE (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG); } _Module.Term (); return (int) msg.wparam; contact me at

YOUNKER@yeah.net

转载请注明原文地址:https://www.9cbs.com/read-1162.html

New Post(0)