我想将传递给test函数的任何内容输出到gui,但不确定如何在范围之外调用windowprocedure
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
textBox3 = CreateWindow("EDIT","",WS_VISIBLE,10,20,475,185,hwnd, NULL, NULL, NULL);
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 1:
SetWindowText(hwnd, "test")
}
break;
}
}
---
class.cpp file:
void class::test(){
string x = "printthis";
SetWindowText(hwnd, x)
}
}
答案 0 :(得分:0)
首先,FindWindowA()
可以找出您需要发送的窗口,并获得窗口句柄(“ hwnd”)。使用SendMessage()
向触发器WindowProcedure()
消息处理程序发送消息,然后在WindowProcedure()
中处理此类消息。
这是代码示例:
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
COPYDATASTRUCT* copyData;
CHAR str[BUFFER_SIZE] = { 0 };
switch (message)
{
case WM_CREATE:
//Do Creat Windows.
break;
case WM_COMMAND:
//Do Something.
break;
case WM_COPYDATA:
copyData = (COPYDATASTRUCT*)lParam;
memcpy(str, (void *)copyData->lpData, copyData->cbData);
MessageBoxA(hWnd, str, "title", MB_OKCANCEL);
break;
}
}
class.cpp文件:
#include <windows.h>
#pragma warning(disable:4996)
int main()
{
char szWindowClass[] = "EDIT";
char cp[] = "printthis";
HWND hWnd = FindWindowA(szWindowClass, "xxx"/*windows title*/);
COPYDATASTRUCT copyData = { 0 };
copyData.lpData = cp;
copyData.cbData = strlen(cp)+1;
LRESULT h = SendMessageA(hWnd, WM_COPYDATA, 0, (LPARAM)©Data);
BOOL reg = SetWindowTextA(hWnd, "test");
return 0;
}