GDI and MFC


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: GDI and MFC

  1. #1
    Join Date
    Mar 2005
    Location
    third world
    Posts
    10

    GDI and MFC

    Can anyone explain to me how to use GDI functions in an MFC project..?
    Some simple examples with changing backgrounds and drawing simple rectagles..? Anyone..?

    ...I've been trying to do it myself, but it doesn't work, unless I put the code into the OnPaint function, but OnPaint works only when the window is created as far as I understand...

    ...I wasted the whole day yesterday to find any info which I one who never worked with GDI could understand - nothing, nothing understandable, reminds me the book of Liberty, where he says 'here's an example of a simple list' and gives 5 pages of code instead of a real simple example...classes instead of 1...is it the way of saying 'look how clever I am'..? Bah...

    ...by now you are probably thinking what the **** I want...I want a simple explanation how to use GDI in MFC projects...for sometimes it works, sometimes it doesn't, sometimes I fill a rectagle with a needed color, but when the window is not active, the color disappeares...

    I don't get it...*hits the table with the head*

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    *You* can call onpaint whenever. Onidle is a good place. Its automatically called when the window is min/maxed or another window uncovers it, couple of other times. I don't know about gdi, but there are some pretty good direct draw examples out there.

  3. #3
    Join Date
    Mar 2005
    Location
    third world
    Posts
    10
    Hmm...let us say that OnPaint the bg of the main window changes to black, if I want to change it to yellow when a button is pressed, how can I call OnPaint again to do that..? That function takes 0 parameters...

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    I think you should be able to do the gdi code in the button handler and then explicitly call onpaint() at the end of the button and that should work, if not, something is wrong.

  5. #5
    Join Date
    Mar 2005
    Location
    third world
    Posts
    10
    ...the color I use to change the bg on button pressed, written in the button handler

    void CColorDlg::OnButton()
    {
    CPaintDC dc(this);
    CBrush b;
    RECT t;
    b.CreateSolidBrush(RGB(0,0,0));
    GetClientRect(&t);
    dc.FillRect(&t,&b);
    }

    ...it gives me no errors as well as no results...on the other hand if I put it into the OnPaint function body - the bg will be red from the very beginning...

    ...I just don't get what am I doing wrong...

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    void CColorDlg::OnButton()
    {
    CPaintDC dc(this);
    CBrush b;
    RECT t;
    b.CreateSolidBrush(RGB(0,0,0));
    GetClientRect(&t);
    dc.FillRect(&t,&b);
    OnPaint(); //did you add this line? and it does nothing? and they have the same 'this'?
    }

    if that did not do it, throw in a updatewindow or updatedialogitem and see if that clears it up. ?? I don't really know but it *should* work after one of these changes.

  7. #7
    Join Date
    Mar 2005
    Location
    third world
    Posts
    10
    *sigh*

    ...all painting seem to be done in the OnPaint function...

    ...thence to change the bg color I have to declare
    RECT t;
    CBrush b;
    in the dialog class, then in OnPaint function write:

    void CWhateverDlg::OnPaint()
    {
    CPaintDC dc(this);
    GetClientRect(&t);
    dc.Rectangle(t.left,t.top,t.right,t.bottom);
    dc.FillRect(&t,&b);
    }

    and on button:

    void CWhateverDlg::OnButton()
    {
    b.CreateSolidBrush(RGB(255,0,0));
    this->InvalidateRect(0);
    }

    ...then it works...otherwise - no...

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    I remember doing that now... onpaint called invalidaterect(everything) which forced onpaint to execute in a tight loop, as fast as it could go. It either had everything hardcoded or accessed say, an rgb array that was a class member (modified wherever) to know what to paint. I never got far into that before I switched to direct draw/ d3d and open gl (gl first, directx later). There should be a better way to use the gdi, but I never found one and really needed more power than it offered.

    please post it if you figure out the proper way to do the gdi... assuming that this is not the best way...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center