在QT中调用windows api的问题

q672856032 2011-03-02 05:02:58
我在QT的文件里include<windows.h>

在原文件里
// 获取一个可供画图的DC,我这里就直接用桌面算了
HDC hdc = GetWindowDC( GetDesktopWindow() );

// 创建红色1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
编译通过可是连接报错
GetWindowDC可以通过可是CreatePen不能通过。
...全文
2206 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
寒山-居士 2013-07-11
  • 打赏
  • 举报
回复
这个问题 GlobalUnlock ( stg.hGlobal ); ReleaseStgMedium ( &stg ); 和楼主类似,可是我加入了LIBS += -lgid32还是编译不过,但是 GlobalUnlock ( stg.hGlobal )过了
DIE654456 2011-03-03
  • 打赏
  • 举报
回复
debug/mainwindow.o: In function `MainWindow':
C:\Documents and Settings\Administrator\My Documents\textApi/mainwindow.cpp:13: undefined reference to `CreatePen@12'
C:\Documents and Settings\Administrator\My Documents\textApi/mainwindow.cpp:13: undefined reference to `CreatePen@12'

你用的是Qt的库和头文件。现在要加入Window的调用,光有头文件是不够的,还需要库。
查看MSDN或者网上百度,可以知道GetWindowDC需要的是:
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.

CreatePen需要的是:
Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Use Gdi32.lib.

上面的两个库在VC或者VS安装后能找到。
例如默认的VC会带着。在C:\Program Files\Microsoft Visual Studio\VC98\Lib

要在工程里把这些库加进去。
比如将库拷贝到C:\下,在.pro文件中加入
LIBS += $$quote(C:/Gdi32.lib)

然后再编译试试
dext 2011-03-03
  • 打赏
  • 举报
回复
试试看 CreatePenIndirect
q672856032 2011-03-03
  • 打赏
  • 举报
回复
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<windows.h>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
HDC hdc = GetWindowDC( GetDesktopWindow() );


HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0));


}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


程序原代码
q672856032 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dbzhang800 的回复:]
不妨贴出错信息
[/Quote]]
debug/mainwindow.o: In function `MainWindow':
C:\Documents and Settings\Administrator\My Documents\textApi/mainwindow.cpp:13: undefined reference to `CreatePen@12'
C:\Documents and Settings\Administrator\My Documents\textApi/mainwindow.cpp:13: undefined reference to `CreatePen@12'
dbzhang800 2011-03-03
  • 打赏
  • 举报
回复
不妨贴出错信息
dbzhang800 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 die654456 的回复:]

debug/mainwindow.o: In function `MainWindow':
C:\Documents and Settings\Administrator\My Documents\textApi/mainwindow.cpp:13: undefined reference to `CreatePen@12'
C:\Documents and Settings\Adminis……
[/Quote]
楼上分析都是对的,只不过最后给的解决方案有些问题。

只需要
LIBS += -lgdi32

即可( 不论是 MinGW 还是 MSVC )
q672856032 2011-03-02
  • 打赏
  • 举报
回复
有人知道吗?

16,210

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧