Changing a window in new function

Hey guys! I realize this is a pretty dumb question but I am completely oblivious to programming with windows, as my education only taught me some very basic c++ console programming, but I find it so interesting I decided to learn how to program with windows aswell but I find there's a lot fewer resources, so my problem is this:

I managed to generate a window, and draw in it. Now I thought I would make a function that takes in two values x and y (which in this case are pixels locations in my window) and then draws per say, the letter J at those coordinates.

I can easily draw the letter J with any colour I wish by using SetPixel, but as said I would like the program to draw only when the function to draw is called.
Obviously, I need to know what needs to be sent to my window, and what needs to be returned. I tried with the following two functions, one sending the window and one sending the HDC, but neither worked.

void drawJ (int x, int y, HWND hwnd)
and
void drawJ (int x, int y, HDC hdc)

Both attempts returning the same error message; MULTIPLE DEFINITIONS OF drawJ and then it basically points to a seemingly random line in my main program.

I may be trying to make things too simplistic and maybe in reality it is much more complex, but I see no reason as to why it should be. All I want to do is seperate it so I can draw the letter J at any time in the program, which kind of is the whole point of object-oriented programming.

Any help would be greatly appreciated!
Can you post the code?

btw, SetPixel function is slow, if you want to draw text, you can use DrawText and TextOut functions

DrawText http://msdn.microsoft.com/en-us/library/dd162498(VS.85).aspx
TextOut http://msdn.microsoft.com/en-us/library/dd145133(VS.85).aspx
Thanks for the comments about SetPixel, but since this is my first windows program I don't really care yet :p.

There isn't really much sample code, but I'll show an example of what I'm trying to do, although this code will just show one of the 10 things I tried to make a decent function call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <windows.h>
#include "drawE.cpp"
#define WNDCLASSNAME "wndclass"

HDC hdc;
HWND hwnd;

bool quit = false;


// The event handler



LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
        switch(msg)
        {
                   case WM_CLOSE:
                        {
                   PostQuitMessage(0);
                   break;
                         }
        }
                   return DefWindowProc(hwnd, msg, wparam, lparam);
}




int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{
    MSG msg;
    WNDCLASSEX ex;
    
    ex.cbSize = sizeof(WNDCLASSEX);
    ex.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
    ex.lpfnWndProc = WinProc;
    ex.cbClsExtra = 0;
    ex.cbWndExtra = 0;
    ex.hInstance = hinstance;
    ex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    ex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
    ex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    ex.lpszMenuName = NULL;
    ex.lpszClassName = WNDCLASSNAME;
    ex.hIconSm = NULL;
    
    
    RegisterClassEx(&ex);

    
    hwnd = CreateWindowEx(NULL,
                               WNDCLASSNAME,              
                               "window",
                               WS_OVERLAPPEDWINDOW,
                               0, 0,
                               300, 300,
                               NULL,
                               NULL,
                               hinstance,
                               NULL);
                               
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);
    
    
    
HDC hdc = GetDC(hwnd);

    
   while(!quit)
    {
               
        if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                quit = true;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        
       drawJ(10, 10, hdc); // This obviously doesn't work
    }
    
    
ReleaseDC(hwnd, hdc);

    
    return msg.lParam;
}


// DrawE.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <windows.h>

void drawJ(int x, int y, HDC &hdc)
{
     

               
             
              for (int i = 0; i < 25; i++)
        {
            x++;
            SetPixel(hdc, x, y, RGB(rand()%255, rand()%255, rand()%255));
        }
        
                
                for (int i = 0; i < 50; i++)
        {
            y++;
            SetPixel(hdc, x, y, RGB(rand()%255, rand()%255, rand()%255));
        }

                for (int i = 0; i < 20; i++)
        {
            x--;
            SetPixel(hdc, x, y, RGB(rand()%255, rand()%255, rand()%255));
        }
        
                for (int i = 0; i < 20; i++)
        {
            y--;
            SetPixel(hdc, x, y, RGB(rand()%255, rand()%255, rand()%255));
        }
       
        
        
}



So as you can see, all I'm trying to do is put the code for drawing the letter J in a seperate function.
Molecular, what you desperately, desperately, desperately, desperately need to do is get yourself a book on Windows programming. And start in chapter 1. Then do chapter 2. Etc.

As far as that program of yours (and I desperately hope you don't take this the wrong way); just throw it out. That isn't how its done.

Fred

But, in the way of encouragement, good try!

PS:

1) Don't put any code in WinMain() besides code to A) Register A Window Class; B) Create The Window; C)Enter a proper message loop. Not one like you've got;

2) Put all code in the Window Procedure or functions called from within the Window Procedure;

3) Try to refrain from the use of GetDC() until you become very experienced at Window SDK style programming. All drawing should be done in WM_PAINT handlers.

One of the mose egregious errors you are making is failing to understand the role and importance of the Window Procedure.
Last edited on
Thanks! I was very much awaiting a reply telling me I had no idea what I'm doing, because truth is I don't :p.

This was merely an attempt at learning how it all hangs together, but I'm guessing I'm way off. I went to the link and it seems there are others asking my very question, so I read up on the links you posted there and I'm likely to go grab a book any time soon ^^.
Topic archived. No new replies allowed.