|
发表于 2013-7-15 15:20:57
|
显示全部楼层
本帖最后由 Ycxw2008 于 2013-7-15 16:12 编辑
回复 3# seniors
或许是au3方面的问题 同样的代码
在c++里 就可以很正常
2段代码在下面
au3;~ #include "DirUIbutton.au3"
#include <APIConstants.au3>
#include <WinAPIEx.au3>
Global Const $szWindowClass = "MyTestClass", $szTitle = "MyTestWindow"
Global Const $tagMSG = "hwnd hwnd; uint message; wparam wParam; lparam lParam; dword time; long X;long Y"
Global $hDLL, $nWidth, $nHeight
WinMain(_WinAPI_GetModuleHandle(0), 0, $WS_OVERLAPPEDWINDOW)
Func WindowProc($hWnd, $iMsg, $wParam, $lParam)
Switch $iMsg
Case $WM_COMMAND
MsgBox(0, "", "回调里能收到")
Return 0
Case $WM_CHAR
If $wParam == $VK_ESCAPE Then Return _WinAPI_DestroyWindow($hWnd)
Return 0
Case $WM_DESTROY
DllCall($hDLL, "void", "PostQuitMessage", "int", 0)
Return 0
EndSwitch
Return _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam)
EndFunc ;==>WindowProc
Func _BtnProc($hWnd, $iMsg, $wParam, $lParam)
Switch $iMsg
Case $WM_LBUTTONDOWN;左键点击
_WinAPI_PostMessage(_WinAPI_GetParent($hWnd), $WM_COMMAND, _WinAPI_MakeLong(_WinAPI_GetDlgCtrlID($hWnd), 0), $lParam)
Return 0
Case $WM_PAINT;处理绘制信息
Local $ps
Local $hDC = _WinAPI_BeginPaint($hWnd, $ps)
$rc = _WinAPI_GetClientRect($hWnd)
_WinAPI_Rectangle($hDC, $rc)
_WinAPI_EndPaint($hWnd, $ps)
Return 0
EndSwitch
Return _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam)
EndFunc ;==>_BtnProc
Func WinMain($hInstance, $dwExStyle, $dwStyle)
Local $MSG = DllStructCreate($tagMSG)
Local $pMSG = DllStructGetPtr($MSG)
Local $hProc = DllCallbackRegister('WindowProc', 'lresult', 'hwnd;uint;wparam;lparam')
Local $wcex = DllStructCreate($tagWNDCLASSEX)
DllStructSetData($wcex, 'Size', DllStructGetSize($wcex))
DllStructSetData($wcex, 'Style', 0)
DllStructSetData($wcex, 'hWndProc', DllCallbackGetPtr($hProc))
DllStructSetData($wcex, 'ClsExtra', 0)
DllStructSetData($wcex, 'WndExtra', 0)
DllStructSetData($wcex, 'hInstance', $hInstance)
DllStructSetData($wcex, 'hIcon', 0)
DllStructSetData($wcex, 'hCursor', _WinAPI_LoadCursor(0, 32512))
DllStructSetData($wcex, 'hBackground', _WinAPI_GetStockObject($NULL_BRUSH))
DllStructSetData($wcex, 'MenuName', 0)
DllStructSetData($wcex, 'ClassName', _WinAPI_CreateString($szWindowClass))
DllStructSetData($wcex, 'hIconSm', 0)
_WinAPI_RegisterClassEx($wcex)
$__DirUIButoon_hProc = DllCallbackRegister('_BtnProc', 'lresult', 'hwnd;uint;wparam;lparam')
DllStructSetData($wcex, 'hWndProc', DllCallbackGetPtr($__DirUIButoon_hProc))
DllStructSetData($wcex, 'ClassName', _WinAPI_CreateString("test"))
_WinAPI_RegisterClassEx($wcex)
Local $hWnd = _WinAPI_CreateWindowEx( _
$dwExStyle, _
$szWindowClass, _
$szTitle, _
$dwStyle, _
(@DesktopWidth - 400) / 2, _
(@DesktopHeight - 300) / 2, _
400, _
300, _
0, _
0, _
$hInstance, _
0)
If Not $hWnd Then Return 0
_WinAPI_ShowWindow($hWnd)
_WinAPI_UpdateWindow($hWnd)
$hDLL = DllOpen("User32.dll")
_WinAPI_CreateWindowEx(0, "test", "", BitOR($WS_CHILD, $WS_VISIBLE, $WS_TABSTOP, 0x4000), 0, 0, 50, 50, $hWnd, 0, 0, 0)
;~ _DirUIButoon_Create($hWnd, "test", 0, 0, 50, 50)
While GetMessage($MSG, $hWnd) == 1
If DllStructGetData($pMSG, "message") == $WM_COMMAND Then
MsgBox(0, '', '')
EndIf
TranslateMessage($pMSG)
DispatchMessage($pMSG)
WEnd
DllClose($hDLL)
_WinAPI_UnregisterClass($szWindowClass, $hInstance)
DllCallbackFree($hProc)
EndFunc ;==>WinMain
Func GetMessage($lpMSG, $hWnd)
Local $Ret = DllCall($hDLL, "bool", "GetMessageW", "ptr", $lpMSG, "handle", $hWnd, "uint", 0, "uint", 0);
Return $Ret[0];
EndFunc ;==>GetMessage
Func TranslateMessage($lpMSG)
Local $Ret = DllCall($hDLL, "bool", "TranslateMessage", "ptr", $lpMSG)
Return $Ret[0]
EndFunc ;==>TranslateMessage
Func DispatchMessage($lpMSG)
Local $Ret = DllCall($hDLL, "lresult", "DispatchMessageW", "ptr", $lpMSG)
Return $Ret[0] ;If Not @error Then Return $Ret[0];
EndFunc ;==>DispatchMessage
C++#include <stdio.h>
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK BtnProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
AllocConsole();
FILE *stream;
freopen_s(&stream,"CONIN$","r",stdin);
freopen_s(&stream,"CONOUT$","w",stdout);
printf("%s\n","test");
WNDCLASSEXW wcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style = 0;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName=L"test";
wcex.hIconSm=0;
RegisterClassExW(&wcex);
wcex.lpszClassName=L"TestButton";
wcex.lpfnWndProc=BtnProc;
RegisterClassExW(&wcex);
HWND hWnd=CreateWindowExW(0,L"test",L"test",WS_OVERLAPPEDWINDOW,0,0, 400, 300,0,0,hInstance,0);
if(!hWnd)
return 0;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
MSG msg;
CreateWindowExW(0,L"TestButton",L"",WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_NOTIFY,0,0,50,50,hWnd,0,0,0);
while(GetMessage( &msg, hWnd, 0, 0 )== 1)
{
if (msg.message==WM_COMMAND)
{
MessageBoxA(0,"循环里也能收到","",0);
printf("%s\n","循环里也能收到");
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
FreeConsole();
return 0;
}
LRESULT CALLBACK BtnProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_LBUTTONDOWN:
PostMessage(GetParent(hwnd),WM_COMMAND,MAKELONG(GetDlgCtrlID(hwnd),BN_CLICKED),(LPARAM)hwnd);
return 0;
case WM_PAINT:
{
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
Rectangle (hdc, 0, 0, rect.right, rect.bottom) ;
EndPaint (hwnd, &ps) ;
}
return 0;
}
return DefWindowProcW(hwnd,message,wParam,lParam);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
MessageBoxA(0,"回调里也能收到","",0);
printf("%s\n","回调里也能收到");
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd,message,wParam,lParam);
}
|
|