【问题标题】:HANDLE in function won't work [closed]功能中的 HANDLE 不起作用[关闭]
【发布时间】:2016-04-20 11:19:44
【问题描述】:

我试图让这段代码在我的应用程序窗口中画一个圆圈:

    Draw Circle;
    Circle.DrawCircle(hwnd, i*200+5, j*200+5, i*200+194, j*200+194, 7, (255, 0, 0));

我使用的类,叫做 Draw.cpp,看起来像这样

    #include "Draw.h"
    #if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
    #elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
    #endif

    #include <tchar.h>
    #include <windows.h>
    #include <vector>

    using namespace std;


    Draw::Draw()
    {
    }

    void DrawCircle(HWND vhwind, int xPosTopLeft, int yPosTopLeft, int xPosBotRight, int yPosBotRight, int width, vector <int> rgb)
    {
         HDC hdc;                          
         PAINTSTRUCT ps;                   
         hdc = BeginPaint(vhwind, &ps);
         HPEN hPenOld;
         HPEN hLinePen = CreatePen(PS_DASH, width, RGB (rgb[0], rgb[2], rgb[2]));
         hPenOld = (HPEN)SelectObject(hdc, hLinePen);
         Arc(hdc, xPosTopLeft, yPosTopLeft, xPosBotRight, xPosBotRight, NULL, NULL, NULL, NULL);
         SelectObject(hdc, hPenOld);
         DeleteObject(hLinePen);
    }

名为 Draw.h 的头文件如下所示:

    #ifndef DRAW_H
    #define DRAW_H
    #include "Draw.h"
    #if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
    #elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
    #endif

    #include <tchar.h>
    #include <windows.h>
    #include <vector>

    using namespace std;


    class Draw
    {
        public:
            Draw();
            void DrawCircle(HWND vhwind, int xPosTopLeft, int yPosTopLeft, int xPosBotRight, int yPosBotRight, int width, vector <int> rgb);
        protected:
        private:
    };

    #endif // DRAW_H

我认为这个问题与“HWND vhwind”有关。 错误消息说:“错误:没有匹配的函数调用'Draw :: DrawCircle(HWND__ *&,int,int,int,int,int,int)'” 谁能告诉我我做错了什么?

【问题讨论】:

  • 错误信息有一个行号。它不指向您发布的任何代码。此外,您没有名为 DrwawCircle 的方法。
  • 确保您已在 Draw.h 文件中声明了 DrawCircle() 函数。同样在 cpp 文件中,现在您将 void DrawCircle 更改为 void Draw::DrawCircle(
  • 您传递的参数类型与函数签名不匹配。

标签: c++ winapi


【解决方案1】:

试试

Draw Circle;
Circle.DrawCircle(hwnd, i*200+5, j*200+5, i*200+194, j*200+194, 7, {255, 0, 0});

最后一个参数写成(255, 0, 0),被解释为单个int

--- 编辑---

抱歉:如果您使用的是 C++11 或 C++14,我的解决方案很好。

如果你使用的是 C++98,你应该准备一个std::vector&lt;int&gt;;像

Draw Circle;
std::vector<int> rgb;

rgb.reserve(3);
rgb.push_back(255);
rgb.push_back(0);
rgb.push_back(0);

Circle.DrawCircle(hwnd, i*200+5, j*200+5, i*200+194, j*200+194, 7, rgb);

ps:对不起,我的英语不好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2011-12-03
    相关资源
    最近更新 更多