Click to See Complete Forum and Search --> : Fatboy is confused.........


Fatboy
August 18th, 2004, 01:27 PM
Hey y'all! I programmed this:





#include <windows.h>
#include "Resource.h"

LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MSG msg;
static TCHAR szAppName[] = TEXT("Test program!!!!!");
WNDCLASSEX wndclass;
HWND hWindow;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
wndclass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST_SM));
wndclass.hCursor = LoadCursor(0, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = szAppName;
if (!RegisterClassEx(&wndclass))
return 0;
hWindow = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640 + GetSystemMetrics(SM_CXFIXEDFRAME) * 2, 480 + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION), 0, 0, hInstance, 0);
ShowWindow(hWindow, iCmdShow);
UpdateWindow(hWindow);
HDC hDC;
PAINTSTRUCT ps;
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
SelectObject(hDC, hPen);
hDC = BeginPaint(hWindow, &ps);
Rectangle(hDC, 32, 32, 128, 128);
DeleteObject(hPen);
EndPaint(hWindow, &ps);
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}



I'm sorry for throwing all this code at you, but please have a look. What do you expect to happen when this is compiled? The reason why I wrote this was to try drawing outside the WndProc function switch statement, i.e. in the WinMain itself. I'm probably making a silly mistake, but.............it compiles fine, and runs fine. Well not really, because I was expecting the window to show a rectangle, but it shows nothing! As if I didn't write the painting code. Why?

Marc G
August 18th, 2004, 03:04 PM
BeginPaint should only be used with WM_PAINT (see MSDN)!

Why do you want to paint outside WM_PAINT?
You shouldn't do that.
If the user moves another window ontop of your window, your drawing will be erased and so you'll need to redraw it in WM_PAINT.


PS: you want to consider choosing a better topic title next time ;)
PS2: There is something called CODE tags to post code in a more readable way ;)

MrViggy
August 18th, 2004, 03:26 PM
For starters:
HDC hDC;
PAINTSTRUCT ps;
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
SelectObject(hDC, hPen);
Won't work at all, since hDC is not initialized.

Oh, and what Marc said! :D

Viggy

Fatboy
August 18th, 2004, 04:18 PM
Yes I understand the redraw problem. But eventually I want to create a loop in the WinMain and create animation.

What if I use GetDC(hWindow)?????? Is there any ways I can paint outside the WndProc?????????????????

Marc G
August 19th, 2004, 06:14 AM
Yes I understand the redraw problem. But eventually I want to create a loop in the WinMain and create animation.

That's not going to work!
If you make a loop inside your WinMain, not a single Windows message will be processed, so your app will seem to be hung.
For Win32 API animation, you could install a timer that fires every 20 ms or so, and that does an Invalidate() which will trigger a WM_PAINT message to be send.

Erik Wiggins
August 21st, 2004, 08:06 AM
The only time I would requmend you placing drawlling code outside of the WM_PAINT is when it is in a function you intend to call from the WM_PAINT. However, you would do it just the same as when you are in the WM_PAINT message. Your problem is with your DC. Try changing your window class to include CS_OWNDC. This should fix your problem.