|
| kikolani (18) | |
| I need some help. Can you please tell me how to draw in the window of a program after the user clicks a button in the window. I want to have a button that draws a few lines in the program's window when clicked. Please help me. | |
Last edited on | |
| chris (70) | |||
| I've done & commented this example code for you - I hope it helps, the functions used are: http://msdn.microsoft.com/en-us/library/ms534247(VS.85).aspx - MoveToEx http://msdn.microsoft.com/en-us/library/aa453296.aspx - LineTo http://msdn.microsoft.com/en-us/library/ms632680(VS.85).aspx - CreateWindowEx http://msdn.microsoft.com/en-us/library/ms632659(VS.85).aspx - LOWORD
| |||
| george135 (209) | |
| Above sample is wrong. You must always draw on WM_PAINT | |
| Malachi (72) | |
| I'm going to sound like a jerk for saying this but the thread title "Windows Problem" is stating the obvious when you're posting it in the windows board. Perhaps you could choose a more meaningful subject line next time? | |
| kikolani (18) | |
| @Malachi I really don't think so, because that I can't draw is a problem. And it is associated with windows, so I think my title is ok as it says I got a problem with windows. @chris I already read those MSDN articles, but they didn't seem to help me. I will try your code, and I hope it works | |
| Bazzy (3180) | |||
| As george135 said, you have to draw on WM_PAINT message so that what you draw won't be deleted on the next redraw. I would modify the window procedure like this:
A better title would be "Drawing After Button Click" as "Windows Problem" is too generic | |||
Last edited on | |||
| kikolani (18) | |
| @Bazzy I also think your suggestion for my title is better. I am changing my title. I will try your code and see which one is better. | |
| chris (70) | |||
| To george135 & to Bazzy; although WM_PAINT will keep it always there, I am pretty sure (& it does for me anyway) it takes almost all the CPU it can & causes (for me) at least 95% CPU usage. In my opinion, it is better just find the cases where it would need to be redrawn (such as sizing the window or the window getting focus/loosing focus) & then put the code into there. So maybe create a global variable if you need to redraw, then have your cases where it needs to redraw & your button click to activate this. I have used this code below & it works well for me, without the CPU usage of calling it on WM_PAINT:
(also as a note, I forgot to delete in my code, in WindowProcedure, the WM_CREATE case statement - that's not necessary in my code a few posts ago). | |||
Last edited on | |||
| Grey Wolf (1407) | |
| Read: http://msdn.microsoft.com/en-us/library/ms534870(VS.85).aspx | |
| kikolani (18) | |
| @chris I alredy tryed your code, but my compiler gives some kind of strange error that says: 1>d:\data\1\tajjada_projects\tfifview\tfifviewdlg.cpp(291) : error C2440: '=' : cannot convert from 'CDC *' to 'HDC' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Please help me solve that error. | |
| Grey Wolf (1407) | |
| Can you give more info on the project type, IDE/compiler? I'm guessing that you have an MFC project (of some description) from the CDC* reference. | |
Last edited on | |
| kikolani (18) | |
| I am using VC++ 2008 and I have an MFC Project. | |
| Grey Wolf (1407) | |||||
| It that case you put your drawing code on the OnPaint() method of the dialog class. It will look something like this:
In the else part of the if construct is where you would put your code.
Hope that helps | |||||
| kikolani (18) | |
| But is that the code for drawing only on the click of a button or my drawings are always going to stay there? | |
| Grey Wolf (1407) | |||||
No, this is just the correct place to put your drawing code. As Bazzy said, You would have a variable in your view class such as static bool draw = false;You can then wrap your drawing code in an if clause:
Then finally your event handler for the button click would be somthing like:
| |||||
This topic is archived - New replies not allowed.
