| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 619 人关注过本帖
标题:c语言 gdi+ 透明按钮怎么弄?
只看楼主 加入收藏
badwuxuxing
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2011-11-28
收藏
 问题点数:0 回复次数:0 
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
图片附件: 游客没有浏览图片的权限,请 登录注册



为什么左上角的透明按钮的背景色是白色的啊? 弄了好久,搞不定了,跪求大神帮忙解答! 不胜感激!

搜索更多相关主题的帖子: comment c语言 
2013-06-16 18:49
快速回复:c语言 gdi+ 透明按钮怎么弄?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021320 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved