GetWindowExt返回cx = 1和cy = 1 [英] GetWindowExt returns cx = 1 and cy = 1

查看:67
本文介绍了GetWindowExt返回cx = 1和cy = 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





为了测试目的,我有一些代码用于绘制对话框中的图片控件(ID = IDC_GRAPH_D_GRAPH)中的一行:



Hi,

I have this bit of code to draw, for testing purposes, a line in a picture control (ID = IDC_GRAPH_D_GRAPH) located in a dialog box:

void CGraphDlg::ShowGraph(void)
{
	m_iCount++;
	CPaintDC dc (GetDlgItem(IDC_GRAPH_D_GRAPH));
	CSize cs = dc.GetWindoxExt();
	CPen lpen(PS_SOLID, 1, RGB(255, 0, 0));
	dc.SelectObject(&lpen);
	dc.LineTo(100,m_iCount);
}





它工作正常,因为它确实在图片控件中绘制了预期的行,因此声明dc和从它到相关图片控件的链接显然有效。



奇怪的是,CClientDC dc的成员函数GetWindowExt总是返回值为cx =的CSize 1和cy = 1,而实际的图片控件大小是单程几百像素。

我现在不使用cx和cy但是我想。



显然我必须遗漏一些东西,任何人都知道它可能是什么?



我正在使用:Visual Studio 2010,它的一个非常基本的MFC对话框应用程序。



任何帮助非常感谢。



It works OK because it does draw the line as expected in the picture control so the declaration of dc and the link from it to the picture control in question clearly works.

Oddly enough though the member function "GetWindowExt" of "CClientDC dc" always returns a CSize with values cx = 1 and cy = 1 while the actual picture control size is several hundred pixels each way.
I am not using cx and cy for now but I would like to.

Obviously I must be missing something, anybody have an idea what it might be?

I am using: Visual studio 2010, and its a very basic MFC dialog box application.

Any help much appreciated.

推荐答案

CPaintDC对象只能在响应WM_PAINT消息时使用。



所以我猜你在处理WM_PAINT消息时没有调用它e用于图片控制。



问候

Espen Harlinn
A CPaintDC object can only be used when responding to a WM_PAINT message.

So I''m guessing that you''re not calling this during the processing of the WM_PAINT message for the picture control.

Regards
Espen Harlinn


根据MSDN文档,CPaintDC可以仅在响应WM_PAINT消息时使用,通常在OnPaint消息处理程序中。它在内部发出一个BeginPaint - EndPaint序列,它请求无效的窗口区域,最后验证该区域。



如果你从任何其他地方调用ShowGraph,它不能工作。



要么使用CClientDC,要么确保在WM_Paint消息处理中调用你的ShowGraph函数。



增加:



如果要检索窗口大小,请使用GetWindowRect或GetClientRect。 GetWindowExt提供其他内容,即视口映射的目标大小。你必须明确地设置它们。我想回想一下,他们默认设置为1.
According to MSDN documentation CPaintDC can only be used when responding to a WM_PAINT message, usually in the OnPaint message handler. It emits internally a BeginPaint - EndPaint sequence, which requests the invalid window area and, at the end, validates that region.

If you called ShowGraph from any other place, it can''t work.

Either use CClientDC, or make sure your ShowGraph function is called inside the WM_Paint message handling.

ADDED:

When you want to retrieve the size of the window, use GetWindowRect or GetClientRect. GetWindowExt delivers something else, namely the target size of the viewport mapping. You have to explicitly set those. I think to recall that they be default are set to 1.






需要一段时间来解决问题但我终于破解了它。



根据我从nv3和Espen Harlin获得的信息以及一个非常有启发性的视频,我找到了http://msdn.microsoft.com/en-us/vstudio/cc843613 我介绍了一个基于类Cwnd并集成的自定义对话框控件Called CGraph它进入我的对话框应用程序。

一旦你知道如何将它集成到对话框中最终会变得容易。只需在对话框中添加一个自定义控件并将其类指定为GraphClass(参见c ++代码)就可以了。



它现在运行良好并且可以完成所有操作我希望并期望它能做到。

实际上它只是另外一个明显有点明显指出的案例。



什么是真正的欺骗我是当对话框初始化窗口能够并且在对话框的主要WM_PAINT消息和后续OnPaint()函数的基础上工作时,它之后再也不会重复它。

结果是我确实获得了预期的图形,代码忠实地再次执行而没有任何错误,但它没有重绘。



感谢所有的帮助。 />


这是新类的头文件:



Hi,

It took a while to sort things out but I have finally cracked it.

Based on the info I got from nv3 and Espen Harlin and a very instructive video I found http://msdn.microsoft.com/en-us/vstudio/cc843613 I introduced a custom dialog control Called CGraph based on class Cwnd and integrated it into my dialog box application.
Integrating it into the dialog box is ultimately dead easy once you know how. Simply adding a custom control to the dialog and specify its class to be "GraphClass" (see c++ code) does the trick.

It works very well now and does everything I want and expect it to do.
In reality its just another case of "Glaringly obvious once pointed out".

What really tricked me is that when the dialog box initialises windows is capable of and does work on the basis of the dialog box''s main WM_PAINT message and subsequent OnPaint() function, it just never repeats it afterwards.
The result was that I did get the expected graph, the code faithfully executed again without any error but it did no more redraw.

Thanks for all the help.

This is the header file for the new class:

#pragma once
#include "ToolBox.h"
#include "Data.h"

class CGraph : public CWnd
{
	DECLARE_DYNAMIC(CGraph)
public:
	CGraph();
	virtual ~CGraph();

	CWorkData workData;	
protected:
	BOOL CGraph::RegisterWndClass();
	static TCHAR m_szWndClass[];
	afx_msg void OnPaint();
	DECLARE_MESSAGE_MAP()
private:
	void ShowGraph(void);
	int m_iCount;
};





这是处理它的c ++代码:





This is the bit of c++ code that handles it:

// GraphDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Eerste.h"
#include "Graph.h"

IMPLEMENT_DYNAMIC(CGraph, CWnd)

TCHAR CGraph::m_szWndClass[] = _T("GraphClass");

BEGIN_MESSAGE_MAP(CGraph, CWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()

CGraph::CGraph()
{
	RegisterWndClass();
	m_iCount = 0;
}

CGraph::~CGraph()
{
}

BOOL CGraph::RegisterWndClass()
{
	WNDCLASS wndclass;
	if (::GetClassInfo(AfxGetInstanceHandle(),m_szWndClass, &wndclass))
		return TRUE;
	wndclass.style = 0;
	wndclass.lpfnWndProc = ::DefWindowProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = ::AfxGetInstanceHandle();
	wndclass.hIcon = NULL;
	wndclass.hCursor = NULL;
	wndclass.hbrBackground = NULL;
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = m_szWndClass;
	return::AfxRegisterClass(&wndclass);
}

void CGraph::OnPaint()
{
	ShowGraph();
	CWnd::OnPaint();
	int fdtest =1;
}

void CGraph::ShowGraph(void)
{
	RECT rect;
	GetClientRect(&rect);
	CPaintDC dc (this);
	dc.SetWindowExt(rect.left,rect.bottom);
	CPen lpen(PS_SOLID, 1, RGB(255, 0, 0));
	dc.SelectObject(&lpen);
	m_iCount +=10;
	dc.LineTo(100,m_iCount);
}


这篇关于GetWindowExt返回cx = 1和cy = 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆