Click here to Skip to main content
15,883,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello people.

I am drawing rect in my application on button click.
but i want draw it on start of application. as i am drawing on OninitDialog function it will not drawing anything on it.

so how can i draw it on starting application?

What I have tried:

void XYZ::OnBnClickedOk()
{
CRect rc;
m_grpDisp.GetClientRect(&rc);
ScreenToClient(&rc);
int width = rc.Width();
int height = rc.Height();

// Get the Device Context
CClientDC dc(GetDlgItem(IDC_GDISP));
// Create a new pen
CPen lDot(PS_DASH, 2, RGB(255, 255, 255));
CPen lPoint(PS_SOLID, 4, RGB(0, 0, 255));
CPen lLine(PS_SOLID, 8, RGB(255, 0, 0));

CBrush grey(RGB(240, 240, 240));
dc.SelectObject(&grey);
dc.Rectangle((width/2)-445,8,(width/2)+445,height-3);
}
Posted
Updated 3-Aug-18 22:39pm
v2
Comments
Member 12533122 4-Aug-18 5:39am    
i am drawing it on button click . so when i am clicking button it will draw that on display.
but i want to draw that when application starts

If you draw objects outside of the function that responds to the WM_PAINT message, then it will not work. As soon as the dialog is shown it will erase the background and redraw all controls in the resource. You should add the rectangle into your resource settings.
 
Share this answer
 
Comments
Member 12533122 4-Aug-18 5:39am    
i am drawing it on button click . so when i am clicking button it will draw that on display.
but i want to draw that when application starts

How can i draw that?
Richard MacCutchan 4-Aug-18 7:09am    
It is not really clear what you are trying to do. If you need a rectangle in a dialog then the easiest way is to add a STATIC control with one of the styles described in Static Control Styles | Microsoft Docs[^].
Member 12533122 4-Aug-18 7:59am    
basically i want to draw some lines and rectangle. i want to draw on screen with different inputs.
Richard MacCutchan 4-Aug-18 8:20am    
Yes, not very useful information though. If this is a Dialog application then it does not really lend itself to such a feature, without quite a lot of work. You need to capture the WM_PAINT message and handle it yourself, before passing it on to all the other controls. If it is a normal Windows application then you just do it all in the OnPaint handler.
Another solution is to use PostMessage for a WM_PAINT message which triggers the painting of your control. Best is to use InvalidateRect for the control before.

Tip: Create the pens only once, as class members or as static objects to improve performance.
 
Share this answer
 
Comments
Member 12533122 4-Aug-18 5:39am    
i am drawing it on button click . so when i am clicking button it will draw that on display.
but i want to draw that when application starts

How can i draw that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900