Solved

win32 question

Posted on 2001-08-09
5
591 Views
Last Modified: 2008-02-01
I wanna do a win32 programming that when i right click my mouse, it will display "hello" and when i drag it from a position to another position.... it come out with many many shadow with the word "hello". What is the problem actually?

below is my program:

#include<windows.h>

long FAR PASCAL WndProc(HWND,UINT,WPARAM,LPARAM);

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
     HWND hWnd;
     MSG msg;
     WNDCLASS wc;

if(!hPrevInstance)
{
     wc.style = NULL;
     wc.lpfnWndProc = (WNDPROC) WndProc;
     wc.cbClsExtra = 0;
     wc.cbWndExtra = 0;
     wc.hInstance = hInstance;
     wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
     wc.hCursor = LoadCursor(NULL,IDC_ARROW);
     wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
     wc.lpszMenuName = NULL;
     wc.lpszClassName = "BATCH 23";
     if(!RegisterClass(&wc))
     {
          MessageBox(0,"Could not register WNDCLASS wc","Application",MB_OK);
          return FALSE;
     }
}

hWnd = CreateWindow("BATCH 23","Sample window", WS_OVERLAPPEDWINDOW,10,10,200,300,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
     MessageBox(0,"Could not create window","Application",MB_OK);
     return FALSE;
}
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&msg,0,0,0))
{
     TranslateMessage(&msg);
     DispatchMessage(&msg);
}

return(0);
} //end of WinMain()

long FAR PASCAL WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     int Xpos,Ypos;
     HDC hdc;
     static POINT current,last;
     static int flag=0;

     switch(message)
     {
          case WM_RBUTTONDOWN:
               current.x=last.x=LOWORD(lParam);
               current.y=last.y=HIWORD(lParam);
               SetCapture(hWnd);
               flag=1;
               break;
          case WM_MOUSEMOVE:
               current.x=LOWORD(lParam);
               current.y=HIWORD(lParam);
               if(flag==1)
               {
                    hdc=GetDC(hWnd);
                    SetROP2(hdc,R2_NOT);
                    //ERASE THE PREVIOUS LINE
                    MoveToEx(hdc,current.x,current.y,NULL);
                    LineTo(hdc,last.x,last.y);
                    MoveToEx(hdc,current.x,current.y,NULL);
                    Xpos=LOWORD(lParam);
                    Ypos=HIWORD(lParam);
                    TextOut(hdc,Xpos,Ypos,"Hello",5);
                    ReleaseDC(hWnd,hdc);
               }
               last.x=current.x;
               last.y=current.y;
               break;
          case WM_RBUTTONUP:
               last.x=LOWORD(lParam);
               last.y=HIWORD(lParam);
               hdc=GetDC(hWnd);
               MoveToEx(hdc,current.x,current.y,NULL);
               LineTo(hdc,last.x,last.y);
               ReleaseDC(hWnd,hdc);
               ReleaseCapture();
               flag=0;
               break;
     case WM_DESTROY:
               PostQuitMessage(0);
               break;
     default:
          return DefWindowProc(hWnd,message,wParam,lParam);
     }
     return 0;
}



0
Comment
Question by:fl78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6367091
>What is the problem actually?

Hmmm, I just can repeat your question, what is the problem?
Does you code works or if it doesn't than what is doesn't works? Can you make yourself more clear?
0
 
LVL 1

Accepted Solution

by:
Aggarwal earned 100 total points
ID: 6367107
It becasue on RBUTTONDOWN u are setting the flag and not doing anything else..

u are displaying "Hello" on MouseMove ..that's the reason for getting number of shadows !!

what u can do is ..
display Hello on RBUTTONDOWN ..

and on ONMOUSEMOVE set some flag ..

now in RBUTTONUP ..check for this flag ..if it is set ..refreash the screen and display hello at new location !! Otherwise ..do nothing ??????
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 6367126
Ahh, I think I can catch what are you trying to do. You have to use WM_PAINT, only in that handler you have to write. On MouseMove you have to invalidate your window, use something like InvalidateRect(hWnd, NULL, TRUE) for that
0
 

Author Comment

by:fl78
ID: 6369061
Aqqarwal..... how to refresh the screen??
0
 

Expert Comment

by:trongnghia
ID: 6371203
I think the answer of MichaelS is correct.

And to refresh the screen you can use InvalidateRect(...) function to add the rectangle of drawing text "Hello"
to the specified window's update region.
The invalidated areas is processed when the next WM_PAINT message occurs. The system sends this message when there are no other messages in the application's message queue
and the invalidated areas is not null.

Cheers,
TrongNghia

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question