我尝试了所有源代码通过实现子类化来替换按钮的背景。
当我改变背景时,我得到了一个正方形的形状,无法点击它。有人可以分享一个好的教程或代码示例吗?
无论如何,这是我的代码:
LRESULT CALLBACK DrawPushButton(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdcMem;
HBITMAP hbmMem;
HANDLE hnd;
PAINTSTRUCT ps;
HDC hdc;
int win_width=100;
int win_height=50;
switch(uMsg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
hbmMem = CreateCompatibleBitmap(hdc, win_width, win_height);
hnd = SelectObject(hdcMem, hbmMem);
BitBlt(hdc, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
break;
}
return CallWindowProc (OldWndProc, hwnd, uMsg, wParam, lParam);
}
编辑:我为按钮添加了BS_OWNERDRAW窗口样式:
HWND hWndButton=CreateWindowEx(NULL,L"BUTTON",L"OK",WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON|BS_OWNERDRAW,50,220,100,24,hWnd,(HMENU)IDC_MAIN_BUTTON,GetModuleHandle(NULL),NULL);
我更改了回调,但WM_DRAWTITEM没有调用:
LRESULT CALLBACK DrawPushButton(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DRAWITEM:
HDC hdc = GetDC(hwnd);
TextOut(hdc,0,0,L"itay",4);
break;
}
return 0;
}
这不是一个"好"代码,尽管它是功能性的——正如前面提到的,你可能应该使用BS_OWNERDRAW样式,并在包含按钮的对话框/窗口的wndpoc中处理图形。我选择粘贴一个旧的例子,用和你一样的方式来处理它。你必须自己完成这幅画的所有。然而,您确实有机会响应按钮收到的WM_TIMER消息,该消息会使按钮跳动。(如果只从对话框/窗口的WindowProc绘制,则不确定是否有机会这样做)
为了简单起见,我还选择使用一种被取代的子类方法。由于通用控件的版本6,您应该使用SetWindowSubclass(http://msdn.microsoft.com/en-us/library/windows/desktop/bb762102(v=vs.85).aspx)
main.cpp
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
HINSTANCE hInst;
LRESULT CALLBACK btnProc(HWND btnWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
long oldWndProc;
int i;
oldWndProc = GetWindowLong(btnWnd, GWL_USERDATA);
switch (uMsg)
{
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
RECT mRect;
HBRUSH mBrush;
hdc = BeginPaint(btnWnd, &ps);
mBrush = CreateSolidBrush( RGB(100, 147, 111));
GetClientRect(btnWnd, &mRect);
FillRect(hdc, &mRect, mBrush);
DeleteObject(mBrush);
EndPaint(btnWnd, &ps);
}
return 0;
}
return CallWindowProc((WNDPROC)oldWndProc, btnWnd, uMsg, wParam, lParam);
}
void onCommand(HWND hwndDlg, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case IDC_BUTTON1:
MessageBeep(MB_ICONEXCLAMATION);
break;
}
}
LRESULT CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
HWND btnWnd;
long oldBtnProc;
btnWnd = GetDlgItem(hwndDlg, IDC_BUTTON1);
oldBtnProc = GetWindowLong(btnWnd, GWL_WNDPROC);
SetWindowLong(btnWnd, GWL_USERDATA, oldBtnProc);
SetWindowLong(btnWnd, GWL_WNDPROC, (long)btnProc);
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_COMMAND:
onCommand(hwndDlg, wParam, lParam);
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}
资源.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#define DLG_MAIN 100
#define IDC_BUTTON1 1000
资源.rc
// Generated by ResEdit 1.5.11
// Copyright (C) 2006-2012
// http://www.resedit.net
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 186, 95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
PUSHBUTTON "Button1", IDC_BUTTON1, 7, 7, 50, 14
}
//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1 RT_MANIFEST ".\manifest.xml"
manifest.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="asInvoker"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>