c语言 gdi+ 透明按钮怎么弄?
resource.h#define BTN_TITLE 100
#define SYSBTN_CLOSE 101
main.cpp
程序代码:#include <windows.h>
#include <gdiplus.h>
#include "resource.h"
#pragma comment(lib, "gdiplus.lib")
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK SetChildFont(HWND hWnd,LPARAM lParam);
void OnPaint(HWND hWnd);
void drawitem(HWND hWnd,LPDRAWITEMSTRUCT lpds);
LPSTR AppName = TEXT("图像测试"); //窗口的名称
LPSTR SzTitle = TEXT("标题"); //窗口的标题
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
MyRegisterClass(hInstance); //注册窗口类
if(!InitInstance (hInstance, nCmdShow)){ //初始化窗口
return FALSE;
}
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg); //消息解释
DispatchMessage(&msg); //消息发送
}
Gdiplus::GdiplusShutdown(gdiplusToken);
return (int)msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_SIZEALL);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = AppName;
wc.hIconSm = LoadIcon(wc.hInstance, (LPCTSTR)IDI_APPLICATION);
return RegisterClassEx(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow( AppName, //窗口名
SzTitle, //窗口标题
WS_POPUP,
100, //窗口左上角的x坐标
100, //窗口左上角的y坐标
800, //窗口的宽度
500, //窗口的高度
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //实例句柄
NULL); //创建参数
if(!hWnd){
return FALSE;
}
ShowWindow(hWnd, nCmdShow); //显示窗口
UpdateWindow(hWnd); //立即显示
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwnd_btn_ok, hwnd_btn_cancel, hwnd_btn_tit, sys_btn_close;
switch(message)
{
case WM_CREATE:
sys_btn_close = CreateWindow(TEXT("Button"),"",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,0,0,30,21,hWnd,(HMENU)SYSBTN_CLOSE,((LPCREATESTRUCT)lParam)->hInstance,NULL);
hwnd_btn_tit = CreateWindow(TEXT("Static"),TEXT("title"),WS_CHILD|WS_VISIBLE|SS_NOTIFY,0,0,800,75,hWnd,(HMENU)BTN_TITLE,((LPCREATESTRUCT)lParam)->hInstance,NULL);
hwnd_btn_ok = CreateWindow(TEXT("Button"),TEXT("确定"),WS_CHILD|WS_VISIBLE,100,100,50,20,hWnd,(HMENU)IDOK,((LPCREATESTRUCT)lParam)->hInstance,NULL);
hwnd_btn_cancel = CreateWindow(TEXT("Button"),TEXT("取消"),WS_CHILD|WS_VISIBLE,200,100,50,20,hWnd,(HMENU)IDCANCEL,((LPCREATESTRUCT)lParam)->hInstance,NULL);
EnumChildWindows(hWnd,SetChildFont,NULL); //设置字体
return 0;
//case WM_PAINT:
//OnPaint(hWnd);
//return 0;
case WM_ERASEBKGND:
OnPaint(hWnd);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case BTN_TITLE:
PostMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(LOWORD(lParam),LOWORD(lParam))); break;
case IDOK:
PostQuitMessage(TRUE); break;
case IDCANCEL:
PostQuitMessage(FALSE); break;
}
return TRUE;
case WM_DRAWITEM:
drawitem(hWnd,(LPDRAWITEMSTRUCT)lParam);
return TRUE;
case WM_DESTROY: //关闭窗口
PostQuitMessage(0); //发送关闭消息
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam); //缺省窗口处理函数
}
return 0;
}
void OnPaint(HWND hWnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
Gdiplus::Graphics graphics(hdc);
Gdiplus::Image image(L"images/back.bmp");
graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
graphics.DrawImage(&image,0,0,800,500);
EndPaint(hWnd, &ps);
hdc = BeginPaint(GetDlgItem(hWnd,BTN_TITLE), &ps);
Gdiplus::Graphics graphics2(hdc);
Gdiplus::Image image2(L"images/head_bg.png");
graphics2.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
graphics2.DrawImage(&image2,0,0,800,75);
EndPaint(GetDlgItem(hWnd,BTN_TITLE), &ps);
}
void drawitem(HWND hWnd,LPDRAWITEMSTRUCT lpds)
{
HDC hdc = lpds->hDC;
Gdiplus::Graphics graphics(hdc);
Gdiplus::Image image(L"images/btn_sys_minimize.png");
graphics.DrawImage(&image,0,0,90,21);
}
BOOL CALLBACK SetChildFont(HWND hWnd,LPARAM lParam)
{
LOGFONT lf;
HDC hdc = GetDC(NULL);
ZeroMemory(&lf, sizeof(&lf));
lf.lfHeight = 12;
lf.lfWidth = 0 ;
lf.lfEscapement = 0 ;
lf.lfOrientation = 0 ;
lf.lfWeight = false;
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfCharSet = DEFAULT_CHARSET ;
lf.lfOutPrecision = 0 ;
lf.lfClipPrecision = 0 ;
lf.lfQuality = 0 ;
lf.lfPitchAndFamily = 0 ;
lstrcpy (lf.lfFaceName, TEXT("MS Shell Dlg"));
HFONT hFont = CreateFontIndirect(&lf);
SendMessage(hWnd,WM_SETFONT,(WPARAM)hFont,MAKELPARAM(TRUE,0));
ReleaseDC(NULL, hdc);
return TRUE;
}head_bg.png
btn_sys_minimize.png
为什么左上角的透明按钮的背景色是白色的啊? 弄了好久,搞不定了,跪求大神帮忙解答! 不胜感激!






