Solved

win32 question

Posted on 2001-08-09
5
599 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

729 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