Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to draw a bit map when mouse is pressed ?

How to draw a bit map when mouse is pressed ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialgraphicshelpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Surivevoli
    wrote on last edited by
    #1

    I want to draw a bitmap,(droughts for example)on dialog, at mouse's coordinate when it's pressed ! Could you help me ! Lgx

    B L 2 Replies Last reply
    0
    • S Surivevoli

      I want to draw a bitmap,(droughts for example)on dialog, at mouse's coordinate when it's pressed ! Could you help me ! Lgx

      B Offline
      B Offline
      Bram van Kampen
      wrote on last edited by
      #2

      :^)This is Not a simple Problem, and a Full solution for your own custom application will resort to a long list of API's The Outline would be as follows:- -Create a Dialog with a Bitmap Resource in it. -Assign a Bitmap -In the Dialog, implement the OnPaint Method to paint the Bitmat,taking account of where you want it painted. -Use the WM_MOUSEBUTTON messages to capture the mouse when it is clicked over the immage -Use the WM_MOUSEMOVE messages to track the mouse, and to issue WM_PAINT messages to keep the image following the mouse. (The Latter is not as simple as it seems)as you will discover. You will also see that the easier UpdateWindow Call does not work for this in a dialog, it only sends a WM_PAINT after you finally release the mouse button. As I said, Not a simple task, there is not a single API available that does the lot, Also, not a task for beginners(Choose something simpler, i.e. Move the Image to where the mouse was released, without dragging), at the same time, we all have to start somewhere. The prize is, that when you master this, you have learned a lot about the Windows API. Let me know how you get on Regards LateNightsInNewry

      S 1 Reply Last reply
      0
      • B Bram van Kampen

        :^)This is Not a simple Problem, and a Full solution for your own custom application will resort to a long list of API's The Outline would be as follows:- -Create a Dialog with a Bitmap Resource in it. -Assign a Bitmap -In the Dialog, implement the OnPaint Method to paint the Bitmat,taking account of where you want it painted. -Use the WM_MOUSEBUTTON messages to capture the mouse when it is clicked over the immage -Use the WM_MOUSEMOVE messages to track the mouse, and to issue WM_PAINT messages to keep the image following the mouse. (The Latter is not as simple as it seems)as you will discover. You will also see that the easier UpdateWindow Call does not work for this in a dialog, it only sends a WM_PAINT after you finally release the mouse button. As I said, Not a simple task, there is not a single API available that does the lot, Also, not a task for beginners(Choose something simpler, i.e. Move the Image to where the mouse was released, without dragging), at the same time, we all have to start somewhere. The prize is, that when you master this, you have learned a lot about the Windows API. Let me know how you get on Regards LateNightsInNewry

        S Offline
        S Offline
        Surivevoli
        wrote on last edited by
        #3

        thank you ! i am studying to build a program that play chess(or gomoku) over LAN ! i will build it step by step ! there are very much so program ! when you click mouse on coordinate(x,y) on the board, drought (stone) will paint ! i want also do that ! have you ever done that ? Lgx

        B W 2 Replies Last reply
        0
        • S Surivevoli

          thank you ! i am studying to build a program that play chess(or gomoku) over LAN ! i will build it step by step ! there are very much so program ! when you click mouse on coordinate(x,y) on the board, drought (stone) will paint ! i want also do that ! have you ever done that ? Lgx

          B Offline
          B Offline
          Bram van Kampen
          wrote on last edited by
          #4

          No, I have never build a program that play chess(or gomoku) over LAN !. However, I see that you are taking on a very complex task. If I where you, I would break the task up into a number of DLL's Seeing that you want to play some sort of game over a network, you will need somewhere where the score is kept, i.e. a server for the purpose of the game.(This need not be the Network Server). The way I would write this would be, Game server Software, which keps track of the various games being played, and which would need no other interface than something to keep track of players and scores, and Client software, which monitors the Game moves, and communicates same with the Game server. The game Server in this case would be the final arbitor on the legality, or otherwise, of any moves.In this scheme of things, you need to think first of exchange protocols for this type of information, implementation of game rules, etc, etc. I would be inclined at the development stage at least, to implement the clients as a console program, i.e. a Chess Move is implemented by a client by typing something like "E2->E4!, and getting back an error message like: "Err 4087: Sorry, The Piece on Field E2 is a Knight, For a Knight the move E2->E4 is Illegal". After you have this Up,Going and Debugged, you can write some beautiful graphics to simulate the game, using essentially the same messaging service to the server. Hope this is Usefull, LateNightsInNewry

          S 1 Reply Last reply
          0
          • B Bram van Kampen

            No, I have never build a program that play chess(or gomoku) over LAN !. However, I see that you are taking on a very complex task. If I where you, I would break the task up into a number of DLL's Seeing that you want to play some sort of game over a network, you will need somewhere where the score is kept, i.e. a server for the purpose of the game.(This need not be the Network Server). The way I would write this would be, Game server Software, which keps track of the various games being played, and which would need no other interface than something to keep track of players and scores, and Client software, which monitors the Game moves, and communicates same with the Game server. The game Server in this case would be the final arbitor on the legality, or otherwise, of any moves.In this scheme of things, you need to think first of exchange protocols for this type of information, implementation of game rules, etc, etc. I would be inclined at the development stage at least, to implement the clients as a console program, i.e. a Chess Move is implemented by a client by typing something like "E2->E4!, and getting back an error message like: "Err 4087: Sorry, The Piece on Field E2 is a Knight, For a Knight the move E2->E4 is Illegal". After you have this Up,Going and Debugged, you can write some beautiful graphics to simulate the game, using essentially the same messaging service to the server. Hope this is Usefull, LateNightsInNewry

            S Offline
            S Offline
            Surivevoli
            wrote on last edited by
            #5

            http://www.codeproject.com/internet/renju.asp[^] thank you for sincere advice ! sorry for my bad English ! the game that i want buid that don't need game server ! for example, game :HalfLife,age of empire,... all is only a program ! when a person run game program, that person can establish that is server program, other persons who want join in will click "show all game" and see a game that they want play with and therefore this program is client ! score and other information is stored temporality, when game over, that information is lost. the game that i want buid is don't need in real time mode it's turn move by move ! especial, i want that : when game is being played, everybody can see all game is playing, opening(that game is created but nobody join in) the this game have many characteristic similar to multiuser chat program ! the link i wrote upper is renju game program over LAN i hope that you will for me many your advice ! see you !

            1 Reply Last reply
            0
            • S Surivevoli

              thank you ! i am studying to build a program that play chess(or gomoku) over LAN ! i will build it step by step ! there are very much so program ! when you click mouse on coordinate(x,y) on the board, drought (stone) will paint ! i want also do that ! have you ever done that ? Lgx

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              I think you are doing this the wrong way. First you don't really to handle the WM_MOUSEMOVE message unless you are drawing to a particualr size or dragging a bitmap across the screen. Second, you don't need to draw the bitmap to the exact position of the mouse for the game you have mentioned. Consider this. you have a checkerboard, each square being 20x20 pixels, you have a checker piece 15 pixels in diameter. now, if the user clicks the board at say 5,5 of a square (upper left corner), and you draw the piece (from the center) at these coordinates, the piece will overlap into the neighbouring squares. Here's your solution. create a map of your board (an array holding position of each square). Check for user mouse clicks, when user clicks, check the position against those in your array to find the square. Then draw the bitmap to the center of that square. This type of game is called a "tile based game", a search on google will yield many results. Also if you are keen to start coding games, I would recomend a library called Allegro, which enables you to handle bitmaps and user input very easily.

              S 1 Reply Last reply
              0
              • W Waldermort

                I think you are doing this the wrong way. First you don't really to handle the WM_MOUSEMOVE message unless you are drawing to a particualr size or dragging a bitmap across the screen. Second, you don't need to draw the bitmap to the exact position of the mouse for the game you have mentioned. Consider this. you have a checkerboard, each square being 20x20 pixels, you have a checker piece 15 pixels in diameter. now, if the user clicks the board at say 5,5 of a square (upper left corner), and you draw the piece (from the center) at these coordinates, the piece will overlap into the neighbouring squares. Here's your solution. create a map of your board (an array holding position of each square). Check for user mouse clicks, when user clicks, check the position against those in your array to find the square. Then draw the bitmap to the center of that square. This type of game is called a "tile based game", a search on google will yield many results. Also if you are keen to start coding games, I would recomend a library called Allegro, which enables you to handle bitmaps and user input very easily.

                S Offline
                S Offline
                Surivevoli
                wrote on last edited by
                #7

                of course as you say about overlap the thing that i want say is how to paint a bitmap when mouse click event happened and location that bitmap is drawed is belong to coordinate of mouse ! i tryed many code but if i minimize the dialog of program, then nothing to see! void CGomokuDlg::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if((nFlags & MK_LBUTTON)== MK_LBUTTON) { //get Device context: CClientDC dcc(this); //draw pixel: dcc.SetPixel(point.x,point.y,RGB(0,0,0)); } CDialog::OnMouseMove(nFlags, point); } void CGomokuDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); // device context for painting CBitmap bitmap; bitmap.LoadBitmap (IDB_BITMAP1); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap); CPen pen1(PS_SOLID,16, RGB(255,100,0)); dc.SelectObject(&pen1); //dc.StretchBlt(100, 130, 20, 20, &dcMem, // 0, 0, 20, 20, SRCCOPY); dc.MoveTo(0,0); dc.LineTo(30,400); CDialog::OnLButtonDown(nFlags, point); } -- modified at 22:14 Sunday 9th April, 2006

                1 Reply Last reply
                0
                • S Surivevoli

                  I want to draw a bitmap,(droughts for example)on dialog, at mouse's coordinate when it's pressed ! Could you help me ! Lgx

                  L Offline
                  L Offline
                  Laxman Auti
                  wrote on last edited by
                  #8

                  See the following link [http://www.codeproject.com/buttonctrl/hoverbutton.asp](http://www.codeproject.com/buttonctrl/hoverbutton.asp) Knock out "T" from CAN'T You 'CAN' if you think you 'CAN' :cool:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups