Ch 11 ¹ï¸Ü²°(4)³q¥Î¹ï¸Ü²°


³q¥Î¹ï¸Ü²°Â²¤¶

¦b Windows ¨t²ÎùØ¡A¦³¤@¨Ç¨t²Î¤w¸g³]­p¦nªº¹ï¸Ü²°¥i¨Ñµ{¦¡³]­p®vª½±µ¨Ï¥Î¡A³o¨Ç¹ï¸Ü²°ºÙ¬°¡y³q¥Î¹ï¸Ü²°¡z( common dialog )¡A¦³¤U­±´XºØ¡G

³q¥Î¹ï¸Ü²°API ¦WºÙ ¨Ï¥Îµ²ºcÅé
³]©wÃC¦âChooseColor CHOOSECOLOR
·j´MFindText FINDREPLACE
¨ú¥NReplaceText FINDREPLACE
³]©w¦r«¬ChooseFont CHOOSEFONT
¶}±ÒÂÂÀÉGetOpenFileName OPENFILENAME
¥t¦s·sÀÉGetSaveFileName OPENFILENAME
ª©­±³]©wPageSetupDlg PAGESETUPDLG

¦b¨Ï¥Î¥L­Ì®É¡A¥u»Ý­n¦b¹ïÀ³ªºµ²ºcÅ餤¶ñ¤J¾A·íªº¸ê®Æ¡AµM«á¦A©I¥s API ´N¥i¥H¤F¡A¤£¥Î¦b¸ê·½Àɤ¤³]©w¦UºØ¤l±±¥ó¡A¬O¤@ºØ«Ü¤è«Kªº°µªk¡C

³q¥Î¹ï¸Ü²°¨ç¦¡­ì«¬¬O¦s¦b comdlg32.inc Àɮפº¡A¦]¦¹¦b¼¶¼gµ{¦¡®É¥²¶·¦b­ì©l½X¤¤¥[¤J

include     comdlg32.inc
includelib  comdlg32.lib

³o¨â¦æ¡A³o¼Ë»s³y¥Xªº¥i°õ¦æÀɦb°õ¦æ¤§«á¡A·|¦Û°Ê¦a³sµ²¨ì ¡yC:\WINDOWS\SYSTEM\COMDLG32.DLL¡z°ÊºA¨ç¼Æ®w¤¤¡C¤]¦]¬°¦p¦¹¡A·í·L³n§ó·s COMDLG32.DLL ®É¡A©Ò§e²{ªºµe­±´N·|¤£¦P¡C¦P®É¨Ï¥Î¹L Windows 9x ©M Windows XP ªº¤H¥i¯àª¾¹D¡A¦P¤@­Óµ{¦¡¦b³o¨â­Ó¨t²Î¤¤¡A¦P¼Ë¬O°õ¦æ¡y¶}±ÒÂÂÀÉ¡z¡A¨äµe­±¬O¤£¦Pªº¡C©³¤U¹Ï¤@¬O¦b Windows 98 ²Ä¤Gª©¤¤°õ¦æ¡y¶}±ÒÂÂÀÉ¡zªºµe­±¡G


¹Ï¤@

©³¤U¹Ï¤G¬O¦P¤@­Óµ{¦¡¡A¦b Windows XP °õ¦æ¡y¶}±ÒÂÂÀÉ¡zªºµe­±¡G

¹Ï¤G


­ì²z

³o¤@³¹ùØ¡A¤p¤ì°¸·QÂǥѼ¶¼g¾\ŪÀɮתº¤è¦¡¨Ó»¡©ú¦p¦ó¨Ï¥Î GetOpenFileName¡C³o­Óµ{¦¡°õ¦æ®Éªºµe­±¦p¤U¹Ï¡G


¹Ï¤T

·í¨Ï¥ÎªÌ«ö¤U¡yÂ÷¶}¡z¿ï³æ®É¡A·|Ãö³¬µøµ¡¨Ãµ²§ôµ{¦¡¡C·í¨Ï¥ÎªÌ«ö¤U¡y¶}±Ò¡z¿ï³æ®É¡A·|Åã¥Ü¡y¶}±ÒÂÂÀÉ¡z¡A¦p¤W­±¹Ï¤@©Î¹Ï¤Gªº¹ï¸Ü²°¡A³o®É¨Ï¥ÎªÌ¥i¥H¿ï¾Ü¤@­ÓÀɮסAµ{¦¡±N¦b¤u§@°ÏùØÅã¥Ü¨ä¤º®e¡C¦pªG¨Ï¥ÎªÌ«ö¤U¡y²M°£¡z¿ï³æ¡Aµ{¦¡·|²M°£¤u§@°Ïªº¤º®e¡C

¨Ï¥ÎªÌ­n¶}±ÒÀɮ׮ɡA¥²©w±o¥ý¨ú±o­n¶}±ÒªºÀɦW¡A¤@­Ó²³æªº¤èªk´N¬O§Q¥Î GetOpenFileName API Åã¥Ü¡y¶}±ÒÂÂÀÉ¡z³q¥Î¹ï¸Ü²°¡AÅý¨Ï¥ÎªÌ¿ï¨úÀɮסC

GetOpenFileName ªº¥Îªk

GetOpenFileName API ±N·|Åã¥Ü¤@­Ó¨t²Î¹w¥ý©w¸q¦nªº³q¥Î¹ï¸Ü²° ( ¦p¤W­±¹Ï¤@¡B¹Ï¤G )¡A¨Ñ¨Ï¥ÎªÌ¦b²M³æ±±¥ó¿ï©wÀɦW©Î¬O¦b½s¿è®Ø¤¤¿é¤JÀɦW¡C·í¨Ï¥ÎªÌ¿ï¦nÀɦW©Î¥Ñ½s¿è®Ø¿é¤JÀɦW¨Ã«ö¤U¡y¶}±ÒÂÂÀÉ¡z¤UÀ£¦¡«ö¶s±±¥ó«á¡AGetOpenFileName ¶Ç¦^ TRUE¡A¨Ã¦b OPENFILENAME µ²ºcÅ骺 lpstrFile Äæ¦ì©Ò«ü©w¦ì§}¤º¡A¶ñ¤J³Q¿ï©wªºÀɦW¡F¦pªG¨Ï¥ÎªÌ«ö¤U¡y¨ú®ø¡z¤UÀ£¦¡«ö¶s¡A«h¶Ç¦^ FALSE¡C¦Ü©ó±µ¤U¨Óªº¤u§@¡A¦p¶}±ÒÀɮסBŪ¨úÀɮסB¼g¤JÀɮסBÃö³¬ÀɮסK¡Kµ¥¤u§@¡A¥²¶·¥Ñ¨ä¥Lµ{¦¡­t³d¡AGetOpenFileName ¤£°µ¶}±Ò¡BŪ¨ú¡B¼g¤J¡KÀÉ®×µ¥¤u§@¡CGetOpenFileName ¤§­ì«¬¬°¡G

BOOL GetOpenFileName(
   LPOPENFILENAME   lpofn   // address of structure with initialization data
   );

lpofn ¬O¤@­Ó«ü¼Ð¡A«ü¦V¤@­ÓºÙ¬° OPENFILENAME ªºµ²ºcÅé¡A³o­Óµ²ºcÅ骺Äæ¦ì¦p¤U¡G

OPENFILENAME      struc
lStructSize       DWORD          ?    ;µ²ºcÅé¤j¤p
hwndOwner         HWND           ?    ;¤÷µøµ¡¥N½X
hInstance         HINSTANCE      ?    ;°õ¦æ¹ê¨Ò¥N½X
lpstrFilter       LPCTSTR        ?    ;«ü¦VÀɦW¿z¿ï¦r¦ê¦ì§}
lpstrCustomFilter LPTSTR         ?    ;«ü¦V¦Û­qÀɦW¿z¿ï¦r¦ê¦ì§}
nMaxCustFilter    DWORD          ?    ;¦Û­qÀɦW¿z¿ï¦r¦ê¤j¤p
nFilterIndex      DWORD          ?    ;Åã¥Ü©óÀÉ®×Ãþ«¬ªºÀɦW¿z¿ï¦r¦ê
lpstrFile         LPTSTR         ?    ;§¹¾ãÀɦW¦ì§}
nMaxFile          DWORD          ?    ;§¹¾ãÀɦW¤j¤p
lpstrFileTitle    LPTSTR         ?    ;¥DÀɦW¦ì§}
nMaxFileTitle     DWORD          ?    ;¥DÀɦW¤j¤p
lpstrInitialDir   LPCTSTR        ?    ;°_©l¤l¥Ø¿ý
lpstrTitle        LPCTSTR        ?    ;¹ï¸Ü²°¼ÐÃD
Flags             DWORD          ?    ;«Ø¥ß¹ï¸Ü²°®Éªº¦U¶µ³]©w
nFileOffset       WORD           ?    ;¥DÀɦW¦b§¹¾ãÀɦWªº²Ä´X­Ó¦ì§}
nFileExtension    WORD           ?    ;°ÆÀɦW¦b§¹¾ãÀɦWªº²Ä´X­Ó¦ì§}
lpstrDefExt       LPCTSTR        ?    ;¹w³]ªº°ÆÀɦW
lCustData         DWORD          ?    ;¶Çµ¹ÄdºI¨ç¦¡ªº¸ê®Æ
lpfnHook          LPOFNHOOKPROC  ?    ;ÄdºI¨ç¦¡¦ì§}
lpTemplateName    LPCTSTR        ?    ;¦Û­q¹ï¸Ü²°¼Òª©¦W
OPENFILENAME      ends

OPENFILENAME ªºÄæ¦ì»¡©ú¦p¤U¡G

¶}±ÒÀÉ®×»P«Ø¥ßÀɮסGCreateFile API

¦b Win32 ¨t²Î¤¤¡A¶}±ÒÀɮשM«Ø¥ßÀɮשҥΪº API ³£¬O CreateFile¡A³o¤@ÂI©M¥H«e DOS ¥Î¤£¦Pªº¤¤Â_ªA°Èµ{¦¡ ( AH=3CH ¬O«Ø¥ß·sÀɮסAAH=3DH ¬O¶}±ÒÀÉ®× )¡A¬O¤£¤@¼Ëªº¡C°£¦¹¤§¥~¡ACreateFile ¤£¶È¶È¯à«Ø¥ß©Î¶}±ÒÀɮסA¥¦ÁÙ¥i¥H¶}±Ò¹êÅéºÏºÐ»PÅÞ¿èºÏºÐ ( Physical Disks and Volumes¡A°Ñ¨£ªþ¿ý¤»)¡B³q«H³]³Æ ( communications resources )¡B±±¨î¥x ( consoles )¡K¡Kµ¥µ¥¸Ë¸m¡A¦ý¦¹³B¶È¶È½Í½×Àɮתº¶}±Ò¡CCreateFile ªº­ì«¬¬O¡G

HANDLE CreateFile(
 LPCTSTR                lpFileName,
 DWORD                  dwDesiredAccess,
 DWORD                  dwShareMode,
 LPSECURITY_ATTRIBUTES  lpSecurityAttributes,
 DWORD                  dwCreationDisposition,  // how to create 
 DWORD                  dwFlagsAndAttributes,   // file attributes 
 HANDLE                 hTemplateFile
);

°Ñ¼Æ lpFileName ¬O«ü¦V¥H NULL ¬°µ²§ÀªºÀɦW¦r¦ê¦ì§}¡A¦¹¦r¦ê³Ì¤j¼Æ­È¬O 260 ¦ì¤¸²Õ¡AÀɦW¦r¦êªº®æ¦¡¤@¯ë¬O¡y'ºÏºÐ¾÷¦W:\¸ô®|¦W\¥DÀɦW.°ÆÀɦW',0¡z¡C

dwDesiredAsess ¬O«ü­n¹ïÀɮ׶i¦æ¤°»ò¾Þ§@¡CGENERIC_READ ¬O«ü¹ïÀɮ׶i¦æŪ¨ú¡AGENERIC_WRITE ¬O«ü¹ïÀɮ׶i¦æÂмg¡A°²¦p­n¹ïÀɮצP®É¶i¦æŪ¼g¡A¥²¶·¨Ï¥Î GENERIC_READ or GENERIC_WRITE¡C

dwShareMode ¬O¦@¨ÉÄÝ©Ê¡A«üÀɮ׳Q¦¹µ{¦¡¶}±Ò«á¡A«á¨Óªºµ{¦¡¬O§_¯à¹ï¦¹Àɮ׶i¦æ¾Þ§@¡C¦¹°Ñ¼Æ­Y¬O NULL ªí¥Ü§Oªºµ{¦¡¤£¯à¶}±Ò¡F­Y¬O FILE_SHARE_DELETE¡Aªí¥Ü§Oªºµ{¦¡¯à§R°£¡F­Y¬O FILE_SHARE_READ¡Aªí¥Ü§Oªºµ{¦¡¤]¯àŪ¨ú¡F­Y¬O FILE_SHARE_WRITE¡Aªí¥Ü§Oªºµ{¦¡¤]¯à¼g¤J¡C

lpSecurityAttributes ¬O¦w¥þÄÝ©Ê¡Aªí¥Ü¬O§_¯à³Q¤lµ{¦¡©ÒÄ~©Ó¡A­Y¬° NULL ªí¥Ü¤£¯àÄ~©Ó¡F­Y¤£¬° NULL «h«ü¦V¤@­ÓºÙ¬° SECURITY_ATTRIBUTES ªºµ²ºcÅé¡C

dwCreationDistribution °Ñ¼Æ¥Î¨Ó¨M©w lpFileName «ü¦V¦ì§}ªºÀɦW¦r¦ê©Ò¥NªíªºÀɮצs¦b©ÎªÌ¤£¦s¦b®É¡AÀ³¦p¦ó³B²z¡A³o­Ó°Ñ¼Æ³]©w¤£¦P¦Ó¦³¶}±Ò©M«Ø¥ßÀɮפ£¦Pªº®ÄªG¡C¥i¥Îªº¼Æ­È¦³¡G

dwFlagsAndAttributes ¬O¥Î¨Ó«ü©w©Ò«Ø¥ßÀɮפ§ÄÝ©Ê¥H¤Î¹ïÀɮתº¾Þ§@¤è¦¡¡A±`¥ÎªºÄݩʦ³¤U­±´XºØ¡G

±`¥Îªº¾Þ§@¤è¦¡¦³¤U­±¼ÆºØ¡A¥L­Ì¥i¥H©MÀÉ®×ÄÝ©Ê·f°t¨Ï¥Î¡G

³Ì«á¤@­Ó°Ñ¼Æ¡AhTemplateFile¡A¬O¶}±ÒÀÉ®×¼Òª©¡A³o¬Oµ¹ Win NT/XP ¨Ï¥Îªº¡AWin 9x/Me ¤£¤ä´©¡A¤@¯ë«Øij¥Î NULL¡A¥Hºû«ù¬Û®e¡C

·í CreateFile ªð¦^®É¡A¦pªG¥¢±Ñ¡A«h¶Ç¦^ INVALID_HANDLE_VALUE¡F¦pªG¦¨¥\¡A«h¶Ç¦^ÀÉ®×¥N½X¡C( ¦pªG­n¶i¤@¨B±o¨ì¿ù»~­ì¦]¡AÁÙ±o©I¥s GetLastError )

Ū¨úÀɮסGReadFile

ReadFile API ¬O¥Î¨ÓŪ¨úÀɮסA©ÒŪ¨úªºÀɮפº®e¡A¥ÑÀɮ׫ü¦ì¾¹©Ò«ü¤§³B¶}©lŪ¨ú¡A¦ÓŪ¨úªºªø«×«h¦b°Ñ¼Æ nNumberOfBytesToRead ùØ«ü©w ( ³o­ÓÆ[©À©M¥H«e DOS ®É¥NŪ¨úÀɮ׬ۦP¡A½Ð°Ñ¦Ò³oùØ )¡CReadFile ªº­ì«¬¬O¡G

BOOL ReadFile(
  HANDLE        hFile,                // handle of file to read 
  LPVOID        lpBuffer,             // address of buffer that receives data  
  DWORD         nNumberOfBytesToRead, // number of bytes to read 
  LPDWORD       lpNumberOfBytesRead,  // address of number of bytes read 
  LPOVERLAPPED  lpOverlapped          // address of structure for data 
);

hFile °Ñ¼Æ«ü©w­nŪ¨ú¨º¤@­ÓÀɮסA³o­Ó°Ñ¼ÆÀ³¶ñ¤J¸ÓÀÉ®×¥N½X¡ClpBuffer «ü¦V¤@­Ó½w½Ä°Ï¦ì§}¡A³o­Ó½w½Ä°Ï¥Î¨Ó¦s©ñ©ÒŪ¨ú¨ìªº¤º®e¡CnNumberOfBytesToRead ¬O­nŪ¨úªºªø«×¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡ClpNumberOfBytesRead «ü¦V¤@­Ó 32 ¦ì¤¸ªºÅܼƦì§}¡A¦b ReadFile °õ¦æ§¹«á¡A³o­ÓÅܼƷ|³Q¶ñ¤J¹ê»ÚŪ¨úªº¦ì¤¸²Õ¼Æ¡C¬°¤°»ò­n¦³ nNumberOfBytesToRead ³o­Ó°Ñ¼Æ©O¡H­ì¦]¦³«Ü¦h¡A¨Ò¦p·í§AªºÀɮ׫ü¦ì¾¹¤w¸g¨ìÀÉ®×¥¼ºÝ¡A¦pªG¦AŪ¨ú¡A´N·|Ū¤£¨ìÀɮסC

lpOverlapped «ü¦V¤@­Ó OVERLAPPED µ²ºcÅé¦ì§}¡A³o­Óµ²ºcÅé¬O¥Î¨Óªí¥Ü¦p¦ó³B²z«D¦P¨BŪ¼g¥Îªº¡A¦ý¬O Win9x ¨Ã¤£¤ä´©«D¦P¨BŪ¼g¡A©Ò¥H¦pªG¦b Win 9x ¤¤¡A¥²¶·³]¬° NULL¡C

ReadFile ªð¦^®É¡A¦pªG¶Ç¦^«D¹s­Èªí¥ÜŪ¨ú¥¿½T¡F¦pªG¶Ç¦^¹s¡Aªí¥ÜŪ¨ú¿ù»~¡C¦pªG­n¶i¤@¨B±o¨ì¿ù»~­ì¦]¡AÁÙ±o©I¥s GetLastError¡C

CloseHandle API

ÅU¦W«ä¸q¡A³o­Ó API ¬O¥Î¨ÓÃö³¬Àɮתº¡A­ì«¬¬°¡G

BOOL CloseHandle(
   HANDLE   hObject     // handle to object to close  
);

¥¦¥u¦³¤@­Ó°Ñ¼Æ¡A´N¬O­nÃö³¬ªºÀÉ®×¥N½X¡C¦pªG¦¨¥\¡A¶Ç¦^ TRUE¡F¥¢±Ñ¡A¶Ç¦^ FALSE¡C( ¨ä¹ê¥¦¤£¥u¥iÃö³¬ÀɮסAÁÙ¥iÃö³¬¦æµ{¡B°õ¦æºü¡K¡K)

§âÀɮפº®eÅã¥Ü©ó¤l±±¥óùØ

·íŪ¨úÀɮפº®e¤§«á¡A§â¤º®eÅã¥Ü©ó¿Ã¥ú¹õ¤W¦³³\¦hºØ¤èªk¡A¦¹³B¤p¤ì°¸±Ä¥Î«Ø¥ß¤@­Ó½s¿è®Ø±±¨î¤¸¥ó¡A¦A§âÀɮפº®eÅã¥Ü©ó³o­Ó½s¿è®Ø±±¨î¤¸¥ó¤¤¡C¬°¤Fµe­±¬üÆ[¡A³o­Ó½s¿è®Ø±±¨î¤¸¥ó³Ì¦n­n¯à¶ñº¡¾ã­Óµøµ¡ªº¤u§@°Ï¡A¦Ó¥B·íµøµ¡§ïÅܤj¤p®É¡A½s¿è®Øªº¤j¤p¤]¯àÀH¤§ÅÜ°Ê¥B¦A«×¶ñº¡¾ã­Ó¤u§@°Ï¡C

¬°¹F¦¹¥Øªº¡A¥²¶·¨ú±o¤u§@°Ïªº¤j¤p¡AµM«á§ïÅܤl±±¥ó¤j¤p¥H²Å¦X¤u§@°Ï¡C¨ú±o¤u§@°Ïªº¤j¤p¥i¥Î GetClientRect API¡A¦b²Ä¤T³¹®É¤w¸g´£¹L¡A½Ð¦Û¦æ°Ñ¦Ò¡C

§ïÅܤl±±¥óªº¤j¤p¡A¤]¤£§xÃø¡A­n°O±o¤l±±¥ó¤]¬Oµøµ¡ªº¤@ºØ¡A§óÄYÂÔªº»¡¡A¥L¬O¤@ºØ¤lµøµ¡¡A©Ò¥H§ïÅܤlµøµ¡ªº¤j¤p¤]´N¬O§ïÅܤl±±¥óªº¤j¤p¡C©³¤Uªº MoveWindow API ¥i¹F¦¹¥Øªº¡C

MoveWindow API

MoveWindow API ¥i¥H§ïÅܵøµ¡ªº¤j¤p¡A¥¦·íµM¤]¥i§ïÅܱ±¨î¤¸¥óªº¤j¤p¡A°£¦¹¤§¥~¡A¥¦¤]¥i¥H²¾°Êµøµ¡¡A¨Ïµøµ¡¦ì¸m§ïÅÜ¡CMoveWindow ­ì«¬¦p¤U¡G

BOOL MoveWindow(
    HWND    hWnd,     // handle of window
    int     X,        // horizontal position
    int     Y,        // vertical position
    int     nWidth,   // width
    int     nHeight,  // height
    BOOL    bRepaint  // repaint flag
   );

hWnd ¬O«ü­n§ó°Ê¦ì¸m©Îµøµ¡¤j¤pªºµøµ¡¥N½X¡CX¡BY ¬O«ü­n§âµøµ¡²¾°Ê¨ì¨º¨à¡AnWidth¡BnHeight ¬O«ü§ó§ï«áªºµøµ¡¼e«×»P°ª«×¡CbRepaint ¬O«ü¬O§_­«Ã¸µøµ¡¡ATRUE ªí¥Ü­«Ã¸¡AFALSE ªí¥Ü¤£­«Ã¸¡C¦pªG­«Ã¸µøµ¡¡AMoveWindow ·|µo¥X WM_PAINT °T®§µ¹¨t²Î¡A¨t²Î·|§â¦¹°T®§¶Çµ¹µøµ¡¨ç¦¡¡C¤£¹L WM_PAINT °T®§¤£¥Îµ{¦¡³B²z¡A¨t²Î¤ºªº¹ï¸Ü®ØºÞ²z¾¹·|­t³d­«Ã¸¡C

WM_SIZE °T®§»P WM_SIZING °T®§

·í¨Ï¥ÎªÌ¥Î·Æ¹«¥kÁä©ì¦²µøµ¡Ãä®Ø¡A¦A©ñ¶}·Æ¹««á¡A´N¯à§ïÅܵøµ¡¤j¤p¡A³o®Éµøµ¡·|¦¬¨ì WM_SIZE °T®§¡A¦Ó¦b©ì¦²ªº¹Lµ{¤¤·|¦¬¨ì WM_SIZING °T®§¡C

¥ý½Í WM_SIZING °T®§¡CWM_SIZING °T®§¤¤¡AwParam ªí¥Üµøµ¡ªº¨º¤@­ÓÃä®Ø³Q©ì¦²¡A¥i¥H¬O¤U­±ªº¦ì¤¸­È¤§²Õ¦X¡G

¼Æ­È »¡¡@¡@¡@©ú ¼Æ­È»¡¡@¡@¡@©ú
WMSZ_BOTTOM©³Ãä®Ø WMSZ_TOP¤WÃä®Ø
WMSZ_LEFT¥ªÃä®Ø WMSZ_RIGHT¥kÃä®Ø
WMSZ_TOPLEFT¥ª¤W¨¤¸¨ WMSZ_TOPRIGHT¥k¤W¨¤¸¨
WMSZ_BOTTOMLEFT¥ª¤U¨¤¸¨ WMSZ_BOTTOMRIGHT¥k¤U¨¤¸¨

lParam «ü¦V¤@­Ó¦W¬° RECT µ²ºcÅ餧¦ì§}¡A¦¹µ²ºcÅé¦s¦³µøµ¡¤§¤j¤p¡C½Ð°Ñ¦Ò²Ä¤T³¹¦³Ãö RECT ¤§»¡©ú¡C¦pªGµ{¦¡­n³B²z WM_SIZING °T®§¡A¦bªð¦^®É¡A¨ä¶Ç¦^­È¥²¶·³]¬° TRUE¡C

·íµøµ¡¦¬¨ì WM_SIZE °T®§®É¡AwParam ªí¥Ü¦¡µ¡¬O¦p¦ó³Q§ïÅܪº¡A±`¨£±¡§Î¬O¡G

lParam ¸û§Cªº 16 ¦ì¤¸¬Oµøµ¡¤u§@°Ïªº¼e«×¡A¸û°ªªº 16 ¦ì¤¸¬O¤u§@°Ïªº°ª«×¡C©Ò¥Hµ{¦¡¥iÂÇ¥Ñ WM_SIZE °T®§¨ú±o§ïÅܵøµ¡¤j¤p«áªº¸ê°T¡C

¦b¹ï¸Ü²°¼ÐÃDÄæÅã¥Ü¹Ï¥Ü¡GWM_SETICON

¥H«e§Ú­Ì­n¦bµøµ¡ªº¼ÐÃDÄæÅã¥Ü¹Ï¥Ü¡A¤@¯ë¬O¦b WNDCLASSEX µ²ºcÅ餤«ü©w¹Ï¥Ü¡A¦AÂÇ¥Ñ RegisterClassEx API ¡yÅã¥Ü¡z¹Ï¥Ü¡F¦ý¬O¹ï¸Ü²°¤¤¡Aµøµ¡Ãþ§O¤w¸g¥Ñ¨t²Î¹w³]¦n¡A¦p¦ó§ó§ï¹Ï¥Ü©O¡Hµª®×¬O§â WM_SETICON °T®§¶Ç»¼µ¹¹ï¸Ü²°¡C

WM_SETICON °T®§ªº HWND ¬O·Q­n§ïÅܹϥܪºµøµ¡¡A¨ä lParam ¬O§ïÅܫ᪺¹Ï¥Ü¥N½X¡A³o­Ó¹Ï¥Ü¥N½X¥iÂÇ¥Ñ LoadIcon API ¦Û¸ê·½¤¤¸ü¤J¡C¦Ó wParam «h¬Oªí¥Ü¤j¹Ï¥Ü©Î¤p¹Ï¥ÜªººX¼Ð¡A¥i¥H¥Î ICON_BIG ©Î ICON_SMALL¡C

¦n¤F¡A©³¤U¬Ý¬Ý­ì©lµ{¦¡§a¡I


­ì©lµ{¦¡

ViewFile.ASM ­ì©lµ{¦¡¦p¤U¡G

        .386
        .model  flat,stdcall
        option  casemap:none

include         windows.inc
include         user32.inc
include         kernel32.inc
include         comdlg32.inc
includelib      user32.lib
includelib      kernel32.lib
includelib      comdlg32.lib

IDM_Exit        equ     100
IDM_Open        equ     200
IDM_Clear       equ     300
IDC_Edit        equ     400
MAX_FullFN      equ     260     ;017 §tºÏºÐ¾÷¡B¸ô®|¡B¥D¡B°ÆÀɦWªø«×
MAX_OnlyFN      equ     100     ;018 ¶È§t¥DÀɦW¡B°ÆÀɦW¤§ªø«×
MAX_Read        equ     4096    ;019 Ū¨úÀɮפ§¦ì¤¸²Õ¼Æ

DlgProc         proto   :DWORD,:DWORD,:DWORD,:DWORD
O_and_R         proto   :DWORD

;**********************************************************
                .DATA
hInstance       HINSTANCE       ?
hFile           HFILE           ?
hwndEdit        HWND            ?
ofn             OPENFILENAME    <?>     ;029 OPENFILENAME µ²ºcÅé
nReadBytes      dd   0                  ;030 Ū¨úÀɮתº¹ê»Ú¦ì¤¸²Õ¼Æ
szIcon          db   'eye',0            ;031 ¹Ï¥Ü¦WºÙ
szNoFile        db   '±z¨S¦³¿ï©wÀɦW¡C',0
szOpenErr       db   'Ū¨úÀɮ׿ù»~¡C',0
szClearText     db   ' ',0
DlgName         db   'VFDlg',0
szFNFilter      db   'All Files (*.*)',0,'*.*',0
                db   'Text Files (*.txt)',0,'*.txt',0
                db   'HTML Files (*.html, *.htm)',0,'*.html;*.htm',0,0
szTitle         db   '¾\ŪÀÉ®×',0       ;039 ¤÷µøµ¡¼ÐÃD
szFullFN        db   MAX_FullFN dup (0) ;040 §¹¾ãÀɦW¦s©ñ³B
szOnlyFN        db   MAX_OnlyFN dup (0) ;041 ¥DÀɦW¦s©ñ³B
szDefExt        db   'txt',0            ;042 ¤º©w°ÆÀɦW
buffer          db   MAX_Read dup (0),0 ;043 Àɮפº®e¦s©ñ³B
;**********************************************************
                .CODE
;----------------------------------------------------------
;¶}±Ò»PŪ¨úÀÉ®×
O_and_R proc    hwnd:HWND
        mov     eax,hwnd
        mov     ofn.lStructSize,sizeof ofn      ;050 OPENFILENAME ªø«×
        mov     ofn.hwndOwner,eax
        mov     ofn.lpstrFilter,offset szFNFilter
        mov     ofn.lpstrCustomFilter,NULL
        mov     ofn.nFilterIndex,2              ;054 ¹w³]°ÆÀɦW¬° *.TXT
        mov     ofn.lpstrFile,offset szFullFN   ;055 §¹¾ãÀɦW¦s©ñ³B
        mov     ofn.nMaxFile,MAX_FullFN
        mov     ofn.lpstrFileTitle,offset szOnlyFN
        mov     ofn.nMaxFileTitle,MAX_OnlyFN
        mov     ofn.lpstrInitialDir,NULL        ;059 ¥Îµ{¦¡©Ò¦b¤l¥Ø¿ý
        mov     ofn.lpstrTitle,NULL             ;060 ¥Î¹w³]¼ÐÃD
        mov     ofn.Flags,OFN_READONLY          ;061 °ßŪ
        mov     ofn.lpstrDefExt,offset szDefExt
        mov     ofn.lCustData,NULL
        mov     ofn.lpTemplateName,NULL
        invoke  GetOpenFileName,addr ofn
        mov     edx,offset szTitle+8
        mov     byte ptr [edx],0
.if eax                                 ;069 «ö¤U¶}±ÒÂÂÀɪº½T©w±±¥ó
        invoke  CreateFile,offset szFullFN,GENERIC_READ,\
                FILE_SHARE_READ,NULL,OPEN_EXISTING,\
                FILE_ATTRIBUTE_NORMAL,NULL
  .if eax!=INVALID_HANDLE_VALUE
        mov     hFile,eax               ;073 ¥¿½T¦a¶}±ÒÀÉ®×
        invoke  ReadFile,hFile,offset buffer,MAX_Read,\
                addr nReadBytes,NULL
        mov     edx,nReadBytes
        mov     buffer[edx],0
        mov     edx,offset szTitle+8
        mov     byte ptr [edx],'-'
        invoke  CloseHandle,hFile
        mov     ecx,offset buffer
  .else
        mov     ecx,offset szOpenErr    ;083 ¶}±ÒÀɮ׿ù»~
  .endif
.else
        mov     ecx,offset szNoFile     ;086 «ö¤U¶}±ÒÂÂÀɪº¨ú®ø±±¥ó
.endif
        invoke  SetDlgItemText,hwnd,IDC_Edit,ecx        ;088 §ïÅܽs¿è®Ø¤å¦r
        invoke  SetWindowText,hwnd,offset szTitle       ;089 §ïÅܼÐÃDÄæ
        ret
O_and_R endp
;----------------------------------------------------------
DlgProc proc    hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
        LOCAL   nWinSize:RECT
.if uMsg==WM_INITDIALOG
        invoke  LoadIcon,hInstance,offset szIcon        ;096 ¸ü¤J¹Ï¥Ü
        invoke  SendMessage,hWnd,WM_SETICON,ICON_SMALL,eax
        invoke  GetDlgItem,hWnd,IDC_Edit
        mov     hwndEdit,eax
        invoke  GetClientRect,hWnd,addr nWinSize        ;100 ¨ú±o¤u§@°Ï¤j¤p
        mov     ecx,nWinSize.bottom
        mov     eax,nWinSize.right
        sub     ecx,nWinSize.top
        sub     eax,nWinSize.left
        invoke  MoveWindow,hwndEdit,0,0,eax,ecx,TRUE    ;105 §ïÅܽs¿è®Ø¤j¤p
        invoke  SetWindowText,hWnd,offset szTitle
.elseif uMsg==WM_CLOSE
exit:   invoke  EndDialog,hWnd,NULL
.elseif uMsg==WM_COMMAND
        mov     eax,wParam
  .if lParam==0
     .if ax==IDM_Open                                   ;112 ¿ï¾Ü¡y¶}±Ò¡z
        invoke  O_and_R,hWnd
     .elseif ax==IDM_Clear                              ;114 ¿ï¾Ü¡y²M°£¡z
        invoke  SetDlgItemText,hWnd,IDC_Edit,offset szClearText
        mov     edx,offset szTitle+8
        mov     byte ptr [edx],0
        invoke  SetWindowText,hWnd,offset szTitle
     .elseif ax==IDM_Exit                               ;119 ¿ï¾Ü¡yÂ÷¶}¡z
        jmp     exit
     .endif
  .endif
.elseif uMsg==WM_SIZE
        mov     eax,lParam
        mov     ecx,eax
        cwd
        shr     ecx,16
        invoke  MoveWindow,hwndEdit,0,0,eax,ecx,TRUE    ;128 §ïÅܽs¿è®Ø¤j¤p
.else
        mov     eax,FALSE
        ret
.endif
        mov     eax,TRUE
        ret
DlgProc endp
;----------------------------------------------------------
start:  invoke  GetModuleHandle,NULL
        mov     hInstance,eax
        invoke  DialogBoxParam,hInstance,addr DlgName,NULL,\
                addr DlgProc,NULL
        invoke  ExitProcess,eax
;**********************************************************
        end     start

ViewFile.RC ¸ê·½Àɦp¤U¡G

#include "c:\masm32\include\resource.h"
#define IDM_Exit        100
#define IDM_Open        200
#define IDM_Clear       300
#define IDC_Edit        400
#define VF_MENU         500

EYE     ICON    eye.ico

VF_MENU MENU
{
MENUITEM    "Â÷¶}(&E)",IDM_Exit
MENUITEM    "¶}±Ò(&O)",IDM_Open
MENUITEM    "²M°£(&C)",IDM_Clear
}

VFDlg   DIALOG  0,0,320,220
STYLE   DS_SETFONT|WS_VISIBLE|WS_OVERLAPPEDWINDOW
FONT    9,"·s²Ó©úÅé"
MENU    VF_MENU
{
EDITTEXT IDC_Edit,0,0,300,200,ES_READONLY|ES_MULTILINE|WS_HSCROLL|
         WS_VSCROLL|ES_AUTOHSCROLL|ES_AUTOVSCROLL
}

ViewFile.MAK ¤º®e¦p¤U¡G

ALL : ViewFile.EXE

viewfile.exe : ViewFile.ASM ViewFile.RES
        ml /coff ViewFile.ASM /link ViewFile.RES

ViewFile.RES : ViewFile.RC EYE.ICO
        rc ViewFile.RC

¹Ï¥Ü¬°¡G


»¡©ú

¦b¼Ò¦¡¹ï¸Ü²°¥[¤W¿ï³æ

¨Æ¹ê¤W¡A³o­Óµ{¦¡´N¬O¤@­Ó¼Ò¦¡¹ï¸Ü²°¡A ¦]¬°«Ø¥ß¼Ò¦¡¹ï¸Ü²°©Ò¥Îªºµ{¦¡½Xµu¡A¥¦¥u»Ý­n©I¥s DialogBoxParam §Y¥i¡A¤£»Ý­nµù¥Uµøµ¡Ãþ§O¡A¤]¤£»Ý­n«Ø¥ß°T®§°j°é¡A¬O¤@­Ó«Ü²«Kªº°µªk¡A³\¦hµ{¦¡³]­p®v³£«Ü³ßÅw¥Î³oºØ°µªk¡C¦ý¬O¨Ï¥Î DialogBoxParam ´N¤Ö¤Fµù¥Uµøµ¡Ãþ§O¡A´N¤£±o¤£¥Î¥t¥~ªº¤èªk¬°¼Ò¦¡¹ï¸Ü²°¥[¤W¡y¹Ï¥Ü¡z¥H¤Î¡y¿ï³æ¡z¡C¥[¤W¹Ï¥Üªº¤èªk¡A«e­±¤w¸g±Ô­z¤F¡A¦Ü©ó¥[¤W¿ï³æ¤èªkªº¶ø¯¦¡AÂéó¸ê·½Àɤ¤¡A©³¤U´N¥ý¨ÓÀˬd¸ê·½Àɧa¡C

³o­Óµ{¦¡ªº¸ê·½ÀÉùØ¥]§t¤T³¡¥÷¡A¹Ï¥Ü¡B¿ï³æ¥H¤Î¹ï¸Ü²°¡C¨ä¤¤©w¸q¹Ï¥Ü¡B¿ï³æªº¤è¦¡¨SÅÜ¡A¦ý¬O¦b©w¸q¹ï¸Ü²°¼Òª©®É¡A¦h¤F¤@­Ó MENU ÃöÁä¦r ( ¦b ViewFile.RC Àɮפ¤¬õ¦âªº³¡¤À )¡A¦Ó³o­Ó MENU ÃöÁä¦r¤§«á«h¬O­n¥[¤Wªº¿ï³æ¦WºÙ¡A³o­Ó¿ï³æ«h¬O¥H¹³¥H«e¤@¼Ë©w¸q¦b¤@¹ïªº BEGIN/END ©Î {¡B} ¤§¶¡¡A¨Ã¥H

¿ï³æ¦WºÙ  MENU

¬°¨ä¦WºÙ¡C¦ý¦¹³B³o­Ó¿ï³æ¦WºÙµLªk¥H¦r¦êªí¥Ü¡A¥u¯à¥H¼Æ¦rªí¥Ü¡A³oÂI¤]©M¥H«e©w¸q¿ï³æªº¤è¦¡¤£¦P¡C

¦b¸ê·½Àɤ¤ÁÙ¦³¥t¤@³B­È±o¤@´£¡A´N¬O EDITTEXT ±±¥ó¡A³o­Ó½s¿è®Ø¤l±±¥óªºªø»P¼e¬O¤p¤ì°¸¥ô·N¿ï©wªº¡A¦]¬°«Ø¥ß¦¹¤lµøµ¡¤§«á¡A¥ß§Y·|³Q MoveWindow API ­×§ï¦¨©M¤u§@°Ïªº¤j¤p¤@¼Ë¡C


¨ì²Ä¤Q³¹¡A¦^¨ì­º­¶¡A ¨ì²Ä¤Q¤G³¹