c ++在mfc应用程序中获得dc [英] c++ get dc in mfc application

查看:94
本文介绍了c ++在mfc应用程序中获得dc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

O有这个控制台应用程序

Hello,
O have this console application

void main()
{
    double i,c,d,f,g,e;
	HDC hdc = GetDC(GetDesktopWindow());
    DWORD ret = GetDeviceCaps(hdc, HORZSIZE);
    DWORD ret2 = GetDeviceCaps(hdc, VERTSIZE);
    std::cout << "Your screen size in inches is " << ret/25.4 << "\" x " << ret2/25.4 << "\" \n";
    ReleaseDC(GetDesktopWindow(), hdc);
	i=ret/25.4;
	c=ret2/25.4;
d=0;
f=0;
d=((i*i)+(c*c));
g=sqrt(d);



工作正确,但在mfc我收到错误


Works correct, but in mfc I get an error here

HDC hdc = GetDC(GetDesktopWindow());



说不能拿1个参数是否有错误输入代码,mfc有什么不同? />


第二个问题:在c ++ win form应用程序中我可以添加为dll纯本机c ++吗?



谢谢


says "cannot take 1 argument" is there any wrong typing in code, it is different in mfc?

And a second question : in c++ win forms application can I add as a dll pure native c++?

thank you

推荐答案

关于第一个问题,请看我对这个问题的评论;另见:

http ://msdn.microsoft.com/en-us/library/windows/desktop/dd144871%28v=vs.85%29.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx> http://msdn.microsoft.com/en-us /library/windows/desktop/ms633504%28v=vs.85%29.aspx [ ^ ]。



不要使用MFC在此刻。请参阅cariolihome对问题的评论:您可以使用范围运算符'::'来表示使用全局命名空间

http://www.cplusplus.com/doc/tutorial/operators [ ^ ],

http://msdn.microsoft.com/en-us/library/5cb46ksf.aspx [ ^ ],

http://www.cplusplus.com/doc/tutorial/namespaces [ ^ ]。



使用MFC,你应该有一些窗口的实例,所以你不要需要一个HWND参数;相反,使用实例this: http://msdn.microsoft.com/en-us/ library / 71eseab0.aspx [ ^ ]。



(你明白实例函数调用是如何工作的吗?你有一个额外的(在这种情况下只有一个)参数this,它是隐式的。它用于将类型的实例传递给方法。而且,实现知道这个对象的HWND并在调用Windows API的 GetDC(HWND)时使用它)我在上面引用过。请参阅我过去的回答: Catch 22 - 当使用它们的功能变为静态时,指向接口对象的指针会死亡。 [ ^ ]。)
关于你的第二个问题...



可能你不是指C ++ ,但是你可以在.NET中使用C ++ / CLI,特别是 System.Windows.Forms 。如果是这样,您也可以在此类项目中使用一些非托管代码。通常的方法是使用P / Invoke,但是,使用C ++ / CLI,您不必这样做;它使一切变得更加简单。您可以创建一个混合模式(托管+非托管)项目,您可以在其中自由组合.NET和非托管代码。但是,我建议保持一定的纪律。将项目分成至少3个不同的部分或层:纯.NET(托管)部分,纯非托管部分,以及在托管ref类中包装非托管类型的层。请参阅:

http://en.wikipedia.org/wiki/C %2B%2B / CLI [ ^ ],

http://www.ecma-international .org / publications / standards / Ecma-372.htm [ ^ ],

http:// www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ]。



-SA
For your first question, please see my comment to the question; see also:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx[^].

Don't use MFC at this point. See the comment to the question by cariolihome: you can use scope operator '::', to indicate the use of the global namespace:
http://www.cplusplus.com/doc/tutorial/operators[^],
http://msdn.microsoft.com/en-us/library/5cb46ksf.aspx[^],
http://www.cplusplus.com/doc/tutorial/namespaces[^].

With MFC, you should have the instance of some window, so you don't need a HWND argument; instead, the instance "this" is used: http://msdn.microsoft.com/en-us/library/71eseab0.aspx[^].

(Do you understand how instance function calls work? You do have one extra (only one in this case) argument "this", which is implicit. It is used to pass the instance of the type to the method. And than, the implementation "knows" the HWND of this objects and uses it in the call to the Windows API's GetDC(HWND) I referenced above. Please see also my past answer: Catch 22 - Pointers to interface objects die when function using them is made static.[^].)
As to your second question…

Probably, you don't mean C++, but C++/CLI you could use with in .NET in general, System.Windows.Forms in particular. If so, you can use some unmanaged code in such project, too. The usual way is to use P/Invoke, but, with C++/CLI, you don't have to do it; it makes everything much simpler. You can create a mixed-mode (managed + unmanaged) project where you can freely combine .NET and unmanaged code. However, I would advise to keep some discipline. Separate your project in at least 3 distinct parts or layers: pure .NET (managed) part, pure unmanaged part, and the layer where you wrap unmanaged types in managed "ref" classes. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

—SA


#include "stdafx.h"
#include "tttest.h"
#include "tttestDlg.h"
#include <windows.h>
#include <iostream>
#include <complex>


这篇关于c ++在mfc应用程序中获得dc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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