0

そのため、別のウィンドウに重ねられたテキストを印刷する関数を作成していましたが、それを別のスレッドにして、ユーザーを開いたままにして表示するテキストのタイマーを実行してプログラムを使用できるようにしたかったのです。ただし、コンパイルすると、次のエラーが発生します。

error C2664: '_beginthreadex' : cannot convert parameter 3 from 'overloaded-function' to 'unsigned int (__stdcall *)(void *)'

メインの cpp ファイルは次のとおりです。

#include "stdafx.h"
#include "Trial.h"

int main()
{
wchar_t* text = L"Message!";
HWND hwnd = FindWindowW(0, L"Halo");
unsigned threadID;
_beginthreadex(0, 0, DrawText,(void *)(hwnd, 175, 15, text, 8), 0 , &threadID);
// Other function here
}

Trial.h のヘッダー ファイルは次のとおりです。

#pragma once    
#include <Windows.h>
#include <string>
#include <process.h>

void DrawText(HWND hWnd, float x, float y, wchar_t* mybuffer, float DisplayTime)
{
SetForegroundWindow(hWnd);
HDC hdc = GetDC(hWnd);
SetBkColor(hdc,RGB(255, 255, 255));                   // While Background color...
SetBkMode(hdc, TRANSPARENT);                        // Set background to transparent so we don't see the white...

int howmany = sizeof(mybuffer) * 2;

DisplayTime *= 500;
int p = 0;
while(p < DisplayTime)
{
            // Shadow Offset
    SetTextColor(hdc,RGB(0, 0, 0)); 
    TextOut(hdc,x+2,y+2, (LPCWSTR)mybuffer,howmany);

    // Primary text
    SetTextColor(hdc,RGB(255, 0, 0));   
    TextOutW(hdc,x,y,(LPCWSTR)mybuffer,howmany);

    UpdateWindow(hWnd);
    p++;
    Sleep(2);
}
ReleaseDC(hWnd,hdc);
_endthreadex(0);
}

複数の例を調べ、構文をチェックし、_beginthreadex を台無しにしていないことを確認しましたが、問題の原因を見つけることができないようです :|

4

2 に答える 2

3

In a nutshell thread startup functions need to follow an exact prototype, and not the one you used.

They can accept a function that takes a single void *.

There are several solutions.

  1. Change your function to accept a void*. Right away cast it to a 'struct *' of some type you have created and has the data you want. You would typically create the struct in main with new/malloc, and then delete/free it when you don't need it in the thread function.
  2. The somewhat cleaner alternative is to 'new' up an object of a class you have made. Give that class a public static method that takes said void *. Use the static method as the thread starter, and pass the address of the object as 'this'. Then have the static cast the void * to the object type and call some 'start/run' routine on the object proper. Have the object delete itself before returning from the thread routine unless you have a more coordinated solution across the threads.
于 2012-03-15T04:45:30.950 に答える
2
_beginthreadex(0, 0, DrawText,(void *)(hwnd, 175, 15, text, 8), 0 , &threadID);

MSDNによると、3 番目の引数は、引数の型が である関数へのポインターである必要がありますvoid*。あなたの場合、DrawText引数が not void*butである関数です(HWND hWnd, float x, float y, wchar_t* mybuffer, float DisplayTime)。したがって、エラーとリンクの例を見てください。

于 2012-03-15T04:44:01.767 に答える