Chia sẻ kiến thức IT và đời sống – Sharing IT Knowledge and Life

Showing posts with label DotNet. Show all posts
Showing posts with label DotNet. Show all posts

Tuesday, February 8, 2011

GDI+ Application

This article discusses vital concepts, including the life cycle of a graphics application. After reading this article, you should understand the basics of the GDI+ coordinate system, basic graphics structures used by GDI+, drawing surfaces, and how to write a graphics application using GDI+.

To write a graphics application, a good understanding of drawing surfaces and coordinates system is necessary. We will begin by discussing these concepts and how they are represented in GDI+. Then you'll learn step-by-step how to write a graphics application in the .NET Framework using GDI+. We will cover the following topics:
  • How to add a reference to the GDI+ library
  • How to get a drawing surface in the program
  • How to create pens and brushes
  • How to use pens and brushes to draw graphics objects

Using Transformations in c#

All of the discussion and examples so far have been drawn in C#’s native drawing units: pixels. By default, all of the Graphics object’s filling and drawing methods work in pixels.

However, pixels may not always be the most convenient unit for you. For example, suppose you want to draw a simple bar chart showing sales figures between $1M and $30M for the years 1990 through 2000. Since you can’t use pixel coordinates as large as (1995,1,000,000), you’ll need to perform some mathematical calculations to map these big values into something that will fit on your screen.
While you can perform those calculations yourself if you like, the Graphics object provides transformation methods that can do this for you. The main transformation methods are TranslateTransform, ScaleTransform, and RotateTransform, which translate, scale, and rotate subsequent graphics, respectively.

Saturday, January 22, 2011

The TreeView Control in C# .NET

Build your own C# Custom Web Browser
To end this section on Events, we'll show you how to build your own custom browser in C# .NET. We'll only use three control: a SplitContainer, A TreeView and, of course, a WebBrowser control. It's surprisingly easy!
Start a new project for this. Select your form and make it nice and big. Expand the Toolbox and locate the SplitContainer. A SplitContainer allows you to have one control in one half and another control in the other half. We'll put the TreeView control on the left, and the browser on the right. But the SplitContainer looks like this:

Double click to add a SplitContainer to your form. By default, it will fill the whole form, and anchor itself to the top left corner. To change this, locate the Anchor property of the SplitContainer. Click the down arrow to see the following:

Thursday, November 4, 2010

Components và Controls trong c#

A control is a reusable class that derives from the System.Windows.Forms.Control base implementation (either directly or indirectly) and whose main purpose in life is to interact with users on behalf of a container, which can be either a form or a container control.1 [1] A control's user interaction takes two forms: acceptance of user input via mice and keyboards, and presentation of processing results and state as UI output. 
Components, it is possible to create specialized classes, known as components, which can be hosted on a form's nonvisual design surface.

Wednesday, November 3, 2010

.NET Reflector 7 Beta 1 – Decompile ứng dụng .NET

.NET Reflector là một công cụ khá quen thuộc đối với các lập trình viên .NET, đặc biệt trong việc xem và phân tích các assembly .NET đã được compile.

Với phiên bản NET Reflector 7 Beta 1, chúng ta có thể mở các file .exe hoặc .dll được viết bằng .NET để xem cấu trúc bên trong của assembly, bao gồm namespace, class, method, property, field và các dependency.

Điểm đáng chú ý nhất của Reflector là khả năng decompile assembly về dạng source code dễ đọc, chẳng hạn C#.

Thông thường, khi viết một chương trình C#:

C# Source Code
      ↓
   Compiler
      ↓
 .NET Assembly
 (.exe / .dll)

Sau khi compile, source code ban đầu không còn nằm nguyên dạng trong file .exe hoặc .dll. Tuy nhiên, assembly .NET vẫn chứa IL (Intermediate Language) và metadata của chương trình. Reflector có thể sử dụng những thông tin này để hiển thị lại code ở mức gần với source code ban đầu.

Ví dụ, một class trong assembly có thể được Reflector hiển thị như sau:

public class Customer
{
    private int id;

    public Customer(int id)
    {
        this.id = id;
    }

    public int GetId()
    {
        return id;
    }
}

Đây là một tính năng rất hữu ích khi chúng ta cần kiểm tra một DLL nhưng không có source code đi kèm.

Xem cấu trúc của một Assembly

Sau khi mở file .dll hoặc .exe bằng Reflector, chương trình sẽ hiển thị cấu trúc của assembly theo dạng cây.

Ví dụ:

MyApplication.dll
 |
 +-- MyApplication
 |    |
 |    +-- Customer
 |    |    +-- Customer()
 |    |    +-- GetId()
 |    |
 |    +-- CustomerService
 |         +-- GetCustomer()
 |
 +-- MyApplication.Data
      |
      +-- CustomerRepository

Từ đây có thể xem được các class và method bên trong assembly mà không cần source code gốc.

Đối với các project lớn, chức năng này khá tiện lợi để tìm hiểu một thư viện .NET của bên thứ ba hoặc kiểm tra xem một DLL thực sự đang thực hiện những gì.

Decompile thành C#

Một trong những chức năng được sử dụng nhiều nhất của Reflector là decompile.

Thay vì chỉ xem IL, Reflector có thể biểu diễn IL thành C# hoặc ngôn ngữ ở mức cao hơn để lập trình viên dễ đọc.

Ví dụ một đoạn code:

public decimal Calculate(decimal price, int quantity)
{
    return price * quantity;
}

sau khi compile sẽ không còn được lưu dưới đúng dạng source code này. CLR sẽ làm việc với IL và metadata.

Reflector phân tích assembly và hiển thị lại logic tương ứng:

Assembly
   ↓
Metadata + IL
   ↓
.NET Reflector
   ↓
C# representation

Do đó, Reflector thường được dùng để debug, nghiên cứu thư viện và tìm hiểu các component .NET.

Trường hợp mất source code

Một trường hợp khá thực tế là có một ứng dụng cũ chỉ còn:

Application.exe
SomeLibrary.dll
DataAccess.dll
Business.dll

nhưng source code đã bị mất hoặc không còn đầy đủ.

Nếu các assembly vẫn có thể đọc được, Reflector có thể giúp developer tìm hiểu lại cấu trúc của chương trình.

Chẳng hạn cần tìm xem một method nào đó xử lý dữ liệu như thế nào:

Business.dll
   ↓
CustomerService
   ↓
GetCustomer()
   ↓
CustomerRepository

Developer có thể lần theo các class và method trong Reflector để xác định logic xử lý.

Điều này đặc biệt hữu ích khi maintain các hệ thống .NET cũ hoặc khi phải làm việc với một component mà tài liệu không đầy đủ.

Reflector Add-ins

Một ưu điểm khác của Reflector là khả năng mở rộng thông qua Add-ins.

Các add-in cho phép bổ sung thêm chức năng cho Reflector và phục vụ những nhu cầu phân tích khác nhau.

Thời điểm đó, các add-in cho Reflector được cộng đồng .NET phát triển và chia sẻ, trong đó có dự án Reflector Addins trên CodePlex.

CodePlex khi đó là một trong những nơi khá phổ biến để tìm kiếm các project và extension mã nguồn mở dành cho hệ sinh thái Microsoft.

Một số lưu ý

Cần lưu ý rằng decompile không có nghĩa là lấy lại chính xác 100% source code ban đầu.

Các thông tin như comment, format code, tên một số biến local và cấu trúc source ban đầu có thể đã bị mất trong quá trình compile.

Ví dụ source ban đầu có thể là:

int totalPrice = price * quantity;
return totalPrice;

nhưng sau khi decompile có thể chỉ còn:

return price * quantity;

Logic tương đương nhưng source code không hoàn toàn giống nhau.

Ngoài ra, nếu assembly được sử dụng obfuscator thì việc đọc và decompile cũng sẽ khó khăn hơn.

Kết luận

NET Reflector 7 Beta 1 là một công cụ rất đáng chú ý đối với developer .NET trong việc browse, inspect và decompile các assembly .NET.

Nếu bạn đang làm việc với một DLL không có source code, muốn tìm hiểu một component của bên thứ ba, debug một thư viện hoặc đơn giản là muốn xem bên trong một assembly .NET có những gì, Reflector là một công cụ rất hữu ích.

Download:

NET Reflector:
http://reflector.red-gate.com/download.aspx?TreatAsUpdate=1

Reflector Add-ins:
http://reflectoraddins.codeplex.com/

Note: Việc decompile và sử dụng mã nguồn thu được cần tuân thủ license và quyền sở hữu trí tuệ của phần mềm được phân tích.

PHỔ BIẾN

CHUYÊN MỤC

TỔNG LƯỢT XEM