CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    6

    Using GDI+ w/Visual C++

    Hello,

    New to posting on these forums, so hopefully I will explain clearly what im trying to accomplish (if this is in the wrong forum please move... my apologies). Ive been programming in C++/VB for a few years now and have been asked to write a program that will draw lines based upon points given in a text file. Not so hard right? Along these lines, I need 'crosstracks' at certain points. Since im familiar with C++ (and using it in school), I decided I wanted to use Visual Studio 2005 to make this program (in VC++ of course).

    I have never worked with GDI before, but apparantly its not hard to do with 2D objects. I have wrote some basic code using GDI+ to create this basic framework (lines, shapes, etc). I do have a few questions though:

    1. Is it possible (using windows forms c++) to make a small "picturebox" type of control and draw whatever you want separate from the rest of the controls on the form? I know in Visual Basic you could add a picturebox control, which was very useful. What im trying to do is create a form with buttons and textboxes that will alter the image based on the input. Think of it as a form with a place to output the result, and a few textboxes to add points and plot the lines.

    2. I didnt notice any built in functions or documentation on how to add textboxes at the end of a random line that you draw.

    Ive been doing research for about a week and a half now, and surprisingly i havent found any really good examples of using gdi+ in a managed c++ format. Ive had a mental block for a few days now and no amount of research seems to help. This is where you come in

    So please, if you have any code samples or even basic suggestions to go off of, it would be much appreciated.

    Thank you!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Using GDI+ w/Visual C++

    Quote Originally Posted by mjgroves
    Hello,

    New to posting on these forums, so hopefully I will explain clearly what im trying to accomplish (if this is in the wrong forum please move... my apologies).
    Welcome to Codeguru

    Quote Originally Posted by mjgroves
    1. Is it possible (using windows forms c++) to make a small "picturebox" type of control and draw whatever you want separate from the rest of the controls on the form?
    You'll need to derive your own class from CStatic and implement OnPaint (WM_PAINT handler).
    Search these articles for more detailed information and examples.

    Quote Originally Posted by mjgroves
    2. I didnt notice any built in functions or documentation on how to add textboxes at the end of a random line that you draw.
    What exactly do you want to achieve? Do you want to dislay some static text at the end of a line or should the user be able to edit that text?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Sep 1999
    Location
    Germany, Hessen
    Posts
    226

    Re: Using GDI+ w/Visual C++

    To draw your own thing, You might also add a button to the Dialog, make it ownerDraw and overwrite parents DrawItem().

    to use GDIplus in C++, you should add something like that to your stdafx:

    Code:
     
    // use GDI+ (Platform SDK needed)      //
    /////////////////////////////////////////
    #define ULONG_PTR ULONG                //
    #include <Gdiplus.h>                   //
    using namespace Gdiplus;               //
    #pragma comment(lib, "gdiplus.lib")    //
    /////////////////////////////////////////
    ... and make sure that the include + lib files from platform SDK are in your visual studio's include pathes (menu: Tools->Options -> tab: directories)

    furthermore, somewhere in your app before using gdiplus (e.G. in your MyApp::InitInstance() ) you must call something like:

    Code:
     
    Status s;
    GdiplusStartupInput		gdiplusStartupInput( NULL, FALSE, FALSE );
    GdiplusStartupOutput	gdiplusStartupOutput;
    s = GdiplusStartup( &g_gdiplus_token, &gdiplusStartupInput, &gdiplusStartupOutput );
    BOOL g_gdiplus_initialized = ( s==Ok );

  4. #4
    Join Date
    Jan 2006
    Posts
    6

    Re: Using GDI+ w/Visual C++

    Quote Originally Posted by Marc G
    Welcome to Codeguru

    You'll need to derive your own class from CStatic and implement OnPaint (WM_PAINT handler).
    Search these articles for more detailed information and examples.

    What exactly do you want to achieve? Do you want to dislay some static text at the end of a line or should the user be able to edit that text?
    Thanks for the warm welcome guys. Yes, I want to be able just to draw static text at the end of a line I draw (showing line information like length and x,y coordinates). Im guessing as long as you have your endpoint you can offset the textbox relatively close to it. The only thing is that I cant have text overlapping and im unsure how to check for this.

    I decided to code in C# rather than C++ (kinda limited on the visual side, but C# is wonderful). Thanks to both of you guys, Im able to draw basic shapes and lines onto my panel control of the windows form. Things are picking up

    If you have any ideas that pop out about the 'text' problem I have, I would be happy to hear it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured