在 Win32ASM 中可以很容易的使用 Windows 自带的按钮、编辑框等控件,因为这些控件都是标准的控件,在系统中注册了唯一的窗口类,我们在使用时只需要调用 CreateWindowEx 或利用对话框模板就可以方便的使用这些控件。所以,如果我们能够把 MFC 编写的控件改造成一个标准的控件,也注册一个窗口类,那使用的时候就跟 Windows 标准控件一样啦。在 VC++ 中新建一个 MFC 标准动态库工程,把 ColorPicker.h 和 ColorPicker.cpp 两个文件添加到工程里。下面就是改造过程,如果有不明白的代码可以参考《VC++ 技术内幕》一书中动态链接库一章。
//ColorPicker.h 文件 ========================================\
#define CPWM_GETCOLOR WM_USER+11
LRESULT CALLBACK AFX_EXPORT ColorPickerWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
class CColorPicker : public CButton
{
...
public:
static BOOL RegisterWndClass (HINSTANCE hInstance);
...
protected:
//{{AFX_MSG(CColorPicker)
...
//}}AFX_MSG
afx_msg LRESULT GetColorRef(WPARAM wParam, LPARAM lParam);
//ColorPicker.cpp 文件 ========================================\
BEGIN_MESSAGE_MAP(CColorPicker, CButton)
//{{AFX_MSG_MAP(CColorPicker)
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
ON_MESSAGE(CPWM_GETCOLOR, GetColorRef) //这一句是新加的
END_MESSAGE_MAP()
...
BOOL CColorPicker::RegisterWndClass(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.lpszClassName = "ColorPickerWnd";
wc.hInstance = hInstance;
wc.lpfnWndProc = ColorPickerWndProc;
wc.hCursor = ::LoadCursor (NULL, IDC_ARROW);
wc.hIcon = 0;
wc.lpszMenuName = NULL;
wc.hbrBackground = (HBRUSH) ::GetStockObject(LTGRAY_BRUSH);
wc.style = CS_GLOBALCLASS;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
return (::RegisterClass(&wc) != 0);
}
LRESULT CALLBACK AFX_EXPORT ColorPickerWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
AFX_MANAGE_STATE (AfxGetStaticModuleState());
CWnd* pWnd;
pWnd = CWnd::FromHandlePermanent(hWnd);
if (pWnd == NULL)
{
pWnd = new CColorPicker();
pWnd->Attach(hWnd);
}
ASSERT(pWnd->m_hWnd == hWnd);
ASSERT(pWnd == CWnd::FromHandlePermanent(hWnd));
LRESULT lResult = AfxCallWndProc (pWnd, hWnd, uMsg, wParam, lParam);
return lResult;
}
LRESULT CColorPicker::GetColorRef(WPARAM wParam, LPARAM lParam)
{
return m_CurrentColor;
}
//ColorPickerWnd.cpp 文件 ========================================\
...
BOOL CColorPickerWndApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
CColorPicker::RegisterWndClass (AfxGetInstanceHandle());
return CWinApp::InitInstance();
}
经过上面的一番改动,编译连接后生成的 ColorPickerWnd.dll 就是我们需要的标准控件,加载这个动态链接库后会注册一个 ColorPickerWnd 的窗口类。我们使用时就可以像使用按钮等标准控件一样使用,不过类名是 ColorPickerWnd。具体请看下面的 Win32ASM 代码:
//TestColorPicker.rc 文件
DLG_MAIN DIALOG DISCARDABLE 0, 0, 178, 118
STYLE DS_CENTER | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "ColorPicker 控件测试"
FONT 9, "宋体"
BEGIN
CONTROL "Custom1",IDC_COLORPICKER1,"ColorPickerWnd",WS_TABSTOP,26,24,14,14
LTEXT "对话框模板生成控件",IDC_STATIC,51,27,73,8
LTEXT "这是动态创建的控件",IDC_STATIC,51,49,73,8
END
这是资源文件,利用对话框模板使用 ColorPicker 控件。
.386
.model flat, stdcall ;32 bit memory model
option casemap :none ;case sensitive
include windows.inc
include kernel32.inc
include user32.inc
include gdi32.inc
include e:\asm\lib\macros.inc
includelib kernel32.lib
includelib user32.lib
includelib gdi32.lib
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD
.data?
g_hInst dd ?
g_hColorPicker1 dd ?
g_hColorPicker2 dd ?
g_rgbBk dd ?
g_rgbText dd ?
.const
DLG_MAIN equ 1000
IDC_COLORPICKER1 equ 1001
IDC_COLORPICKER2 equ 1002
CPWM_GETCOLOR equ WM_USER+11 ;自定义消息
.code
start:
invoke GetModuleHandle, NULL
mov g_hInst, eax
invoke LoadLibrary, CTXT("ColorPickerWnd.dll") ;加载动态库
invoke DialogBoxParam, g_hInst, DLG_MAIN, NULL, addr DlgProc, NULL
invoke ExitProcess, 0
DlgProc proc hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
mov eax, uMsg
.if eax==WM_INITDIALOG
RGB 255, 255, 255
mov g_rgbBk, eax
RGB 0, 128, 0
mov g_rgbText, eax
invoke GetDlgItem, hWnd, IDC_COLORPICKER1
mov g_hColorPicker1, eax
;这里就是动态创建 ColorPicker 控件
invoke CreateWindowEx, 0, CTXT("ColorPickerWnd"), CTXT("Ctl2"),
WS_CHILD + WS_VISIBLE, 39, 70, 21, 21, hWnd, IDC_COLORPICKER2, g_hInst, 0
mov g_hColorPicker2, eax
.elseif eax==WM_COMMAND
mov eax, wParam
and eax, 0ffffh
;当选择了别的颜色时我们通过自定义消息 CPWM_GETCOLOR 取得新的颜色
.if eax==IDC_COLORPICKER1
invoke SendMessage, g_hColorPicker1, CPWM_GETCOLOR, 0, 0
mov g_rgbBk, eax
.elseif eax==IDC_COLORPICKER2
invoke SendMessage, g_hColorPicker2, CPWM_GETCOLOR, 0, 0
mov g_rgbText, eax
.endif
invoke InvalidateRect, hWnd, NULL, TRUE
;对话框背景颜色
.elseif eax==WM_CTLCOLORDLG
invoke CreateSolidBrush, g_rgbBk
ret
;静态框颜色
.elseif eax==WM_CTLCOLORSTATIC
invoke SetTextColor, wParam, g_rgbText
invoke SetBkColor, wParam, g_rgbBk
invoke CreateSolidBrush, g_rgbBk
ret
.elseif eax==WM_CLOSE
invoke EndDialog, hWnd, 0
.else
mov eax, FALSE
ret
.endif
mov eax, TRUE
ret
DlgProc endp
end start
初始化时动态创建了一个 ColorPicker 控件,自定义消息 CPWM_GETCOLOR 对应控件当中的 GetColorRef 函数,返回新的颜色值。
怎么样,只需要做少许工作我们就可以在 Win32ASM 中实现很酷的界面啦!
文章来源于领测软件测试网 https://www.ltesting.net/