创建异形窗体:
1:
窗口背景画刷设置为空画刷
2:
创建窗口 风格为WS_POPUP 扩展风格设置为WS_EX_TOPMOST 和WS_EX_LAYERED
3:WM_CREATE消息中设置透明颜色(Set)
4: WM_PAINT 绘制背景位图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 BITMAP g_hBitmap; 2 // 加载位图 只能加载24位真彩以下的位图 3 g_hBitmap = NULL; 4 g_hBitmap = (HBITMAP)LoadImage(NULL, _T("F:\Shape.bmp"), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE); 5 if (g_hBitmap == NULL) 6 { 7 MessageBox(NULL, _T("加载位图失败"), _T("Error"), MB_ICONERROR); 8 return FALSE; 9 } 10 11 //获取图片的头信息 文件的大小 尺寸 and so on 12 BITMAP bm; 13 GetObject(g_hBitmap, sizeof(bm), &bm); 14 hWnd = ::CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED, szWindowClass, szTitle, WS_POPUP, 640, 300, bm.bmWidth, bm.bmHeight,NULL,NULL, 15 hInstance,NULL); 16 17 if (!hWnd) 18 { 19 return FALSE; 20 } 21 22 23 24 25 26 case WM_CREATE: 27 {// 28 29 COLORREF clrTransparent = RGB(0,0,0); 30 SetLayeredWindowAttributes(hWnd, clrTransparent, 0, LWA_COLORKEY); 31 32 break; 33 } 34 case WM_PAINT: 35 { 36 hdc = BeginPaint(hWnd, &ps); 37 // TODO: 在此添加任意绘图代码... 38 // 获取窗口客户区大小 39 RECT rcClient; 40 GetClientRect(hWnd, &rcClient); 41 // 创建一个与窗口兼容的内存DC 42 HDC hMenDC = CreateCompatibleDC(hdc); 43 44 SelectObject(hMenDC, g_hBitmap); 45 BitBlt(hdc, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hMenDC, 0, 0, SRCCOPY); 46 47 DeleteDC(hMenDC); 48 EndPaint(hWnd, &ps); 49 50 } 51 break; 52 case WM_LBUTTONDOWN: 53 54 PostMessage(hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0); 55 return TRUE; 56 57 case WM_KEYDOWN: 58 { 59 switch (wParam) 60 { 61 case VK_ESCAPE: 62 SendMessage(hWnd, WM_DESTROY, 0, 0); 63 return TRUE; 64 } 65 66 } 67 break; 68 69 70 71 72 73 74 75 76 77 ATOM MyRegisterClass(HINSTANCE hInstance) 78 { 79 WNDCLASSEX wcex; 80 81 wcex.cbSize = sizeof(WNDCLASSEX); 82 83 wcex.style = CS_HREDRAW | CS_VREDRAW; 84 wcex.lpfnWndProc = WndProc; 85 wcex.cbClsExtra = 0; 86 wcex.cbWndExtra = 0; 87 wcex.hInstance = hInstance; 88 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SHAPEDEMO)); 89 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 90 wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);// (COLOR_WINDOW + 1);//背景画刷 91 wcex.lpszMenuName = NULL;//菜单 92 wcex.lpszClassName = szWindowClass; 93 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 94 95 return RegisterClassEx(&wcex); 96 }