CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2015
    Posts
    2

    Displaying shapes in the same client area in win32

    case WM_RBUTTONDOWN:

    InvalidateRect(hwnd,NULL,FALSE);
    return 0;

    case WM_RBUTTONUP:
    GetClientRect(hwnd,&rect);
    SendMessage(hwnd,WM_LBUTTONUP,wParam,lParam);
    InvalidateRect(hwnd,&rect,TRUE);

    return 0;

    Hi I have managed to create a rectangle and to draw shapes in the client area using the above code. Now I want to display the shape drawn using mouse in the rectangle. Pls help

  2. #2
    Join Date
    Feb 2015
    Posts
    2

    Re: Displaying shapes in the same client area in win32

    #include<windows.h>
    #define MAXPOINTS 1000

    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow)
    {
    static TCHAR szAppName[]=TEXT("Shapes");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszClassName=szAppName;
    wndclass.lpszMenuName=NULL;

    if(!RegisterClass(&wndclass))
    {
    MessageBox(NULL,TEXT("Class Not registered"),szAppName,MB_ICONERROR);
    return 0;
    }

    hwnd=CreateWindow(szAppName,TEXT("SHAPES"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);




    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }

    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    HDC hdc;
    static int x,y;
    int i;
    static POINT pt[MAXPOINTS] ;
    static int iCount ;
    HPEN hpen;
    RECT rect;

    switch(msg)
    {
    case WM_PAINT:
    GetClientRect(hwnd,&rect);
    hdc=GetDC(hwnd);
    SetMapMode(hdc,MM_ISOTROPIC);
    SetWindowExtEx(hdc,10,10,NULL);
    SetViewportExtEx(hdc,10,10,NULL);
    DPtoLP(hdc,NULL,4);
    Rectangle(hdc,15,20,200,200);
    ReleaseDC(hwnd,hdc);
    return 0;



    case WM_LBUTTONDOWN:
    InvalidateRect(hwnd,NULL,TRUE);
    return 0;

    case WM_MOUSEMOVE:

    if(wParam & VK_LBUTTON)
    {
    InvalidateRect(hwnd,NULL,FALSE);
    pt[iCount ].x=LOWORD(lParam);
    pt[iCount++].y=HIWORD(lParam);
    hdc=GetDC(hwnd);
    //hpen=CreatePen(PS_SOLID,2,RGB(0,0,220));
    SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0) ;
    for(i=0;i<iCount-1;i++)
    {
    MoveToEx(hdc,pt[i].x,pt[i].y,NULL);
    LineTo(hdc,pt[i+1].x,pt[i+1].y);
    }
    ReleaseDC(hwnd,hdc);
    }
    return 0;

    case WM_LBUTTONUP:
    InvalidateRect(hwnd,NULL,FALSE);
    iCount=0;
    GetClientRect(hwnd,&rect);

    return 0;





    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    }
    return DefWindowProc(hwnd,msg,wParam,lParam);
    }


    This is the actual code

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Displaying shapes in the same client area in win32

    Please read http://forums.codeguru.com/announcement.php?f=7 regarding posting as your code is not very readable. When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

Featured