C++如何给一个自绘Button贴图作背景

不是MFC我生成了一个按纽
HWND TestBTN = CreateWindow(
TEXT("Button"),
TEXT("Test"),
BS_PUSHBUTTON | WS_VISIBLE |WS_CHILD|BS_OWNERDRAW,// OWNERDRAW自绘按钮标识
300,
150,
120,
40,
g_hWnd,
NULL,
hInst,
NULL);
如何把test.bmp贴在上面

恩,很好用的是纯正的api,用loadimage(里面的参数),获取对应的image 的句柄,这个是api,然后如果是在你的mfc环境下,可以这么做获取Cbutton* b=Cbutton::fromhandle(你上面的按钮句柄),然后b->setbitmap(图像对应的句柄),或者直接api函数,SetBitmap(按钮句柄,图片句柄),遗憾,我已经转入C﹟的学习,追问

MFC的我会贴,就是想知道API的,把主要的再说一下句柄获得了,然后呢,我贴上了但是老是闪,若隐若现,难道winmain老刷新?不刷新怎么办,还有贴不到这个按纽上面去,贴下面去了,好像也没有设置Z序的地方啊

追答

看看在WM_PAINT 里面的看看能话吗,
你看看吧。。。
下面解释很好啊。。。
今天做了个图片浏览器,用了Java呵C#一起完成的,才发现C#跟java差不多
为什么我要用两种语言呢?
1:刚学习C# 不懂的枚举的文件的函数或者类,但是Java的我会,因此将得到的文件名字写入了一个文本文件。
2:因为Java要虚拟机的,种是超过内存,而且还是要布局,图像无法放大,
因此用了C#,用去来跟Java差不多,于是我很顺利的读取了该文件。并且显示的很好。。
那学习VC 呢,VC很复杂,但是想想C#的简单和Javad的简单,值得学习C++
http://msdn.microsoft.com/en-us/library/a1446sbt(v=vs.71).aspx
Parameters
hBitmap
The handle of a bitmap.
Return Value
The handle of a bitmap previously associated with the button.

Remarks
The bitmap will be automatically placed on the face of the button, centered by default. If the bitmap is too large for the button, it will be clipped on either side. You can choose other alignment options, including the following:

BS_TOP
BS_LEFT
BS_RIGHT
BS_CENTER
BS_BOTTOM
BS_VCENTER
Unlike CBitmapButton, which uses four bitmaps per button, SetBitmap uses only one bitmap per the button. When the button is pressed, the bitmap appears to shift down and to the right.

Example
CopyCButton myButton;

// Create a bitmap button.
myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_BITMAP,
CRect(10,10,60,50), pParentWnd, 1);

// Set the bitmap of the button to be the system check mark bitmap.
myButton.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(OBM_CHECK)) );

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-06-15

Win32下按钮自绘:

    新建一个类CBmpButton。写一个Create函数,此函数加载bmp到一个HBITMAP中;创建按钮CreateWindow;修改按钮消息循环SetWindowLong到自定义的消息循环中

    在自定义的消息循环中,捕获WM_PAINT消息,在此消息中使用SelectObject选中第1步中的HBITMAP,然后再在按钮的HDC中使用BitBlt绘制背景图

    如有需要,捕获WM_LBUTTONUP等消息并添加响应。


MFC中自绘:

    1.子类化。PreSubclassWindow

    2.重载OnPaint、OnCtrlColor、OnLButtonDown等函数

    3.在DrawItem和OnPaint中使用BitBlt在按钮的CDC中绘背景图片。


综上所述,自绘的关键是子类化,然后重载实现Paint、ButtonDown等函数。

相似回答