当前位置: 代码迷 >> QT开发 >> 在QT中调用windows api的有关问题
  详细解决方案

在QT中调用windows api的有关问题

热度:115   发布时间:2016-04-25 03:47:29.0
在QT中调用windows api的问题
我在QT的文件里include<windows.h>

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

     // 创建红色1像素宽度的实线画笔
     HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
编译通过可是连接报错
GetWindowDC可以通过可是CreatePen不能通过。

------解决方案--------------------
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)

然后再编译试试
------解决方案--------------------
引用:
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……

楼上分析都是对的,只不过最后给的解决方案有些问题。

只需要
LIBS += -lgdi32

即可( 不论是 MinGW 还是 MSVC )
  相关解决方案