#include
#include
#include
//#include "resource.h"
//#include
#include
#define TWO_PI (2.0*3.1415926)
#define WS_EX_LAYERED 0x00080000
#define LWA_COLORKEY 0x00000001
LRESULT CALLBACK MyWindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
void DrawRectangle(HWND hwnd);
int cxClient,cyClient;
typedef
BOOL (WINAPI *LP_SetLayeredWindowAttributes)(
HWND hwnd, // handle to the layered window
COLORREF crKey, // specifies the color key
BYTE bAlpha, // value for the blend function
DWORD dwFlags // action
);//注意添加WINAPI,定义成C++约定,否则报错崩溃
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
static TCHAR MyAppName[]=TEXT("My first windows app");
//设置窗口背景画刷为图片画刷,再指定透明颜色即可以创建透明区域。
HBITMAP hBitmap;
hBitmap = (HBITMAP)LoadImage(NULL, TEXT("Kitty.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL)
{
MessageBox(NULL, TEXT("位图加载失败"), TEXT("Error"), MB_ICONERROR);
return 0;
}
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=MyWindowProc;
wndclass.cbClsExtra=0;//预留的额外空间
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=::LoadIcon(NULL,IDI_INFORMATION);
wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);//::LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
wndclass.hbrBackground=CreatePatternBrush(hBitmap);//(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=TEXT("My first windows app");
if (!RegisterClass(&wndclass))
{
int i=GetLastError();
//char str[25]={0};
wchar_t chResult[128]={0};
_itow(i,chResult,10);
//sprintf(str,TEXT("%d"),i);
//sprintf(buffer,L"Key State = 0X%X ",key_state);
MessageBox(NULL,chResult,TEXT("Error"),MB_OK);
//AfxMessageBox(TEXT("Error") );
}
//hwnd=CreateWindow( MyAppName,
//TEXT("My first windows"),
//WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
//CW_USEDEFAULT ,
//CW_USEDEFAULT,
//CW_USEDEFAULT,
//CW_USEDEFAULT,
//NULL,NULL,
//hInstance,
//NULL
//);
BITMAP bm;
GetObject(hBitmap, sizeof(bm), &bm);
hwnd = CreateWindowEx(WS_EX_TOPMOST,
MyAppName,
MyAppName,
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
bm.bmWidth,
bm.bmHeight,
NULL,
NULL,
hInstance,
NULL);
//ShowWindow(hwnd,SW_SHOWNORMAL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
DrawRectangle(hwnd);
}
return msg.wParam;
}
LRESULT CALLBACK MyWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HRGN hRgnClip,hRgnClip1;
//static int cxClient,cyClient;
double fAngle,fRadius;
HCURSOR hCursor;
HDC hdc;
HRGN hRgnTem[6];
int i;
PAINTSTRUCT ps;
RECT WindowsRect;
HFONT hFont ;
LOGFONT lf ;
HRGN rgnWnd,rgnTemp;
static HDC s_hdcMem;
static HBRUSH s_hBackBrush;
switch(uMsg)
{
case WM_CREATE:
{
// 设置分层属性
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE)|WS_EX_LAYERED);
//设置透明色
COLORREF clTransparent = RGB(0, 0, 0);
LP_SetLayeredWindowAttributes MySetLayeredWindowAttributes = (LP_SetLayeredWindowAttributes)GetProcAddress(LoadLibrary(TEXT("user32.dll")),"SetLayeredWindowAttributes");
MySetLayeredWindowAttributes(hwnd, clTransparent, 0, LWA_COLORKEY);
}
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE: //按下Esc键时退出
SendMessage(hwnd, WM_DESTROY, 0, 0);
return 0;
}
break;
case WM_LBUTTONDOWN: //当鼠标左键点击时可以拖曳窗口
PostMessage(hwnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
void DrawRectangle(HWND hwnd)
{
//RECT rect;
//HBRUSH hBrush;
//if (cxClient==0||cyClient==0)
//{
//return;
//}
//SetRect(&rect,rand()%cxClient,rand()%cyClient,rand()%cxClient,rand()%cyClient);
//hBrush=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
//HDC hdc=GetDC(hwnd);
//FillRect(hdc,&rect,hBrush);
//ReleaseDC(hwnd,hdc);
//DeleteObject(hBrush);
}
void GetWindowSize(HWND hwnd, int *pnWidth, int *pnHeight)
{
RECT rc;
GetWindowRect(hwnd, &rc);
*pnWidth = rc.right - rc.left;
*pnHeight = rc.bottom - rc.top;
}