首页 > 代码库 > 5.静态控件背景透明化
5.静态控件背景透明化
一、静态控件透明化
前面我们介绍过,通过WM_CTLCOLORDLG消息来设置对话框背景,还有消息来设置其他控件得背景:
WM_CTLCOLORBTN - 按钮
WM_CTLCOLOREDIT - 编辑框
WM_CTLCOLORLISTBOX - 列表框
WM_CTLCOLORSCROLLBAR - 滚动条
WM_CTLCOLORSTATIC - 静态框
二、具体事例 - 静态框
通过在WM_CTLCOLORSTATIC消息中,返回一个空画刷达到背景透明化的要求。
设置背景透明:
int SetBkMode ( HDC hdc, int iBkMode );
代码:
1 #include "stdafx.h" 2 #include "05静态控件背景透明化.h" 3 4 WCHAR szDlgTitle[] = L"静态控件背景透明化"; 5 6 BOOL CALLBACK DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 7 { 8 RECT rcDialog; 9 HBITMAP hBitmap; 10 static BITMAP s_bm; 11 static HDC s_hdcMem; 12 switch (message) 13 { 14 case WM_INITDIALOG: 15 SetWindowText(hWnd, szDlgTitle); 16 SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) | WS_SIZEBOX); 17 // 加载背影图片 18 hBitmap = (HBITMAP)LoadImage(NULL, L"006.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); 19 if (hBitmap == NULL) 20 { 21 MessageBox(hWnd, L"LoadImage failed", L"Error", MB_ICONERROR); 22 exit(0); 23 } 24 else 25 { 26 // 将背影图片放入HDC - s_hdcMem 27 HDC hdc; 28 hdc = GetDC(hWnd); 29 s_hdcMem = CreateCompatibleDC(hdc); 30 SelectObject(s_hdcMem, hBitmap); 31 ReleaseDC(hWnd, hdc); 32 // 得到位图信息 33 GetObject(hBitmap, sizeof(s_bm), &s_bm); 34 } 35 return 0; 36 case WM_COMMAND: 37 if (IDCANCEL == LOWORD(wParam)) 38 { 39 DeleteDC(s_hdcMem); 40 EndDialog(hWnd, LOWORD(wParam)); 41 return TRUE; 42 } 43 break; 44 case WM_SIZE: 45 InvalidateRect(hWnd, NULL, TRUE); 46 return TRUE; 47 case WM_CTLCOLORSTATIC: 48 SetBkMode((HDC)wParam, TRANSPARENT); 49 return (BOOL)((HBRUSH)GetStockObject(NULL_BRUSH)); 50 case WM_CTLCOLORDLG: 51 GetClientRect(hWnd, &rcDialog); 52 //通过SetStretchBltMode的设置能使StretchBlt在缩放图像更加清晰 53 SetStretchBltMode((HDC)wParam, COLORONCOLOR); 54 StretchBlt((HDC)wParam, 0, 0, rcDialog.right, rcDialog.bottom, s_hdcMem, 0, 0, s_bm.bmWidth, s_bm.bmHeight, SRCCOPY); 55 return (BOOL)((HBRUSH)GetStockObject(NULL_BRUSH)); 56 } 57 return FALSE; 58 } 59 60 int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) 61 { 62 DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc); 63 return 0; 64 }
运行结果:
未处理WM_CTLCOLORSTATIC消息 处理WM_CTLCOLORSTATIC消息
三、继续优化
静态框的背景已经透明化了,但是组合框的文字是被白色得边框划过的,很不美观。要解决这个问题,我们用位图画刷,在WM_CTLCOLORSTATIC消息中返回一个位图画刷。
代码:
1 #include "stdafx.h" 2 #include "05静态控件背景透明化.h" 3 4 WCHAR szDlgTitle[] = L"静态控件背景透明化"; 5 6 BOOL CALLBACK DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 7 { 8 static HBRUSH s_hBitmapBrush; //位图画刷 9 switch (message) 10 { 11 case WM_INITDIALOG: 12 SetWindowText(hWnd, szDlgTitle); 13 SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) | WS_SIZEBOX); 14 // 加载背影图片 15 HBITMAP hBitmap; 16 hBitmap = (HBITMAP)LoadImage(NULL, L"006.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); 17 if (hBitmap == NULL) 18 { 19 MessageBox(hWnd, L"LoadImage failed", L"Error", MB_ICONERROR); 20 exit(0); 21 } 22 // 创建位图画刷 23 s_hBitmapBrush = CreatePatternBrush(hBitmap); 24 return 0; 25 case WM_COMMAND: 26 if (IDCANCEL == LOWORD(wParam)) 27 { 28 DeleteObject(s_hBitmapBrush); 29 EndDialog(hWnd, LOWORD(wParam)); 30 return TRUE; 31 } 32 break; 33 case WM_CTLCOLORSTATIC: 34 SetBkMode((HDC)wParam, TRANSPARENT); 35 case WM_CTLCOLORDLG: 36 return (BOOL)s_hBitmapBrush; 37 } 38 return FALSE; 39 } 40 int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) 41 { 42 DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc); 43 return 0; 44 }
运行结果:
5.静态控件背景透明化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。