Results 1 to 13 of 13

Thread: How to make a simple drawing?

  1. #1
    Aragorn
    Guest

    How to make a simple drawing?

    Here i post the code of a simple function. I want it to create a window and draw a circle in it, but i can't make it.

    [code]
    HWND initEditor(HWND hMainWnd) {
    HWND hewnd;
    HDC hDispl;
    PAINTSTRUCT *lpPaint;


    hewnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szEditClassName,
    "Test Window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 500, 350,
    hMainWnd, NULL, g_hEdInst, NULL);


    ShowWindow(hewnd, SW_SHOWNORMAL);
    UpdateWindow(hewnd);


    hDispl = BeginPaint(hwnd, lpPaint);
    Ellipse(hDispl, 10, 10, 15, 15); // this should draw a circle!!!!
    EndPaint(hwnd, lpPaint);
    }
    [\code]

    Probably there's something wrong with it, but i can't find anything wrong...
    Thanks

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Can you post the full code

    Can you post the full code because if find some words that should not be there. One of them is the hwnd of the window. Because you are not drawing the ellipse in "WndProc" you dont have any "hwnd". Use "hewnd" because that is what you put for your windows' hwnd.

    Just post the full code here and i may be able to fix it for you
    Baaaaaaaaah

  3. #3
    Aragorn
    Guest
    well, the full code is quite large, but this is it.
    I made an error when transcribing the code in here, because the original one is slpitted up in parts, and there i was using some other names for vars. Here it is:

    #include <windows.h>
    #include <wingdi.h>
    #include "nnetwork.rh"

    void draw_cell(HDC handle, int x, int y);
    void DrawNetwork(HWND hwnd);

    static char g_szMainClassName[] = "Main Window class";
    static char g_szEditClassName[] = "Network Editor class";
    static HINSTANCE g_hInst = NULL;
    static HINSTANCE g_hEdInst = NULL;

    class cell {
    float x, y;
    cell(float a, float b) {x=a;y=b;}
    void sep_pos(float a, float b) {x=a; y=b;}
    };

    ////////////////////////////////////////////////////////////////////////////////
    // EDITOR WINDOW PROCEDURES //
    ////////////////////////////////////////////////////////////////////////////////

    void draw_cell(HDC handle, int x, int y) {
    Ellipse(handle, x, y, x+5, y+5);
    }

    void DrawNetwork(HWND hwnd) {
    HDC hDispl;
    PAINTSTRUCT *lpPaint;

    hDispl = BeginPaint(hwnd, lpPaint);
    draw_cell(hDispl, 1, 1);
    EndPaint(hwnd, lpPaint);
    }

    LRESULT CALLBACK EditProc(HWND hewnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    switch(Message)
    {
    case WM_CLOSE:
    DestroyWindow(hewnd);
    break;
    case WM_DESTROY:
    // PostQuitMessage(0);
    break;
    case WM_PAINT:
    DrawNetwork(hewnd);
    default:
    return DefWindowProc(hewnd, Message, wParam, lParam);
    }
    return 0;
    }

    HWND initEditor(HWND hMainWnd) {
    HWND hewnd;

    hewnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szEditClassName,
    "Network Editor",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 500, 350,
    hMainWnd, NULL, g_hEdInst, NULL);

    if(hewnd == NULL)
    {
    MessageBox(0, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
    return 0;
    }

    ShowWindow(hewnd, SW_SHOWNORMAL);
    UpdateWindow(hewnd);

    DrawNetwork(hewnd);
    }

    ////////////////////////////////////////////////////////////////////////////////
    // MAIN WINDOW PROCEDURE //
    ////////////////////////////////////////////////////////////////////////////////

    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    switch(Message)
    {
    case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case ID_EXIT:
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    break;
    case ID_NETEDIT: {
    HWND hewnd;
    hewnd = initEditor(hwnd);
    }
    }
    default:
    return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
    }

    ////////////////////////////////////////////////////////////////////////////////
    // WIN MAIN PROCEDURE //
    ////////////////////////////////////////////////////////////////////////////////

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    WNDCLASSEX MainClass, EditorClass;
    HWND hwnd;
    MSG Msg;

    g_hInst = hInstance;

    // Main Window Class registration
    MainClass.cbSize = sizeof(WNDCLASSEX);
    MainClass.style = NULL;
    MainClass.lpfnWndProc = WndProc;
    MainClass.cbClsExtra = 0;
    MainClass.cbWndExtra = 0;
    MainClass.hInstance = g_hInst;
    MainClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    MainClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    MainClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    MainClass.lpszMenuName = MAKEINTRESOURCE(IDM_MENU1);
    MainClass.lpszClassName = g_szMainClassName;
    MainClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&MainClass))
    {
    MessageBox(0, "Window Registration Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
    return 0;
    }

    // Editor window registration

    EditorClass.cbSize = sizeof(WNDCLASSEX);
    EditorClass.style = NULL;
    EditorClass.lpfnWndProc = EditProc;
    EditorClass.cbClsExtra = 0;
    EditorClass.cbWndExtra = 0;
    EditorClass.hInstance = g_hEdInst;
    EditorClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    EditorClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    EditorClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    EditorClass.lpszMenuName = NULL;
    EditorClass.lpszClassName = g_szEditClassName;
    EditorClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&EditorClass))
    {
    MessageBox(0, "Window Registration Failed!", "Error!",
    MB_OK);
    return 0;
    }

    // Window creation
    hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szMainClassName,
    "Main Window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
    NULL, NULL, g_hInst, NULL);

    if(hwnd == NULL)
    {
    MessageBox(0, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
    return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Main loop
    while(GetMessage(&Msg, NULL, 0, 0))
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }
    return Msg.wParam;
    }

    This is going to be an editor for my project, a neural network editor. But i have to learn how to draw them on screen before i start with the serious stuff... Should i post also the resource file?
    i guess not!

  4. #4
    Aragorn
    Guest
    BTW: How do i make it look nicer? i mean whith indentatoin and stuff like that... thanks alot!

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use [code][/code] tags. Or you could use php tags for colour coding, but then you get an extra newline at the top.

    PS: you shouldn't need to #include <wingdi.h>
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    what is rong with that source code

    What is rong with that code. I did not find any mistake mistake but also I am not at home so I have not tested it. When I test, I will just see what is rong with that.

    PS: Why do you need the resource files except the resource menu file.
    Baaaaaaaaah

  7. #7
    Aragorn
    Guest
    Yeah, i know there's nothing wrong whith that code, but it simply doesn't work. I'm getting a null pointer at
    hDispl = BeginPaint(hwnd, lpPaint);
    so the problem should be located here. Then i never get the circle painted on the window.
    Any guesses?

    Also, i #include wingdi.h for the graphics functions.

    thanks for your help

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You shouldn't need wingdi.h as well, just use windows.h
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Well I got it to work, but for some reason the hDC in your paint code is not returning to correct hDC. But if I directly ask Windows to return it I get it to work.

    Example:

    PHP Code:
    draw_cell(GetDC(hwnd), 11); 
    Also you have done a bad thing according to MSDN. You have Parksie to thank for this

    PHP Code:
    while(GetMessage(&MsgNULL00))   //THIS IS A NO NO

      
    TranslateMessage(&Msg); 
      
    DispatchMessage(&Msg); 


    //CHANGE TO
    while(GetMessage(&MsgNULL00) > 0

      
    TranslateMessage(&Msg); 
      
    DispatchMessage(&Msg); 

    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  10. #10
    Aragorn
    Guest

    WONDERFUL!!

    Thank's A LOT!
    Now i got it to work.
    There is something strange with that null pointer anyway...
    You have helped more than you imagine already.
    I didn't know that the wingdi library is included in the windows library. thankye parksie
    I'll come up with more things, probably, but i see i need a good reference book to windows programming. What would you recommend?
    See you

  11. #11
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I recommend "online msdn" if you dont have msdn on a cd. It is the best refrence for Windows API.

    Here is the main page:

    http://msdn.microsoft.com/library/
    Baaaaaaaaah

  12. #12
    Zaei
    Guest
    Code:
    if(PeekMessage(&msg, my_hWnd, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    This way, your application doesnt wait for windows to get a message before moving on.

    Z.

  13. #13
    Aragorn
    Guest
    Thank you guys. I'll take that into account

    bye

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