Visual C++ NET Tutorials.pdf
(
697 KB
)
Pobierz
14620592 UNPDF
Visual C++ .NET Tutorial
Using the ATL CImage Class
Visual Studio.NET introduced Managed C++, but that wasn't the only thing that was new
in it. A number of interesting changes to MFC and ATL make life simpler for those who
are working in unmanaged C++. In this column I'll introduce you to the CImage class,
which has been added to ATL and adds enhanced bitmap support. Figure 1 shows a
simple application that displays and converts images.
This is an ordinary MFC dialog application. I added a textbox, two buttons captioned
"Load" and "Save As JPG," and a picture control. Then I deleted the Cancel button, and
changed the caption (but not the ID) of the OK button to Done.
Figure 1 -
Displaying and converting images.
Because this is a dialog application, AppWizard created a dialog class for me. I added
variables by right-clicking on the surface of the dialog in the resource editor and
choosing Add Variable. (ClassWizard is gone in this version of Visual Studio, so you
have to learn how to get to the Add Member Variable Wizard dialog.) Here's a summary
of the variables I added, all private:
Control ID
Variable Name Control/Value Variable Type
IDC_PICTURE m_picture
Control
CStatic
IDC_FILENAM
E
m_filename
Value
CString
IDC_SAVE
m_savebutton Control
Cbutton
I used the Properties Window to disable the "Save As JPG" button by default (there's
code later to enable it.) I also changed the Type of the picture control to Bitmap. And
finally, I edited the header file for the dialog class, adding a member variable called
m_image of type CImage. At the top of the header file go these two #include statements:
#include <afxstr.h>
#include <atlimage.h>
It's important that these include statements appear in this order. This is really the only
indication you have that CImage is an ATL class rather than an MFC class.
Here's how simple it is to load a GIF or JPG from the hard drive into a CImage object
and show it in the picture control:
void CImageDlg::OnBnClickedLoad()
{
UpdateData(TRUE);
if (m_filename == "")
m_filename = "Please enter a file name first";
else
{
if (m_image.Load(m_filename) == S_OK)
{
m_picture.SetBitmap((HBITMAP)m_image);
m_picture.Invalidate();
m_savebutton.EnableWindow(TRUE);
}
else
{
AfxMessageBox("File not found or invalid format");
}
}
UpdateData(FALSE);
}
I even have room for a little error checking! I also enable the "Save As JPG" button once
an image is successfully loaded. If you're adapting this code for your own use, don't skip
the call to Invalidate() - it ensures the picture control will redraw itself.
What about converting a GIF to a JPEG? All you have to do is save the image. The
format is implied by the file name. Here's how to do it:
void CImageDlg::OnBnClickedSave()
{
CString filename = m_filename.Left(
m_filename.Find('.')) + ".JPG";
m_image.Save(filename);
}
Two lines of code! How hard is that?
What else can you do with a CImage object? Well, you don't have to load one from an
existing picture file. You can draw on it by creating a device context associated with the
bitmap inside the CImage:
CDC* pDC = CDC::FromHandle(image.GetDC());
Be sure to call ReleaseDC afterwards.
CImage supports transparency, alpha blends and a variety of other cool effects, on
reasonably recent versions of Windows. If you are writing unmanaged (Classic C++)
code that needs to work with images, look into CImage. While it is part of ATL, you
can't tell, can you? There's no sign of templates or anything tricky at all. And in my next
column, I'll show you the Managed C++ equivalent for this application.
Using the ATL CImage Class
Simple Role-Based Security in Managed C++
<http://www.codeguru.com/system/KG101802.html>
The .NET Framework makes things so much simpler for developers who care about
security. There are a lot of useful ways to make sure that only authorized users update the
database, place orders, or otherwise interact with your system. In addition, code access
security ensures that only trusted code can perform certain tasks, no matter who runs the
code.
In this column I focus only on one small topic: how to establish that the user running
your application is authorized to perform some subtask within the application. You can
extend the examples here to any application you write in Managed C++.
Who's There?
Here's a simple console application:
using namespace System;
using namespace System::Security::Principal;
// This is the entry point for this application
int _tmain(void)
{
WindowsIdentity* Identity = WindowsIdentity::GetCurrent();
WindowsPrincipal* Principal = new WindowsPrincipal(Identity);
//Print the values.
Console::WriteLine("Principal Values for current thread:");
Console::WriteLine("Name: {0}",
Principal->Identity->Name);
Console::WriteLine("Type: {0}",
Principal->Identity->AuthenticationType);
Console::WriteLine("IsAuthenticated: {0}",
__box(Principal->Identity->IsAuthenticated));
Console::WriteLine();
Console::WriteLine();
Console::WriteLine("Identity Values for current thread:");
Console::WriteLine("Name: {0}", Identity->Name);
Console::WriteLine("Type: {0}", Identity->AuthenticationType);
Console::WriteLine("IsAuthenticated: {0}",
__box(Identity->IsAuthenticated));
Console::WriteLine("IsAnonymous: {0}",
__box(Identity->IsAnonymous));
Console::WriteLine("IsGuest: {0}", __box(Identity->IsGuest));
Console::WriteLine("IsSystem: {0}", __box(Identity->IsSystem));
Console::WriteLine("Token: {0}", Identity->Token.ToString());
return 0;
}
The first line of this code gets the identity under which the application is running. That
should be you, since the application doesn't contain any code to change the identity. Then
it gets a principal based on that identity. The remainder of the code illustrates some
useful properties of the identity and principal objects. Notice the use of __box to wrap up
the Boolean values, and the ToString method to convert non-string values to strings.
When I run this application, it prints out:
Principal Values for current thread:
Name: GREGORY\kate
Type: NTLM
IsAuthenticated: True
Identity Values for current thread:
Name: GREGORY\kate
Type: NTLM
IsAuthenticated: True
IsAnonymous: False
IsGuest: False
IsSystem: False
Token: 304
You could do a little security code of your own at this point, comparing the name of the
identity or principal with a list of names of authorized users that you have stored
somewhere. But that will require you to write code to manage user lists: adding and
deleting users, changing their authority code, and so on. I prefer to leverage Windows
groups. Even if they aren't in use by the people who'll be running the application, it's less
work to teach people to use Windows groups than to write your own equivalent
administration section.
What Group Are You In?
This code checks to see if a user is in a particular Windows group or not:
String* role = "GREGORY\\Domain Users";
if (Principal->IsInRole(role))
Console::WriteLine("You are a domain user");
else
Console::WriteLine("You are not a domain user");
role = "BUILTIN\\Administrators";
if (Principal->IsInRole(role))
Console::WriteLine("You are an administrator");
else
Console::WriteLine("You are not an administrator");
role = "JSERV\\CodeGuru";
if (Principal->IsInRole(role))
Console::WriteLine("You are a Code Guru");
else
Console::WriteLine("You are not a Code Guru");
When I run this code, it prints out:
You are a domain user
You are an administrator
You are a Code Guru
For you, it almost certainly will not. Notice the three different prefixes in the role strings:
·
GREGORY is my domain. Use this prefix when you're referring to a group that
has been created on your domain controller.
·
BUILTIN refers to built-in groups such as Administrators, created when you
install Windows 2000 or NT.
·
JSERV is my machine name. Use this prefix when you're referring to a local
group.
You can test this code by creating a local group, and adding yourself to it. If you've never
done that before, you can use Computer Management. Right-click My Computer on your
desktop and choose Manage. Expand the Local Users and Groups section, then click on
Groups.
Click here for larger image
Choose Actions, New Group to create a group. Name it CodeGuru, and click Add
to add users to it. Add yourself. Then log off Windows and log back on again to
update your security profile, and run the application again. Now you should be
told you are a Code Guru. (And don't forget about the logging off and logging on.
You'll do a lot of that when you're testing security code.)
Plik z chomika:
zmora1
Inne pliki z tego folderu:
cpp-winapi new styl.rar
(995 KB)
[ebook] - Programming - Teach Yourself Visual C++ In 21 Days.pdf
(5676 KB)
Visual C++ NET Tutorials.pdf
(697 KB)
Visual C++ for Dummies Quick Reference.pdf
(10774 KB)
Visual C++ 6 Special Edition.pdf
(16522 KB)
Inne foldery tego chomika:
asembler
C
delphi
kompilatory
pascal
Zgłoś jeśli
naruszono regulamin