編譯器:DEV-C++

test.cpp

#include "test.h"

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char		szClassName[ ] = "WindowsApp";
HINSTANCE	hInst;
HDC		hdc, hdcMem;


int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH)CreateSolidBrush( RGB(0,0,0) );

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_POPUP, 			/* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           640,                 /* The programs width */
           480,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

	hInst = hThisInstance;
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd,GWL_EXSTYLE)|WS_EX_LAYERED);
	HINSTANCE hD = LoadLibrary("User32.DLL");
	if(hD)
	{ 
		typedef BOOL (WINAPI  *MYFUNC)(HWND, COLORREF, BYTE, DWORD); 
		MYFUNC fun = NULL;
		
		fun = (MYFUNC)GetProcAddress(hD, "SetLayeredWindowAttributes");     
		
		if(fun)
			fun(hwnd, RGB(0,0,0), 125, 1);
		
		FreeLibrary(hD);
	}
	
	/* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
		{
			CreateWindowEx(0,
						"BUTTON",
						"CLOSE",
						WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
						142,
						88,
						38,
						38,
						hwnd,
						(HMENU)mCLOSE,
						hInst,
						0);
			break;
		}
		case WM_PAINT:
		{
			PAINTSTRUCT		ps;
			HBITMAP			hBmp = (HBITMAP)LoadImage(hInst,
									MAKEINTRESOURCE(IDB_BK),
									IMAGE_BITMAP,
									640,
									480,
									0);
			if( hBmp == NULL)
			{
				MessageBox( NULL,
				"No test.bmp",
				"Error",
				MB_OK | MB_ICONERROR );
				SendMessage(hwnd, WM_DESTROY, 0, 0);
				break;
			}
			
			hdc = BeginPaint( hwnd, &ps );
			hdcMem = CreateCompatibleDC(hdc);
			
			BITMAP				bm;

			SelectObject(hdcMem, hBmp);
			GetObject(hBmp, sizeof(bm), &bm);
			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCPAINT);
			
			DeleteDC(hdcMem);
			EndPaint(hwnd, &ps);
			break;
		}
		case WM_DRAWITEM:
		{
			LPDRAWITEMSTRUCT	lpDIS = (LPDRAWITEMSTRUCT)lParam;
			HBITMAP				hBtn = (HBITMAP)LoadImage(hInst,
										MAKEINTRESOURCE(IDB_BTN),
										IMAGE_BITMAP,
										38,
										38,
										0);
			if( hBtn == NULL)
			{
				MessageBox( NULL,
				"No close.bmp",
				"Error",
				MB_OK | MB_ICONERROR );
				SendMessage(hwnd, WM_DESTROY, 0, 0);
				break;
			}
			
			hdcMem = CreateCompatibleDC(hdc);
			
			SelectObject(hdcMem, hBtn);
			BitBlt(hdc, 0, 0, 38, 38, hdcMem, 0, 0, SRCCOPY);
			
			DeleteDC(hdcMem);
			
			break;
		}
		case WM_COMMAND:
        {
            switch( LOWORD(wParam) )
            {
                case mCLOSE:
                {
					SendMessage(hwnd, WM_DESTROY, 0, 0);
					break;
				}
			}
			break;
		}	//	WM_COMMAND
		case WM_LBUTTONDOWN:
		{
			SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
			break;
		}
		case WM_DESTROY:
		{
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        }
		default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

test.h

#include <windows.h>

#define			IDB_BK					1
#define			mCLOSE					2
#define			IDB_BTN					3

test.rc

有問題的話,把檔案路徑改成絕對路徑

#include "test.h"

IDB_BK				BITMAP	DISCARDABLE		"test.bmp"
IDB_BTN				BITMAP	DISCARDABLE		"close.bmp"

效果如下


原本要放影片

但是作罷