码迷,mamicode.com
首页 > Windows程序 > 详细

win32 便签

时间:2015-01-07 00:28:11      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

无聊做了一个桌面便签练手

刚学C++,所以代码风格还没改过来。。。

NotePin.h

 1  /*----------------------------------------
 2    Note Pin on your screen.
 3                 (c) 2014 LJN
 4   ----------------------------------------*/
 5 
 6 #include <windows.h>
 7 #include <stack>
 8 
 9 #define ID_EDIT        1
10 #define IDN_TRAY    2
11 
12 #define IDM_TRAY    3
13 #define IDM_SHOW    4
14 #define IDM_QUIT    5
15 #define IDM_AUTO    6
16 
17 #define WM_TRAY        WM_USER + 1
18 
19 BOOL FileWrite (HWND hwndEdit, PTSTR pstrFileName);
20 BOOL FileRead (HWND hwndEdit, PTSTR pstrFileName);
21 
22 class TextStack
23 {
24 private:
25     std::stack <PTSTR> text;
26 public:
27     BOOL PushText (HWND hwndEdit);
28     BOOL PopText (HWND hwndEdit);
29     ~TextStack ();
30 };

 

Main.cpp

  1  /*----------------------------------------
  2    Note Pin on your screen.
  3                 (c) 2014 LJN
  4   ----------------------------------------*/
  5 
  6 //Improve:    RegisterHotKey to Use global Hot Key
  7 //            Introduce Redo
  8 
  9 #include <windows.h>
 10 #include "notepin.h"
 11 #include "resource.h"
 12 
 13 LRESULT    CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
 14 LRESULT CALLBACK TempProc (HWND, UINT, WPARAM, LPARAM);
 15 
 16 WNDPROC OldProc;
 17 BOOL    fUAC;
 18 
 19 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
 20                     PSTR szCmdLine, int iCmdShow)
 21 {
 22     static TCHAR szAppName[] = TEXT ("Note-Pin");
 23     HWND        hwnd;
 24     MSG            msg;
 25     WNDCLASS    wndclass;
 26     int            iWidth, iHeight;
 27     HACCEL        hAccel;
 28 
 29     wndclass.style            = CS_HREDRAW | CS_VREDRAW;
 30     wndclass.lpfnWndProc    = WndProc;
 31     wndclass.cbClsExtra        = 0;
 32     wndclass.cbWndExtra        = 0;
 33     wndclass.hInstance        = hInstance;
 34     wndclass.hIcon            = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON));
 35     wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
 36     wndclass.hbrBackground    = (HBRUSH) (COLOR_ACTIVEBORDER + 1);
 37     wndclass.lpszMenuName    = NULL;
 38     wndclass.lpszClassName    = szAppName;
 39     RegisterClass (&wndclass);
 40 
 41     iWidth = GetSystemMetrics (SM_CXSCREEN);
 42     iHeight = GetSystemMetrics (SM_CYSCREEN);
 43 
 44     fUAC = *szCmdLine==\0 ? FALSE : TRUE;
 45 
 46     hwnd = CreateWindow (szAppName, TEXT ("Note-Pin"),
 47                             WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME,
 48                             360 * iWidth / 512,    313 * iHeight / 512,
 49                             38 * iWidth / 128,    43 * iHeight / 128,
 50                             NULL, NULL, hInstance, NULL);
 51 
 52     ShowWindow (hwnd, iCmdShow);
 53     UpdateWindow (hwnd);
 54 
 55     hAccel = LoadAccelerators (hInstance,  MAKEINTRESOURCE (IDR_ACCELERATOR));
 56 
 57     while (GetMessage (&msg, NULL, 0, 0))
 58     {
 59         if (!TranslateAccelerator (hwnd, hAccel, &msg))
 60         {
 61             TranslateMessage (&msg);
 62             DispatchMessage (&msg);
 63         }
 64     }
 65     return (int) msg.wParam;
 66 }
 67 
 68 
 69 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 70 {
 71     static int    cxChar, cxCaps, cyChar;
 72     TEXTMETRIC    tm;
 73     HDC            hdc;
 74     PAINTSTRUCT    ps;
 75 
 76     static int                cxClient, cyClient;
 77     static HINSTANCE        hInstance;
 78     static HWND                hwndEdit;
 79     static POINT            pt1, pt2;
 80     static BOOL                fIsRestore = TRUE, fHide = FALSE , fAuto = FALSE;
 81     static RECT                rect;
 82     static HFONT            hFont;
 83     static HMENU            hMenu;
 84     static HKEY                hKey;
 85     static NOTIFYICONDATA    Notify;
 86     static SYSTEMTIME        st;
 87     
 88     static TCHAR            szFullpath[MAX_PATH];
 89     static SHELLEXECUTEINFO    sei = {0};
 90 
 91     PTSTR    pTmp;
 92     DWORD    cbSize, dType;
 93     int        i;
 94 
 95     WIN32_FILE_ATTRIBUTE_DATA    wfad;
 96     FILETIME                    ftLocal;
 97     TCHAR                        szBuffer[1000];
 98 
 99     static TextStack text;
100 
101     switch (message)
102     {
103     case WM_CREATE:
104 
105         hdc = GetDC (hwnd);
106 
107         GetTextMetrics (hdc, &tm);
108         cxChar = tm.tmAveCharWidth;
109         cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
110         cyChar = tm.tmHeight + tm.tmExternalLeading;
111 
112         ReleaseDC (hwnd, hdc);
113 
114         hInstance = ((LPCREATESTRUCT) lParam) -> hInstance;
115         hwndEdit = CreateWindow (TEXT ("edit"), NULL,
116                                 WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
117                                 0, 0, 0, 0, hwnd, (HMENU) ID_EDIT, hInstance, NULL);
118 #ifndef _WIN64
119         OldProc = (WNDPROC) SetWindowLong (hwndEdit, GWL_WNDPROC, (LPARAM) TempProc);
120 #else
121         OldProc = (WNDPROC) SetWindowLongPtr (hwndEdit, GWLP_WNDPROC, (LONG_PTR) TempProc);
122 #endif
123 
124         GetModuleFileName (NULL, szFullpath, MAX_PATH);
125         for (i = 0; i < lstrlen(szFullpath); i++)
126             if (szFullpath[i] == .)
127             if ((szFullpath[i+1] == e && szFullpath[i+2] == x && szFullpath[i+3] == e) || 128                 (szFullpath[i+1] == E && szFullpath[i+2] == X && szFullpath[i+3] == E))
129                 break;
130         for (; i > 0; i--)
131             if (szFullpath[i] == \\)
132                 break;
133         lstrcpyn (szBuffer, szFullpath, i + 1);
134         SetCurrentDirectory (szBuffer);
135 
136         fIsRestore &= FileRead (hwndEdit, TEXT ("TempStore.txt"));
137         fIsRestore &= GetFileAttributesEx (TEXT ("TempStore.txt"), GetFileExInfoStandard, &wfad);
138         fIsRestore &= FileTimeToLocalFileTime (&wfad.ftLastWriteTime, &ftLocal);
139         fIsRestore &= FileTimeToSystemTime (&ftLocal, &st);
140 
141         hFont = CreateFont (-15, 0, 0, 0, 400, 0, 0, 0, 0, 
142                             OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, FF_SWISS,
143                             TEXT("Microsoft Sans Serif"));
144         SendMessage (hwndEdit, WM_SETFONT, (WPARAM) hFont, 0);
145         SendMessage (hwndEdit, EM_SETSEL, GetWindowTextLength (hwndEdit) + 1, GetWindowTextLength (hwndEdit) + 1);
146         SendMessage (hwndEdit, EM_LINESCROLL, 0, SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0));
147 
148         hMenu = GetSystemMenu (hwnd, FALSE);
149         DeleteMenu (hMenu, SC_MAXIMIZE,    MF_BYCOMMAND);
150         DeleteMenu (hMenu, SC_MINIMIZE,    MF_BYCOMMAND);
151         DeleteMenu (hMenu, SC_RESTORE,    MF_BYCOMMAND);
152         AppendMenu (hMenu, MF_STRING,    IDM_QUIT,    TEXT ("Quit"));
153 
154         hMenu = CreatePopupMenu ();
155         if (fUAC)    AppendMenu (hMenu, MF_STRING, IDM_AUTO, TEXT ("Auto Run"));
156         else        AppendMenu (hMenu, MF_STRING, IDM_AUTO, TEXT ("Auto Run (UAC)"));
157         AppendMenu (hMenu, MF_STRING, IDM_SHOW, TEXT ("Hide"));
158         AppendMenu (hMenu, MF_SEPARATOR, 0, 0);
159         AppendMenu (hMenu, MF_STRING, IDM_QUIT, TEXT ("Quit"));
160 
161         Notify.cbSize = (DWORD) sizeof (NOTIFYICONDATA);
162         Notify.hWnd = hwnd;
163         Notify.uID = IDN_TRAY;
164         Notify.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
165         Notify.uCallbackMessage = WM_TRAY;
166         Notify.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON2));
167         wcscpy_s (Notify.szTip, sizeof TEXT ("Note Pin"), TEXT ("Note Pin"));
168         Shell_NotifyIcon (NIM_ADD, &Notify);
169 
170         if (fUAC)
171         {
172             RegOpenKey (HKEY_LOCAL_MACHINE, TEXT ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), &hKey);
173             RegQueryValueEx (hKey, TEXT ("Note Pin"), 0, &dType, NULL, &cbSize);
174             pTmp = (PTSTR) malloc (cbSize);
175             RegQueryValueEx (hKey, TEXT ("Note Pin"), 0, &dType, (LPBYTE) pTmp, &cbSize);
176             fAuto = lstrcmpi (szFullpath, pTmp) ? FALSE : TRUE;
177             free (pTmp);
178 
179             if (fAuto)    ModifyMenu (hMenu, IDM_AUTO, MF_BYCOMMAND | MF_CHECKED, IDM_AUTO, TEXT ("Auto Run"));
180             else        ModifyMenu (hMenu, IDM_AUTO, MF_BYCOMMAND | MF_UNCHECKED, IDM_AUTO, TEXT ("Auto Run"));
181         }
182         else
183         {
184             RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),
185                             0, KEY_QUERY_VALUE, &hKey);
186             RegQueryValueEx (hKey, TEXT ("Note Pin"), 0, &dType, NULL, &cbSize);
187             pTmp = (PTSTR) malloc (cbSize);
188             RegQueryValueEx (hKey, TEXT ("Note Pin"), 0, &dType, (LPBYTE) pTmp, &cbSize);
189             fAuto = lstrcmpi (szFullpath, pTmp) ? FALSE : TRUE;
190             free (pTmp);
191 
192             if (fAuto)    ModifyMenu (hMenu, IDM_AUTO, MF_BYCOMMAND | MF_CHECKED, IDM_AUTO, TEXT ("Auto Run (UAC)"));
193             else        ModifyMenu (hMenu, IDM_AUTO, MF_BYCOMMAND | MF_UNCHECKED, IDM_AUTO, TEXT ("Auto Run (UAC)"));
194         }
195         return 0;
196 
197     case WM_PAINT:
198 
199         hdc = BeginPaint (hwnd, &ps);
200         SetBkMode (hdc, TRANSPARENT);
201         SelectObject (hdc, hFont);
202 
203         fIsRestore &= GetFileAttributesEx (TEXT ("TempStore.txt"), GetFileExInfoStandard, &wfad);
204         fIsRestore &= FileTimeToLocalFileTime (&wfad.ftLastWriteTime, &ftLocal);
205         fIsRestore &= FileTimeToSystemTime (&ftLocal, &st);
206 
207         if (fIsRestore)
208         {
209             TextOut (hdc, cxChar, cyClient - cyChar * 17 / 16, szBuffer,
210                     wsprintf (szBuffer, TEXT ("Last Edited: %4d/%#02d/%#02d  %#02d:%#02d:%#02d"),
211                     st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond));
212 
213             TextOut (hdc, cxClient - (lstrlen (TEXT("© 2014 LJN")) + 1) * cxChar, cyClient - cyChar * 17 / 16,
214                     TEXT("© 2014 LJN"), lstrlen (TEXT("© 2014 LJN")));
215 
216             MoveToEx  (hdc, 0, cyClient - cyChar * 35 / 32, NULL);
217             LineTo    (hdc, cxClient, cyClient - cyChar * 35 / 32);
218         }
219         EndPaint (hwnd, &ps);
220         
221         if (fIsRestore)    MoveWindow (hwndEdit, 0, 0, cxClient, cyClient - cyChar * 18 / 16, TRUE);
222         else            MoveWindow (hwndEdit, 0, 0, cxClient, cyClient, TRUE);
223         return 0;
224 
225     case WM_SIZE:
226 
227         cxClient = LOWORD (lParam);
228         cyClient = HIWORD (lParam);
229         return 0;
230 
231     case WM_SETFOCUS:
232 
233         SetFocus (hwndEdit);
234         return 0;
235 
236     case WM_TRAY:
237 
238         switch (lParam)
239         {
240         case WM_LBUTTONUP:
241 
242             if (fHide)
243             {
244                 ShowWindow (hwnd, SW_SHOW);
245                 fHide = FALSE;
246                 SetForegroundWindow (hwnd);
247             }
248             else
249             {
250                 FileWrite (hwndEdit, TEXT ("TempStore.txt"));
251                 ShowWindow (hwnd, SW_HIDE);
252                 fHide = TRUE;
253             }            
254             break;
255 
256         case WM_RBUTTONUP:
257 
258             if (fHide)    ModifyMenu (hMenu, IDM_SHOW, MF_BYCOMMAND | MF_STRING, IDM_SHOW, TEXT ("Restore"));
259             else        ModifyMenu (hMenu, IDM_SHOW, MF_BYCOMMAND | MF_STRING, IDM_SHOW, TEXT ("Hide"));
260 
261             SetForegroundWindow (hwnd);
262             GetCursorPos (&pt1);
263             TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt1.x, pt1.y, 0, hwnd, NULL);
264             break;
265         } 
266         return 0;
267 
268     case WM_COMMAND:
269     case WM_SYSCOMMAND:
270 
271         if (lParam && LOWORD (wParam) == ID_EDIT)
272         {
273             switch (HIWORD (wParam))
274             {                
275             case EN_ERRSPACE:
276             case EN_MAXTEXT:
277 
278                 MessageBox (NULL, TEXT ("Too Many Characters!!!"), NULL, MB_ICONWARNING);
279                 return 0;
280 
281             case EN_UPDATE:
282 
283                 text.PushText ((HWND) lParam);
284                 return 0;
285             }
286             break;
287         }
288 
289         switch (LOWORD (wParam))
290         {
291         case IDM_SELALL:
292 
293             SendMessage (hwndEdit, EM_SETSEL, 0, -1);
294             return 0;
295 
296         case IDM_SAVE:
297 
298             FileWrite (hwndEdit, TEXT ("TempStore.txt"));
299             fIsRestore = TRUE;
300             InvalidateRect (hwnd, NULL, TRUE);
301             return 0;
302 
303         case IDM_MY_UNDO:
304 
305             if (!text.PopText (hwndEdit)) MessageBeep (MB_ICONWARNING);
306             SendMessage (hwndEdit, EM_SETSEL, GetWindowTextLength (hwndEdit) + 1, GetWindowTextLength (hwndEdit) + 1);
307             SendMessage (hwndEdit, EM_LINESCROLL, 0, SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0));
308             return 0;
309 
310         case IDM_SHOW:
311 
312             if (fHide)
313             {
314                 ShowWindow (hwnd, SW_SHOW);
315                 fHide = FALSE;
316                 SetForegroundWindow (hwnd);
317             }
318             else
319             {
320                 FileWrite (hwndEdit, TEXT ("TempStore.txt"));
321                 ShowWindow (hwnd, SW_HIDE);
322                 fHide = TRUE;
323             }
324             return 0;
325 
326         case IDM_AUTO:
327 
328             if (!fUAC)
329             {
330                 sei.cbSize = sizeof (SHELLEXECUTEINFO);
331                 sei.lpVerb = TEXT ("runas");
332                 sei.lpFile = szFullpath;
333                 sei.lpParameters = TEXT ("runas");
334                 sei.nShow = SW_SHOW;
335 
336                 if (ShellExecuteEx (&sei))
337                     SendMessage (hwnd, WM_DESTROY, 0, 0);
338             }
339             else
340             {
341                 if (fAuto)
342                 {
343                     ModifyMenu (hMenu, IDM_AUTO, MF_BYCOMMAND | MF_UNCHECKED, IDM_AUTO, TEXT ("Auto Run"));
344                     RegDeleteKeyValue (HKEY_LOCAL_MACHINE,
345                                 TEXT ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), TEXT ("Note Pin"));
346                     fAuto = FALSE;
347                 }
348                 else
349                 {
350                     ModifyMenu (hMenu, IDM_AUTO, MF_BYCOMMAND | MF_CHECKED, IDM_AUTO, TEXT ("Auto Run"));
351                     RegSetValueEx (hKey, TEXT ("Note Pin"), 0, REG_SZ, (CONST BYTE*) szFullpath, (lstrlen (szFullpath) + 1) * 2);
352                     fAuto = TRUE;
353                 }
354             }
355             return 0;
356 
357         case IDM_QUIT:
358 
359             SendMessage (hwnd, WM_DESTROY, 0, 0);
360             return 0;
361         }
362         break;
363 
364     case WM_CLOSE:
365 
366         if (!fHide)
367         {
368             ShowWindow (hwnd, SW_HIDE);
369             FileWrite (hwndEdit, TEXT ("TempStore.txt"));
370             fHide = TRUE;
371         }
372         return 0;
373 
374     case WM_QUERYENDSESSION:
375     case WM_DESTROY:
376 
377         FileWrite (hwndEdit, TEXT ("TempStore.txt"));
378         DeleteObject (hFont);
379         DestroyMenu (hMenu);
380         Shell_NotifyIcon (NIM_DELETE, &Notify);
381         RegCloseKey (hKey);
382 
383         PostQuitMessage (0);
384         return 0;
385     }
386     return DefWindowProc (hwnd, message, wParam, lParam);
387 }
388 
389 LRESULT CALLBACK TempProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
390 {
391     static BOOL iOnce = TRUE;
392 
393     if (message == WM_PAINT && iOnce)
394     {
395         SendMessage (GetParent (hwnd), WM_COMMAND, MAKELONG (ID_EDIT, EN_UPDATE), (LPARAM) hwnd);
396         iOnce = FALSE;
397     }
398 
399     return CallWindowProc (OldProc, hwnd, message, wParam, lParam);
400 }


Func.cpp

  1  /*----------------------------------------
  2    Note Pin on your screen.
  3                 (c) 2014 LJN
  4   ----------------------------------------*/
  5 
  6 #include <windows.h>
  7 #include <algorithm>
  8 #include "notepin.h"
  9 #include "resource.h"
 10 
 11 BOOL FileWrite (HWND hwndEdit, PTSTR pstrFileName) //By Charles Petzold, 1998
 12 {
 13     DWORD    dwBytesWritten;
 14     HANDLE    hFile;
 15     int        iLength;
 16     PTSTR    pstrBuffer;
 17     WORD    wByteOrderMark = 0xFEFF;
 18 
 19     if (INVALID_HANDLE_VALUE == 
 20         (hFile = CreateFile (pstrFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)))
 21     {
 22         MessageBox (NULL, TEXT ("NO Access to Create the File!!!"), NULL, MB_ICONWARNING);
 23         return FALSE;
 24     }
 25 #ifdef UNICODE
 26     WriteFile (hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL);
 27 #endif
 28     iLength = GetWindowTextLength (hwndEdit);
 29     pstrBuffer = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR));
 30     GetWindowText (hwndEdit, pstrBuffer, iLength + 1);
 31 
 32     if (!pstrBuffer)
 33     {
 34         MessageBox (NULL, TEXT ("Can NOT Create the Buffer!!!"), NULL, MB_ICONWARNING);
 35         CloseHandle (hFile);
 36         return FALSE;
 37     }
 38 
 39     WriteFile (hFile, pstrBuffer, iLength * sizeof (TCHAR), &dwBytesWritten, NULL);
 40 
 41     if ((iLength * sizeof (TCHAR)) != (int) dwBytesWritten)
 42     {
 43         MessageBox (NULL, TEXT ("Too Many Characters!!!"), NULL, MB_ICONWARNING);
 44         CloseHandle (hFile);
 45         free (pstrBuffer);
 46         return FALSE;
 47     }
 48 
 49     free (pstrBuffer);
 50     CloseHandle (hFile);
 51     return TRUE;
 52 }
 53 
 54 BOOL FileRead (HWND hwndEdit, PTSTR pstrFileName) //By Charles Petzold, 1998
 55 {
 56     BYTE    bySwap;
 57     DWORD    dwBytesRead;
 58     HANDLE    hFile;
 59     int        i, iFileLength, iUniTest;
 60     PBYTE    pBuffer, pText, pConv;
 61 
 62         // Open the file.
 63 
 64     if (INVALID_HANDLE_VALUE == 
 65             (hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ,
 66                             NULL, OPEN_EXISTING, 0, NULL)))
 67         return FALSE;
 68 
 69         // Get file size in bytes and allocate memory for read.
 70         // Add an extra two bytes for zero termination.
 71                 
 72     iFileLength = GetFileSize (hFile, NULL); 
 73     pBuffer = (BYTE *) malloc (iFileLength + 2);
 74 
 75         // Read file and put terminating zeros at end.
 76     
 77     ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL);
 78     CloseHandle (hFile);
 79     pBuffer[iFileLength] = \0;
 80     pBuffer[iFileLength + 1] = \0;
 81 
 82         // Test to see if the text is Unicode
 83 
 84     iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
 85     
 86     if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
 87     {
 88         pText = pBuffer + 2;
 89         iFileLength -= 2;
 90 
 91         if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
 92         {
 93             for (i = 0; i < iFileLength / 2; i++)
 94             {
 95                 bySwap = ((BYTE *) pText) [2 * i];
 96                 ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1];
 97                 ((BYTE *) pText) [2 * i + 1] = bySwap;
 98             }
 99         }
100 
101             // Allocate memory for possibly converted string
102 
103         pConv = (BYTE *) malloc (iFileLength + 2);
104 
105             // If the edit control is not Unicode, convert Unicode text to 
106             // non-Unicode (ie, in general, wide character).
107 
108 #ifndef UNICODE
109         WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, pConv, 
110                         iFileLength + 2, NULL, NULL);
111 
112             // If the edit control is Unicode, just copy the string
113 #else
114         lstrcpy ((PTSTR) pConv, (PTSTR) pText);
115 #endif
116 
117     }
118     else    // the file is not Unicode
119     {
120         pText = pBuffer;
121 
122             // Allocate memory for possibly converted string.
123 
124         pConv = (BYTE *) malloc (2 * iFileLength + 2);
125 
126             // If the edit control is Unicode, convert ASCII text.
127 
128 #ifdef UNICODE
129         MultiByteToWideChar (CP_ACP, 0, (PSTR) pText, -1, (PTSTR) pConv, 
130                         iFileLength + 1);
131 
132             // If not, just copy buffer
133 #else
134         lstrcpy ((PTSTR) pConv, (PTSTR) pText);
135 #endif
136     }
137     
138     SetWindowText (hwndEdit, (PTSTR) pConv);
139     free (pBuffer);
140     free (pConv);
141    
142     return TRUE;
143 }
144 
145 BOOL TextStack::PushText (HWND hwndEdit)
146 {
147     int        iLength;
148     PTSTR    pBuffer;
149 
150     iLength = GetWindowTextLength (hwndEdit);
151     pBuffer = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR));
152     GetWindowText (hwndEdit, pBuffer, iLength + 1);
153 
154     text.push (pBuffer);
155 
156     if (text.empty ())
157         return FALSE;
158     else
159         return TRUE;
160 }
161 
162 BOOL TextStack::PopText (HWND hwndEdit)
163 {
164     PTSTR pBuffer;
165 
166     if (text.size () > 1)
167     {
168         pBuffer = text.top ();
169         free (pBuffer);
170         text.pop ();
171         pBuffer = text.top ();
172         SetWindowText (hwndEdit, pBuffer);
173         return TRUE;
174     }
175     else
176         return FALSE;
177 }
178 
179 TextStack::~TextStack ()
180 {
181     while (!text.empty ())
182     {
183         free (text.top ());
184         text.pop ();
185     }
186 }

 

自high一下。。。

win32 便签

标签:

原文地址:http://www.cnblogs.com/BOT-Man/p/4207370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!