跳到主要內容

發表文章

目前顯示的是 4月, 2010的文章

在程式裡嵌入二進位檔(VC++)

Import: 1. 切換到 Resource Tab 2. (VC6)在專案名稱上按下 mouse 右鍵,選擇 "Import..." 2. (VC2005)在專案名稱上按下 mouse 右鍵,選擇 Add,再選擇 Resource,或是直接選擇 rc 按下右鍵,選擇 Add Resource,點下 "Import..." 3. 點選所要嵌入的檔案後,Resource Type 填入 RCDATA,存檔/編譯即完成。 解開: int ExtractFile(int nResourceId, LPCTSTR lpType, LPCTSTR strOutputName) {     DWORD dwWritten = 0;     HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(nResourceId), lpType);     if(NULL == hRsrc)     {         OutputDebugString(_T("FindResource fail !!!"));         return 1;     }     DWORD dwImageSize = SizeofResource(NULL, hRsrc);     if(dwImageSize == 0)     {         OutputDebugString(_T("FSize is 0 !!!"));         return 1;     }     HGLOBAL hBinImage = LoadResource(NU...

C-style String & C++ String & CString

C-style string to C++ string C-style string to CString char str[] = "Hello World"; string str = string(str); char str[] = "Hello World"; CString cs = CString(str); C++ string to C-style string CString to C-style string string str = string("Hello World"); char *ptr = str.c_str(); CString str = CString("Hello World"); char *ptr = str.GetBuffer(str.GetLength()); str.ReleaseBuffer();

在視窗中繪圖

這裡只簡單建立幾個要點,有時間再補完整一點。 Windows 中負責圖形輸出的部份為圖形裝置介面(Graphic Device Interface, GDI),GDI 使用簡單 的機制來確保每個在視窗中執行的程式都根據此規則運作。而這個機制就是 DC(Device Context)。 DC 是一種 Windows 的資料結構,它含有一個描述所有 GDI 需要知道有關顯示平面資的欄位,而該 顯示平面則關聯到實際的輸出裝置,在螢幕上畫出任何東西之前,Windows 程式必須透過 GDI 來取 得 DC 的 handle。 DC 讓 GDI 能夠成為一個「與裝置無關」的工具,只要給予程式一個 DC,GDI 函式就可以在各種不 同的輸出裝置中繪圖。 CWnd::GetDC 它會傳回一個指向 Windows DC 的 CDC 物件指標,由 CWnd::GetDC 產生的 DC 必須在 繪圖完成後由 CWnd::ReleaseDC 來釋放;在 OnPaint 處理函式中,就必須用 CWnd::BeginPaint 及 CWnd::EndPaint 來取 GetDC 及 ReleaseDC,以確保對 WM_PAINT 做出適當的處理。 ex. CDC *pDC = GetDC(); // do something ReleaseDC(pDC); PAINTSTRUCT ps; CDC *pDC = BeginPaint(&ps); // do something EndPaint(&ps); 特殊目的之 DC 類別 類別名稱 內容描述 CPaintDC 可在視窗中的client area繪圖(僅用在 OnPaint 訊息處理函式中) CClientDC 可在視窗中 client area 繪圖(可用在除了 OnPaint 外的任何地方) CWindowDC 可在視窗中的任何地方(包含 non-client area)繪圖 CMetaFileDC 可在 GDI metafile 中繪圖 CPaintDC Class It performs a CWnd::BeginPaint at construction time and CWnd::EndPaint...

Hello, MFC

一個典型的 MFC 應用程式並沒有 WinMain 函式,也不會呼叫 RegisterClass 或是 CreateWindow 這兩個函式。 MFC 是一組由 Microsoft 所提供的 C++ 類別函式庫,它將 Windows API 用物件導向的方式包裝起來。 MFC 以全域函式的形式提供了一組 API,它們的名稱都以 Afx 開頭。 下面是書中 MFC 的範例應用程式碼 /////////////////////////////////////////// // Hello.h class CMyApp : public CWinApp { public:     virtual BOOL InitInstance(); }; class CMainWindow : public CFrameWnd { public:     CMainWindow();     protected:     afx_msg void OnPaint();     DECLARE_MESSAGE_MAP() }; /////////////////////////////////////////// // Hello.cpp #include #include "Hello.h" CMyApp myApp; // CMyApp member functions BOOL CMyApp::InitInstance() {     m_pMainWnd = new CMainWindow;     m_pMainWnd->ShowWindow(m_nCmdShow);     m_pMainWnd->UpdateWindow();     return TRUE; } // CMainWindow message map and member functions BEGIN_MESSAGE_MAP(CMainWin...

windows programming for GUI

在 windows 裡,所有控制項幾乎每一個都是從 WNDCLASS Structure設定好結構,再用 CreateWindow 建立起相對的 window / control,所以從另一個角度來看,每一個控制項(control),都可以看成是 wnidow,以下這個網站提供了一個不使用 MFC,單純用 win32 API 來做教學。 http://zetcode.com/tutorials/winapi/

UAC VC2005

The assembly identity The name The type The description The attributes in the requestedExecutionLevel Application Manifest Schema: ========================================================== Windows Vista only <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="IsUserAdmin" type="win32"/> <description>Description of your application</description> <!-- Identify the application security requirements. --> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges...