按下按下处理win32 c ++

时间:2016-05-18 11:47:28

标签: c++ multithreading winapi button win32gui

我正在创建一个插件,可以加载带有2个按钮的原生window,按下它们会弹出一个消息框但是没有瞳孔

为消息循环创建线程

    //create message thread
class startMessageThreadLoop
{
public:
    static DWORD WINAPI StaticThreadStart(void* Param)
    {
        MessageBox(hWnd, L"StaticThreadStart", L"StaticThreadStart", 0);
        startMessageThreadLoop* This = (startMessageThreadLoop*)Param;
        return This->ThreadStart();
    }

    DWORD ThreadStart(void)
    {
        MessageBox(hWnd, L"ThreadStart", L"ThreadStart", 0);
        //create message loop for buttons
        MSG msg = { 0 };
        while (GetMessage(&msg, NULL, 0, 0))
        {
            //translate and send messages
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        return 0;
    }

    void startMyThread()
    {
        MessageBox(hWnd, L"startMyThread", L"startMyThread", 0);
        DWORD ThreadID;
        CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
        char szTest[100];
        printf(szTest, "%d", ThreadID);
        MessageBox(hWnd, LPCWSTR(szTest), L"ThreadIDBaby", 0);
    }
};

启动帖子

startMessageThreadLoop ThreadLoopInstance;

ThreadLoopInstance.startMyThread();

创建窗口

vidUploader.cbSize = sizeof(WNDCLASSEX);

vidUploader.style = CS_HREDRAW | CS_VREDRAW;
vidUploader.lpfnWndProc = WndProc;
vidUploader.cbClsExtra = 0;
vidUploader.cbWndExtra = 0;
vidUploader.hInstance = hUpload;
vidUploader.hIcon = LoadIcon(hUpload, MAKEINTRESOURCE(IDI_P2GOVIDEOUPLOADER20));
vidUploader.hCursor = LoadCursor(NULL, IDC_ARROW);
vidUploader.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
vidUploader.lpszMenuName = MAKEINTRESOURCE(IDC_P2GOVIDEOUPLOADER20);
vidUploader.lpszClassName = (LPCWSTR)(L"UploadVideo");
vidUploader.hIconSm = LoadIcon(wcexUpload.hInstance, MAKEINTRESOURCE(IDI_SMALL));

RegisterClassEx(&vidUploader);

hInst = hUpload; // Store instance handle in our global variable

hWnd = CreateWindow((LPCWSTR)(L"UploadVideo"), (LPCWSTR)(L"Upload Video's"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hUpload, NULL);

处理按钮

 LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
            MessageBox(hWnd, L"Heeey", L"Hoi", 0);
        }
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
}

创建按钮&显示窗口

SelectVideoBTN = CreateWindow(
    L"BUTTON",  // Predefined class; Unicode assumed 
    L"Select Video's",      // Button text 
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
    10,         // x position 
    460,        // y position 
    100,        // Button width
    25,         // Button height
    hWnd,       // Parent window
    (HMENU)IDC_SELECT_VIDEO, // Assign appropriate control ID
    (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
    NULL);      // Pointer not needed.

UploadBTN = CreateWindow(
    L"BUTTON",  // Predefined class; Unicode assumed 
    L"Upload",      // Button text 
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
    390,         // x position 
    460,         // y position 
    100,        // Button width
    25,        // Button height
    hWnd,     // Parent window
    NULL,       // No menu.
    (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
    NULL);      // Pointer not needed.

RECT rect = { 0, 0, uploadWNDWidth, uploadWNDHeight };
AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);

if (!hWnd)
{
    MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);

    return 1;
}

MSG msg;

// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
//nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
    nCmdShowUpload);

UpdateWindow(hWnd);

我相信我正在做我必须做的所有事情以使按钮工作,我在一个线程中有一个消息循环,我正在注册&处理按钮和窗口,我缺少什么?

1 个答案:

答案 0 :(得分:2)

窗口的消息队列链接到创建窗口的线程。消息循环必须在同一个线程中处理。