Windows CE 6.0에서는 따로 화면 캡쳐를 지원하지 않습니다. 프로그래밍 문서를 작성하거나, 사용설명서를 만들 때 캡쳐 화면이 필요하여, 다음과 같은 캡쳐 코드를 넣었습니다.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace NameSpace {
    public class Copy {

        enum RasterOperation : uint { SRC_COPY = 0x00CC0020 }

        [DllImport("coredll.dll")]
        static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, RasterOperation rasterOperation);

        [DllImport("coredll.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("coredll.dll")]
        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        public static void CopyThere() {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            IntPtr hdc = GetDC(IntPtr.Zero);
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format16bppRgb565);
            using (Graphics graphics = Graphics.FromImage(bitmap)) {
                IntPtr dstHdc = graphics.GetHdc();
                BitBlt(dstHdc, 0, 0, bounds.Width, bounds.Height, hdc, 0, 0,
                RasterOperation.SRC_COPY);
                graphics.ReleaseHdc(dstHdc);
            }
            string time = DateTime.Now.ToString("yyyyMMddHHmmss");
            bitmap.Save(time + "screenshot.jpg", ImageFormat.Jpeg);
            ReleaseDC(IntPtr.Zero, hdc);
        }

        internal static void SetCopy(Form form) {
            form.KeyPreview = true;
            form.KeyDown += new System.Windows.Forms.KeyEventHandler(Copy.Form_KeyDown);
        }

        internal static void Form_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.F23) { //여기에 캡쳐를 하기 위한 물리 키 값를 넣으면 됩니다.
                Copy.CopyThere();
            }
        }
    }
}

위의 클래스를 구현한 후, 캡쳐 기능이 필요한 Windows Form에서 CopyThere(this) 메소드를 넣고, 실제 실행 중 지정한 키를 누르면 가장 상위 디렉토리에 스크린샷 사진이 저장됩니다.

.Net Compact Framework 에는 없는 것이 많습니다. ( 이하 .Net CF )

개중 컬럼 명을 클릭했을 때, ListViewItem 정렬도 구현되어 있지 않습니다.

 

검색결과 MS에서 직접 Sort 구현하는 법을 올려놓았더라구요.

그러나 VB code여서, C# 코드로 변환을 해봤습니다.

 

대략적인 흐름은 다음과 같습니다.

> 먼저 ColumnHeader를 상속받아 CostumHeader를 만들어 오름차순, 내림차순 변수를 추가한다.

> SortWrapper를 구현하여, ListViewItem을 감싸는 용도로 쓰고, 정렬이 가능하게 IComparer를 달아준다.

> 이벤트를 받아 컬럼이 선택되면, ArrayList에 담아 Sort를 시키고, 다시 꺼내어 ListView에 담는다.

 

첫 번째로 CostumHeader 입니다.

public class ColHeader : ColumnHeader { 
    public Boolean ascendig { get; set; } 

    public ColHeader(String text, int width, HorizontalAlignment align, Boolean asc) { 
        this.Text = text; 
        this.Width = Width; 
        this.TextAlign = align; 
        this.ascendig = asc; 
    } 
}

이를 ColumnHeader 대신에 대상 ListView에 넣어주시면 됩니다.

 

두 번째로 SortWrapper입니다. 클래스로 네임스페이스 안에 구현해주시면 됩니다.

( - 정렬을 위한 비교가 가능하게 IComparer가 삽입되어 있습니다. )

public class SortWrapper { 
    public ListViewItem sortItem { get; set; } 
    public int sortColumn { get; set; } 

    public SortWrapper(ListViewItem Item, int iColumn) { 
        sortItem = Item; 
        sortColumn = iColumn; 
    } 

    public class Sortcomparer : IComparer { 
        private Boolean ascending; 

        public Sortcomparer(Boolean asc){ 
            ascending = asc; 
        } 
         
        int IComparer.Compare(object x, object y) { 
            SortWrapper xItem = (SortWrapper) x; 
            SortWrapper yItem = (SortWrapper) y; 

            String xText = xItem.sortItem.SubItems[xItem.sortColumn].Text; 
            String yText = yItem.sortItem.SubItems[yItem.sortColumn].Text; 

            int direction = ascending ? 1 : -1; 
            return xText.CompareTo(yText) * direction; 
        } 
    } 
}

 

마지막으로, ColumClick Event 내부입니다.

저는 Sort라는 함수로 구현하였는데요.

Event 메소드 안에 "Sort(e.Column, lv_Target);" 이런 방식으로 넣어주시면 됩니다.

private void Sort(int SelectColumn, ListView lv_) { 
     
    ColHeader clickCol = (ColHeader)lv_.Columns[SelectColumn]; 
    clickCol.ascendig = !clickCol.ascendig; 

    lv_.BeginUpdate(); 

    ArrayList SortArray = new ArrayList(); 
    foreach (ListViewItem item in lv_.Items) { 
        SortArray.Add(new SortWrapper(item, SelectColumn)); 
    } 

    SortArray.Sort(new SortWrapper.Sortcomparer(clickCol.ascendig)); 
    lv_.Items.Clear(); 

    foreach (SortWrapper Sort in SortArray) { 
        lv_.Items.Add(Sort.sortItem); 
    } 

    lv_.EndUpdate(); 
}

인터페이스를 적용시키는데 애먹었는데, 다들 잘 사용하셨으면 좋겠습니다.

+ Recent posts