• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Opacity

Started by Patrice Terrier, October 24, 2013, 11:50:15 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

Opacity.exe is a small GDImage based utility.

Its purpose is to turn a graphic file into a transparent PNG file, that can be rendered in composited mode onto a DirectDraw surface.

The alpha channel is created on the fly based on the analyze of the current pixel colors.
To see the image in its original state, display it over a black background. 

PowerBASIC code source:

#COMPILE EXE "Opacity.exe"
#INCLUDE "GDIMAGE.INC"

DECLARE FUNCTION MessageBox LIB "USER32.DLL" ALIAS "MessageBoxA" (BYVAL hWnd AS DWORD, lpText AS ASCIIZ, lpCaption AS ASCIIZ, BYVAL dwType AS DWORD) AS LONG

FUNCTION WinMain (BYVAL hInstance     AS LONG, _
                  BYVAL hPrevInstance AS LONG, _
                  BYVAL lpCmdLine     AS ASCIIZ PTR, _
                  BYVAL iCmdShow      AS LONG) AS LONG

    LOCAL zIn, zOut AS ASCIIZ * 260

    zIn = LCASE$(command$)
    zOut = PATHNAME$(PATH, zIn) + "_" + PATHNAME$(NAMEX, zIn)

    IF RIGHT$(zIn, 4) = ".png" THEN
       IF ZI_CreateVariableOpacityPNG(zIn, zOut) = 0 THEN ' Means no error
          CALL MessageBox(0, "Alpha channel changed successfuly!", "Opacity", 0)
       END IF
    END IF

END FUNCTION


Usage:
Perform image drag and drop from the Explorer onto Opacity.exe.

Binary code is attached to this post
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#1
And here is a small GDImage 7.00 C++ 64-bit project, using an image based on variable opacity to render it as a layered window onto the OS DirectDraw surface (must run in AERO mode).

This is perfect to create amazing games, altogether with a video playing in the background.

To close the window press on the Escape key or ALT+F4.

Note: the Spinner animations are based on the same principle.

Here is the C++ code of HUDwin window:
// HelloWin.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

#include "stdafx.h"
#include <windows.h>
#include <Commctrl.h>
#include <vector>

#pragma comment(lib, "D:\\VS2010\\GDImage64.lib")
#pragma comment(lib, "D:\\VS2010\\GDImage32.lib")
#include "GDImage.h"

#include "Utility.h"

LONG_PTR ghImg;

void RenderWindow(IN HWND hWnd, IN LONG_PTR hImg) {
    if (hImg) {
        BLENDFUNCTION bf = {0};
        RECT rw; GetWindowRect(hWnd, &rw);
        SIZEL lpSize; lpSize.cx = rw.right - rw.left; lpSize.cy = rw.bottom - rw.top;
        POINT lp; lp.x = rw.left; lp.y = rw.top;
        POINT ptSrc = {0};
        HDC hParentDC = GetDC(GetParent(hWnd));
        HDC hMemDC = CreateCompatibleDC(hParentDC);
        HBITMAP hBmp = CreateCompatibleBitmap(hParentDC, lpSize.cx, lpSize.cy);
        if (hMemDC) {
            SelectObject(hMemDC, hBmp);
            LONG_PTR graphics = 0;
            if (GdipCreateFromHDC(hMemDC, graphics) == 0) {
                GdipDrawImageRectRectI(graphics, hImg, 0, 0, lpSize.cx, lpSize.cy, 0, 0, lpSize.cx, lpSize.cy, 2, 0, NULL, NULL);
                GdipDeleteGraphics(graphics);
            }
            bf.BlendOp             = AC_SRC_OVER;
            bf.BlendFlags          = 0;
            bf.AlphaFormat         = AC_SRC_ALPHA; // Use source alpha
            bf.SourceConstantAlpha = 255;          //alpha
            UpdateLayeredWindow (hWnd, hParentDC, &lp, &lpSize, hMemDC, &ptSrc, 0, &bf, ULW_ALPHA);
            DeleteObject(hBmp);
            DeleteDC(hMemDC);
        }
        ReleaseDC(GetParent(hWnd), hParentDC);
    }
}

// Main winproc
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    long wmId, wmEvent;

    switch (uMsg)
    {
    case WM_COMMAND:
         wmId    = LOWORD(wParam);
         wmEvent = HIWORD(wParam);
         return 0;

    case WM_KEYDOWN:
         if (wParam == 0x1B) { DestroyWindow(hWnd); }
         break;

    case WM_NCHITTEST:
         return HTCAPTION;

    case WM_DESTROY:
         PostQuitMessage(0);
         if (ghImg) { GdipDisposeImage(ghImg); ghImg = 0; }
         return 0;
    }
   
    return DefWindowProc(hWnd, uMsg, wParam, lParam);


int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {

    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // Declaration of local variables
    long nRet = 0, IsInitialized = 0;
    WNDCLASSEX wcx; MSG msg;
    WCHAR szWindowClass[] = L"ZHUDWIN";
    WCHAR szString[MAX_PATH];
    if (sizeof(LONG_PTR) == 8) {
        wcscpy_s(szString, L"HUDwin-64 Template"); }
    else {
        wcscpy_s(szString, L"HUDwin-32 Template");
    }
    // Register the main window class
    wcx.cbSize = sizeof(WNDCLASSEX);
    IsInitialized = GetClassInfoEx(hInstance, szWindowClass, &wcx);
    if (!IsInitialized)
    {
        wcx.style         = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc   = WndProc;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hInstance     = hInstance;
        wcx.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wcx.lpszMenuName  = NULL;
        wcx.lpszClassName = szWindowClass;
        wcx.hIconSm       = LoadIcon(wcx.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        if (RegisterClassEx(&wcx)) { IsInitialized = -1; }
     }

     if (IsInitialized) { 
         DWORD dwStyleEx = WS_EX_LAYERED; // | WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
         DWORD dwStyle = WS_POPUP | WS_VISIBLE;
         HWND hParent = GetDesktopWindow();
         HWND hWnd = CreateWindowEx(dwStyleEx, szWindowClass, szString, dwStyle,
                                    // Size and position
                                    0, 0, 0, 0,
                                    hParent,       // Parent window   
                                    NULL,       // Menu
                                    hInstance,  // Instance handle
                                    NULL        // Additional application data
                                    );
         if (hWnd) {

             ZI_LoadDLL(L""); // Load GDImage.dll
             Load_GDIPLUS();
             wstring sExePath;
             WCHAR exepath[MAX_PATH];
             if (GetModuleFileName(0, exepath, sizeof(exepath))) {
                 sExePath = exepath; sExePath = sExePath.substr( 0, sExePath.rfind(L"\\")); sExePath += L"\\";
             }
             wstring sResource = sExePath;  sResource += L"Resource\\sinanju_hud.png";
             sResource = sExePath;  sResource += L"Resource\\technomage.png";
             long imgW = 0, imgH = 0;
             LONG_PTR img = 0;
             ghImg = zLoadImageFromFile ((WCHAR*) sResource.c_str(), imgW, imgH, 0); // GDImage load image from file
             if (ghImg) {
                 RECT rw;
                 GetWindowRect(hParent, &rw);
                 MoveWindow(hWnd, rw.left + ((rw.right - rw.left - imgW) / 2), rw.top + ((rw.bottom - rw.top - imgH) / 2), imgW, imgH, 0);

                 RenderWindow(hWnd, ghImg);
                 GdipDisposeImage(ghImg); ghImg = 0; }
             else {
                 DestroyWindow(hWnd);
             }

             UpdateWindow(hWnd);
   
             // Main message loop
             while (GetMessage(&msg, NULL, 0, 0)) {
                 TranslateMessage(&msg);
                 DispatchMessage(&msg);
             }
             nRet = (long) msg.wParam;
         }
     }
     return nRet;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#2
The GDImage Of The Bay project would show you how to animate child controls on a layered window.
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com