创建自定义按钮 [英] Creating custom buttons

查看:110
本文介绍了创建自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问如何将自己选择的图像设为按钮。例如像磁盘图像

Please how do I make an image of my own choosing to be a button.Like a disket image for instance

推荐答案

你可能看看这个 CodeProject 的文章:普通C中的所有者绘制图标按钮(无MFC) [ ^ ]。
You might have a look at this CodeProject's article: Owner-draw icon buttons in plain C (no MFC)[^].


有几种方法可以为Windows创建自定义按钮用C ++。



这里有两个最常见的:



There are several ways to create Custom-Buttons for Windows in C++.

Here are two of the most common:

// Load Bitmap for Button
hBitmap = (HBITMAP)LoadImage (GetModuleHandle (NULL), L"BMButton.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE);

// Version1: BS_BITMAP
hBButton1 = CreateWindow( TEXT("button"), TEXT(""),
          WS_CHILD | WS_VISIBLE | BS_FLAT | BS_BITMAP,
          50,50,BWIDTH,BHIGHT,  // pos x, pos y, width, height
          hwnd, (HMENU)ID_BBUTTON1,
          hInstGlobal, NULL);      // hInstGlobal see  ((LPCREATESTRUCT) lParam)->hInstance,

SendMessage(hBButton1, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)(HANDLE)hBitmap);

// Version2: BS_OWNERDRAW
hBButton2 = CreateWindow(TEXT("button"),TEXT(""),
          WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | BS_PUSHBUTTON,
          50,170,BWIDTH,BHIGHT,  // pos x, pos y, width, height
          hwnd,(HMENU)ID_BBUTTON2,
          hInstGlobal, NULL);
return 0 ;





版本2更复杂,因为你必须自己在WM_DRAWITEM中进行绘图。



我建议使用Version1。



Version2 is more complicate because you have to do the drawing yourself in WM_DRAWITEM.

I suggest using Version1.


提供了很少的信息来说明如何使用。至少你应该指定你正在使用的UI框架,例如,mfc,wxwidgets..Net,GTK +,QT或其他任何东西。不同的框架为开发人员提供了创建自定义UI元素的不同方法,但是,在大多数情况下,您只需从框架提供的元素派生,并重写您自己的绘制函数或事件处理程序。
there are rare information provided to say how. at least you should specify which UI framework you are using, for instance, mfc, wxwidgets..Net, GTK+, QT or anything else. different framework provides different approach for developers to create custom UI elements, however, at most cases, you just simply derived from the element the framework provide, and rewrite your own paint function or eventhandler.


这篇关于创建自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆