Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

win32 question

Posted on 2001-08-09
5
Medium Priority
?
623 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
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

782 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