我试图阅读其他人的LNK2019错误示例,但它们与我的情况不符。它困扰了我2个小时,不知道该怎么做。
kbam_emu和pixel_check是我的命名空间。背后的名字是我的功能。它们在“kbam_emu.h”,“kbam_emu.cpp”,“pixel_check.h”和“pixel_check.cpp中声明“分别:
“kbam_emu.h”
/*
Name: keyboard and mouse emulation
Copyright: -
Author: Samuel YKC
Date: 15/11/14 20:17
Description: This header file is designed for creating bots. The essential codes for keyboard and mouse input are written by
a programmer on the Internet with the user name 'Muted'. Thanks, Muted. Here is the address for his original post:
http://forum.codecall.net/topic/45292-c-keyboardmouse-emulation/
*/
#ifndef _KBAM_EMU_H_
#define _KBAM_EMU_H_
#include <string>
#include <sstream>
#include <ctype.h>
#include <windows.h>
#include <winable.h>
using namespace std;
namespace kbam_emu
{
enum mouse_button{ left, middle, right };
void GenerateKey(int vk, BOOL bExtended);
void GenerateLine(string line);
void Click(const int X, const int Y, mouse_button button = left);
void Drag(const int X, const int Y, const int X2, const int Y2);
}
#endif
“kbam_emu.cpp”
#include <kbam_emu.h>
/* This is a function to simplify usage of sending keys */
void kbam_emu::GenerateKey(int vk, BOOL bExtended)
{
KEYBDINPUT kb = {0};
INPUT Input = {0};
/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
return;
}
/* This is a function to send a line containing English alphabets, numbers and spaces
It assumes that the user does not have the Caps Lock turned on initailly */
void kbam_emu::GenerateLine(string line)
{
stringstream is(line);
char c;
while(is.get(c))
{
if(isupper(c))
{
GenerateKey(VK_CAPITAL, TRUE); //Caps Lock on
GenerateKey(c, FALSE);
GenerateKey(VK_CAPITAL, TRUE); //Caps Lock off
}
else
{
GenerateKey(toupper(c), FALSE);
}
}
return;
}
/* This is a function to click somewhere on the screen */
void kbam_emu::Click(const int X, const int Y, mouse_button button)
{
SetCursorPos(X, Y);
DWORD down;
DWORD up;
switch(button)
{
case left:
down = MOUSEEVENTF_LEFTDOWN;
up = MOUSEEVENTF_LEFTUP;
break;
case middle:
down = MOUSEEVENTF_MIDDLEDOWN;
up = MOUSEEVENTF_MIDDLEUP;
break;
case right:
down = MOUSEEVENTF_RIGHTDOWN;
up = MOUSEEVENTF_RIGHTUP;
break;
}
INPUT Input[2];
ZeroMemory(Input, sizeof(INPUT) * 2);
Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = down;
Input[1].type = INPUT_MOUSE;
Input[1].mi.dwFlags = up;
SendInput(2, Input, sizeof(INPUT));
return;
}
void kbam_emu::Drag(const int X, const int Y, const int X2, const int Y2)
{
SetCursorPos(X, Y);
INPUT Input[1];
ZeroMemory(Input, sizeof(INPUT));
Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, Input, sizeof(INPUT));
SetCursorPos(X2, Y2);
INPUT Input2[1];
ZeroMemory(Input2, sizeof(INPUT));
Input2[0].type = INPUT_MOUSE;
Input2[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, Input2, sizeof(INPUT));
return;
}
“pixel_check.h”
/*
Name: pixel check
Copyright: -
Author: Samuel YKC
Date: 16/11/14 00:26
Description: This header file is designed for checking pixels. The essential codes for retrieving the color of a pixel
on screen are written by a programmer on the Internet with the user name 'templatetypedef'. Thanks, templatetypedef. Here
is the address for his original post:
http://stackoverflow.com/questions/4839623/getting-pixel-color-in-c
*/
#ifndef _PIXEL_CHECK_H_
#define _PIXEL_CHECK_H_
#include <windows.h>
#include "kbam_emu.h"
using namespace std;
using namespace kbam_emu;
namespace pixel_check
{
COLORREF GetPixelColor(int x, int y);
bool ComparePixelWithColor(int x, int y, COLORREF color);
void SleepOnColor(int x, int y, COLORREF color, int interval=100);
void SleepUntilColor(int x, int y, COLORREF color, int interval=100);
void ClickUntilColor(int click_x, int click_y, int pixel_x, int pixel_y, COLORREF color, int interval=1000);
}
#endif
“pixel_check.cpp”
#include "pixel_check.h"
COLORREF pixel_check::GetPixelColor(int x, int y)
{
HDC dc = GetDC(NULL);
COLORREF color = GetPixel(dc, x, y);
ReleaseDC(NULL, dc);
return color;
}
bool pixel_check::ComparePixelWithColor(int x, int y, COLORREF color)
{
HDC dc = GetDC(NULL);
COLORREF pixel_color = GetPixel(dc, x, y);
ReleaseDC(NULL, dc);
return (pixel_color==color);
}
void pixel_check::SleepOnColor(int x, int y, COLORREF color, int interval)
{
while(ComparePixelWithColor(x,y,color)) Sleep(interval);
}
void pixel_check::SleepUntilColor(int x, int y, COLORREF color, int interval)
{
while(!ComparePixelWithColor(x,y,color)) Sleep(interval);
}
void pixel_check::ClickUntilColor(int click_x, int click_y, int pixel_x, int pixel_y, COLORREF color, int interval)
{
while(!ComparePixelWithColor(pixel_x,pixel_y,color))
{
Click(click_x, click_y);
Sleep(interval);
}
}
在我的项目中,我把#include这样(我把.h&amp; .cpp放在默认的include目录中,这样可以使用<>
):
#include <kbam_emu.h>
#include <pixel_check.h>
并以这种方式使用这些功能:
kbam_emu::Click(0, 0);
最后,在.pro文件中我添加了这一行:
LIBS += -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 -lodbccp32
我认为我所做的一切都是合乎逻辑的。仍然,链接器打赌不同。我试图在这里发布完整的代码,这样我就不会错过任何东西。有谁知道出了什么问题?
答案 0 :(得分:0)
在我连续几天不停地尝试之后,我意外地找到了解决方案。
我这次将文件放入项目而不是默认的 \ include 目录。然后,我点击按钮&#34;运行qmake&#34;在Qt和那些kbam_emu.obj&amp;最终创建之前尚未创建的pixel_check.obj。现在没有更多的链接器问题了!
我认为这可能是关于makefile没有自动更新所以我需要手动给qt运行qmake指令。希望我的案子对你们有所帮助。