4

My classmates started using Delphi with pascal but I as c++ porgrammer have to use win32 API. They were changing background color so I need to know this aswell but there are some differencies.

In delphi each form looks like it has it's own instance, and setting background color for one window is just a matter of changing one value. Not the case with win32 where when you change that value it affects every window using that class (after update ofc).

What I need to do is to clear/erase my window background. I can do that with FillRect(..) and it's working very nicely, but I've found also WM_ERASEBKGND which seems to be doing exactly what I need. They mentioned that if this message gets proccessed I should return a non-zero, but they didn't tell how to process it.

So could anything else then FillRect(and similliar) let me erase a window with a brush I defined and not with the default for class?

Thanks

braX
  • 11,506
  • 5
  • 20
  • 33
Raven
  • 4,783
  • 8
  • 44
  • 75
  • If you want to learn the Win API at this low level you should pick up a an old copy of "Programming Windows 3.1" by Charles Petzold (http://www.amazon.com/Programming-Windows-3-1-Charles-Petzold/dp/1556153953) and see where Windows programming started. – Tony Mar 06 '11 at 22:52
  • 1
    where it started? Where it still is today. Just that we wrap it up with frameworks that do the dirty work. – David Heffernan Mar 06 '11 at 22:54
  • @David - That's what I meant but just didn't phrase it very well :) – Tony Mar 06 '11 at 23:03

2 Answers2

13

You process WM_ERASEBKGND simply by erasing the background (using FillRect() is fine).

By returning a non-zero value, you are simply telling Windows that this message has been taken care of and no further action is needed. There is nothing more formal than that.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • Yup as I said I can do that way. But isn't there anything like ClearBackground(handle, color) made specificaly for clearing? It's good to know FillRect() is right way, just curious. – Raven Mar 06 '11 at 22:46
  • ClearBackground == FillRect. You do have to deal with writing more explicit code in win32. Gotta make that brush first. That's why almost anyone uses a class library. – Hans Passant Mar 06 '11 at 22:50
  • @Raven - What do you think `ClearBackground` might be doing in it's method body, probably calling `FillRect()` :) – Tony Mar 06 '11 at 22:50
  • the delphi vcl will no doubt implement it this way. If you have a copy of the delphi source code then you could peruse it you your benefit. You should also read Petzold. – David Heffernan Mar 06 '11 at 22:52
  • @Raven: What does FillRect() functionally do differently than ClearBackground()? Both set all pixels in a rectangle to a specified color. Do this for the entire client area (or update region) and the background is cleared. I'm not really sure what else you are looking for. – Jonathan Wood Mar 06 '11 at 22:57
  • @Jonathan As I said I'm only beggining and I'm used to from openGL to have precise function for everything, but as Hans said it's going to be more explicit :) But I'll get used to that win32 later on.. @David not at the moment but I think it was like "Form1.background = ...." not very useful for me :/ @Hans what libraries you have on mind? – Raven Mar 06 '11 at 23:24
  • @Raven: That's fine. I'm just trying to make it clear. For C++/Windows, the most popular library is probably MFC. – Jonathan Wood Mar 06 '11 at 23:29
  • @Jonathan Ah yes.. Well this is mainly for my exams where I'll be using Visual Studio Express (without MFC I believe) I need to know the "dirty way" as was mentioned. – Raven Mar 07 '11 at 00:39
  • @Raven: Then just handle WM_ERASEBKGND. Your app will be smaller and faster than just about any other Windows application out there. – Jonathan Wood Mar 07 '11 at 01:49
  • @raven hans means things like vcl or mfc. All windows apps do this with FillRect. Frameworks and libraries hide it from you. If you want to learn read Petzold. I can't stress that enough. – David Heffernan Mar 07 '11 at 07:49
  • @raven when in Delphi you write `Form1.BackgroundColor := ...` the framework remembers the colour and then calls `FillRect` with the appropriate colour the next time `WM_ERASEBKGND` comes around! – David Heffernan Mar 07 '11 at 08:28
1

If you're using MFC with C++ you can also check out that framework's implementation of CWnd::OnEraseBkgnd http://msdn.microsoft.com/en-us/library/a0a52fkz(v=vs.80).aspx

hogwell
  • 31
  • 1