0

i need use some GDI32 functions, but i need add the library 1st. i try link it with linker options, but the text wasn't showed. so how can add the library by code? note: the #pragma comment(lib, "gid32.lib") isn't portable, that's why i can't use it :(

#include <iostream>
#include <string.h>
#include <windows.h>
//#include <WinGdi.h>


using namespace std;

int main()
{
    //TextBlink("hello world", 10,20,3,5);
    HDC hDC=GetDC(GetConsoleWindow());
    SetTextColor(hDC,6);
    TextOut(hDC,1,5,"hello world",strlen("hello world"));
    cin.get();
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • The whole GDI library isn't portable... or did you mean between different Windows Compilers only? – nvoigt Oct 31 '13 at 12:04

2 Answers2

1

You cannot use TextOut in a console, you have to create a Window get the handle of the window and use the device context of that window.

Another problem is why worry about portability when you are using non portable function? TextOut is microsoft dependent...

Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
0

In the "project build options" under the "linker setting" tab, make sure that kernel32, gdi32 and user32 libraries are into the "link library" list.

If something is missing, you cannot even get an executable (the linker will fail!).

Any way, what you did is not hortodox: the console is painted internally by the operation system. Drawing on top of it isn't persistent and is overwritten every time the OS updates the console screen. May be during getch() or after every writing.

If you want to draw yourself the text or use whatever gaphic function, you had better to create yourself a window and do the drawing on it through its own WM_PAINT event. In other words, you need a windows you can control yourself.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63