Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: WinAPI и системное программирование > LayeredWindow


Автор: Graf_h 24.2.2009, 17:57
Здравтсвуйте! Может кто знает как использовать LayeredWindow в Делфи?

Автор: Snowy 24.2.2009, 18:23
Цитата
Layered Windows
Using a layered window can significantly improve performance and visual effects for a window that has a complex shape, animates its shape, or wishes to use alpha blending effects. The system automatically composes and repaints layered windows and the windows of underlying applications. As a result, layered windows are rendered smoothly, without the flickering typical of complex window regions. In addition, layered windows can be partially translucent, that is, alpha-blended.

To create a layered window, specify the WS_EX_LAYERED extended window style when calling the CreateWindowEx function, or call the SetWindowLong function to set WS_EX_LAYERED after the window has been created. After the CreateWindowEx call, the layered window will not become visible until the SetLayeredWindowAttributes or UpdateLayeredWindow function has been called for this window. Note that WS_EX_LAYERED cannot be used for child windows.

To set the opacity level or the transparency color key for a given layered window, call SetLayeredWindowAttributes. After the call, the system may still ask the window to paint when the window is shown or resized. However, because the system stores the image of a layered window, the system will not ask the window to paint if parts of it are revealed as a result of relative window moves on the desktop. Legacy applications do not need to restructure their painting code if they want to add translucency or transparency effects for a window, because the system redirects the painting of windows that called SetLayeredWindowAttributes into off-screen memory and recomposes it to achieve the desired effect.

For faster and more efficient animation or if per-pixel alpha is needed, call UpdateLayeredWindow. UpdateLayeredWindow should be used primarily when the application must directly supply the shape and content of a layered window, without using the redirection mechanism the system provides through SetLayeredWindowAttributes. In addition, using UpdateLayeredWindow directly uses memory more efficiently, because the system does not need the additional memory required for storing the image of the redirected window. For maximum efficiency in animating windows, call UpdateLayeredWindow to change the position and the size of a layered window. Please note that after SetLayeredWindowAttributes has been called, subsequent UpdateLayeredWindow calls will fail until the layering style bit is cleared and set again.

Hit testing of a layered window is based on the shape and transparency of the window. This means that the areas of the window that are color-keyed or whose alpha value is zero will let the mouse messages through. However, if the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to other windows underneath the layered window.

Код
  SetWindowLong(wnd, GWL_EXSTYLE, GetWindowLong(wnd, GWL_EXSTYLE) and WS_EX_LAYERED);

Автор: Graf_h 24.2.2009, 18:38
Snowy,  спасибо! я так и делал:
Вот код:
Код

procedure TForm1.FormCreate(Sender: TObject);
var
col:COLORREF;
wnd:HWND;
wnd1:HWND;
begin
   col:=clWhite;
   wnd:=Form1.Handle;
   SetWindowLong(wnd, GWL_EXSTYLE, GetWindowLong(wnd, GWL_EXSTYLE) or WS_EX_LAYERED);
   SetLayeredWindowAttributes(wnd,col,150,0);
   UpdateLayeredWindow(wnd,GetDC(wnd),nil,nil,0,nil,col,0,0);
end;


И ничего:(

Автор: Rrader 24.2.2009, 19:17
Graf_h, если на то пошло, то флаг LWA_ALPHA or LWA_COLORKEY надо указать у SetLayeredWindowAttributes. А вообще, в VCL этот код уже есть (я о том, что покурить можно):
Код

Form.AlphaBlend := True;
Form.AlphaBlendValue := 250;
// еще Transparent и TransparentColor

Автор: Graf_h 24.2.2009, 19:26
Rrader, мне не нужно просто прозрачное окноsmile! Нужно отрендерить картинку на окно с прозрачным цветом(В моем случае все что залито белым цветом на форме было прзрачным(ну типа CreateRegion)). 

Автор: bems 24.2.2009, 22:28
Graf_h, ну тебе ж ткнули в TrasparentColor

Автор: AntonN 26.2.2009, 13:29
Rrader, 
Цитата

 А вообще, в VCL этот код уже есть (я о том, что покурить можно):

в D5 еще нету smile


Graf_h, нужно получить типа вот этого (и ниже)? http://forum.vingrad.ru/forum/topic-194182/anchor-entry1400262/0.html

Автор: Graf_h 27.2.2009, 13:33
AntonN,  вот  код  чисто на WIN API
Код
  
     const
      Alpha: array [Boolean] of Integer = (0, LWA_ALPHA);
      ColorKey: array [Boolean] of Integer = (0, LWA_COLORKEY);
      .....
      var
      TranspColValue:COLORREF;

      ..... 
      TranspColValue:=clWhite;
      SetWindowLong(Form1.Handle, GWL_EXSTYLE, GetWindowLong(wnd, GWL_EXSTYLE) or WS_EX_LAYERED);
      SetLayeredWindowAttributes(Form1.Handle, TranspColValue, FAlphaBlendValue, Alpha[1]  or ColorKey[1]);



Разобрался! Но заметил один недостаток: в итоге получаются потрепаные края формочки(похоже как при СreateRegion). Можно как то это сгладить? 


AntonN cпасибо, скачал примерчик. Создается маска и UpdateLayeredWindow smile  

Автор: Snowy 27.2.2009, 18:17
Рваные края - из-за картинки.
Обычно фотошоп любит сглаживать линии.
Из-за этого линии получаются не чёткими, но более ровными.
То есть часть цвета линии немного разбавляется на соседнии пиксели.
В результате чего они уже не имеют чистого цвета и прозрачность на них не действует.
Вот эти пиксели и образуют рваные края.
Нужно поправить картинку в менее умном редакторе, типа паинта.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)