向hellovfp大哥及各位论坛大牛求助!!
windows编程手册看到“创建一个 Windows 外壳”那一章 有个实例程序 放到VC下编译通不过……懒的下的话我把三个文件贴下面:(代码有点长 也比较占地方 但各位看官完全可以一扫而过 关键是后面的错误 和源程序没直接的关系 是出在了一个依赖文件(可以这么叫吧)上头。而且出现的两个错误指向同一个地方)
程序代码:///////////////////////////////////////
// Program Name: FileBox.cpp
// Programmer: skyline
// Description: FileBox Windows program
///////////////////////////////////////
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include "windowsx.h"
#pragma warning (disable:4068)
#pragma hdrstop
// #include "excpt.h"
#include "direct.h"
#include "string.h"
#include "FileBox2.h"
static char szAppName[]="skyline";
static HWND MainWindow;
static HINSTANCE hInstance;
char * ButtonText[]={"&Drives","&Directories","&Files"};
char * BmpName[]={"DRIVES","DIRS","FILES"};
WORD DirShowVal[]={DDL_DRIVES|DDL_EXCLUSIVE,
DDL_DIRECTORY|DDL_EXCLUSIVE,DDL_ARCHIVE};
HWND hControl[8];
HBITMAP Bmp[3];
HWND PathWin;
FARPROC lpfnNewEditProc;
char FilesWildCard[100];
HBRUSH BtnBrush;
//////////////////////////////////
// Program entry point.
//////////////////////////////////
#pragma argsused
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,
LPSTR lpszCmdParam,int nCmdShow)
{
MSG Msg;
if(! hPrevInstance)
if(! Register(hInst))
return FALSE;
SetMessageQueue(20);
MainWindow=Create(hInst,nCmdShow);
if(! MainWindow)
return FALSE;
while(GetMessage(&Msg,NULL,0,0))
{
if(!IsDialogMessage(MainWindow,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}
////////////////////////////////////
// Register the window
////////////////////////////////////
BOOL Register(HINSTANCE hInst)
{
WNDCLASS WndClass;
WndClass.style = CS_HREDRAW|CS_VREDRAW;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInst;
WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = szAppName;
return (RegisterClass(&WndClass)!=0);
}
////////////////////////////////////
// Create the Window and show it.
////////////////////////////////////
HWND Create(HINSTANCE hInst,int nCmdShow)
{
hInstance=hInst;
HWND hWnd = CreateWindowEx (0,szAppName,szAppName,
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|
WS_MINIMIZEBOX|WS_THICKFRAME,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInst,NULL);
if(hWnd == NULL)
return hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return hWnd;
}
//////////////////////////////////
// The window proc is where message get processed
//////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd,UINT Message,
WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
HANDLE_MSG(hWnd,WM_CREATE,skyline_OnCreate);
HANDLE_MSG(hWnd,WM_DESTROY,skyline_OnDestroy);
HANDLE_MSG(hWnd,WM_CLOSE,skyline_OnClose);
HANDLE_MSG(hWnd,WM_COMMAND,skyline_OnCommand);
HANDLE_MSG(hWnd,WM_GETMINMAXINFO,skyline_OnGetMinMaxInfo);
HANDLE_MSG(hWnd,WM_PAINT,skyline_OnPaint);
HANDLE_MSG(hWnd,WM_PARENTNOTIFY,skyline_OnParentNotify);
HANDLE_MSG(hWnd,WM_RBUTTONDOWN,skyline_OnRButtonDown);
#ifdef WIN32
HANDLE_MSG(hWnd,WM_CTLCOLORBTN,skyline_OnCtlColor);
HANDLE_MSG(hWnd,WM_CTLCOLORSTATIC,skyline_OnCtlColor);
#else
HANDLE_MSG(hWnd,WM_CTLCOLOR,skyline_OnCtlColor);
#endif
default:
return skyline_DefProc(hWnd,Message,wParam,lParam);
}
}
//////////////////////////////////
// Create the Callback procedures
//////////////////////////////////
void MakeCallBackProcs()
{
#ifdef WIN32
OldEditProc=(WNDPROC)SetWindowLong(hControl[ID_EDIT],
GWL_WNDPROC,LONG(NewEditProc));
#else
lpfnNewEditProc=MakeProcInstance((FARPROC)NewEditProc,hInstance);
OldEditProc=(WNDPROC)SetWindowLong(hControl[ID_EDIT],
GWL_WNDPROC,LONG(lpfnNewEditProc));
#endif
}
//////////////////////////////////
// Handle WM_CREATE
//////////////////////////////////
#pragma argsused
BOOL skyline_OnCreate(HWND hWnd,
CREATESTRUCT FAR * lpszCreateStruct)
{
BtnBrush=CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
for(int i=0;i<3;i++)
{
Bmp[i]=LoadBitmap(hInstance,BmpName[i]);
if(!Bmp[i])
{
MessageBox(hWnd,"No Bitmap !",
"Fatal Error",MB_ICONSTOP|MB_OK);
return FALSE;
}
}
hControl[ID_FILELIST]=CreateWindow("listbox",NULL,
WS_CHILD|WS_VISIBLE|LBS_STANDARD|WS_TABSTOP,
15,30,220,180,hWnd,HMENU(ID_FILELIST),hInstance,NULL);
hControl[ID_GROUP]=CreateWindow("button","Mode",
WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
250,2,131,122,hWnd,HMENU(ID_FILELIST),hInstance,NULL);
for(i=0;i<3;i++)
hControl[i]=CreateWindow("button",ButtonText[i],
WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,
270,25+(i*30),95,30,hWnd,HMENU(i+100),hInstance,NULL);
hControl[ID_EDIT]=CreateWindow("edit","*.exe",
WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP|WS_GROUP,
250,130,131,25,hWnd,HMENU(ID_EDIT),hInstance,NULL);
hControl[ID_CLOSE]=CreateWindow("button","Close",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP|WS_GROUP,
250,161,131,45,hWnd,HMENU(ID_CLOSE),hInstance,NULL);
PathWin=CreateWindow("static",NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER,
14,10,222,20,hWnd,HMENU(ID_PATHS),hInstance,NULL);
DlgDirList(hWnd,"*.*",ID_FILELIST,
ID_PATHS,DirShowVal[DirShowType]);
MakeCallBackProcs();
Button_SetCheck(hControl[0],TRUE);
SetFocus(hControl[ID_FILELIST]);
strcpy(FilesWildCard,"*.exe");
SetListBox(hWnd);
return TRUE;
}
//////////////////////////////////
// Handle WM_DESTROY
//////////////////////////////////
#pragma argsused
void skyline_OnDestroy(HWND hWnd)
{
for(int i=0;i<3;i++)
DeleteBitmap(Bmp[i]);
DeleteBrush(BtnBrush);
SetWindowLong(hControl[ID_EDIT],GWL_WNDPROC,LONG(OldEditProc));
#ifndef WIN32
FreeProcInstance(lpfnNewEditProc);
#endif
PostQuitMessage(0);
}
//////////////////////////////////
// Handle WM_CLOSE
//////////////////////////////////
#pragma argsused
void skyline_OnClose(HWND hWnd)
{
if(MessageBox(hWnd,"Do you want to exit ?","FileBox",
MB_ICONQUESTION|MB_OKCANCEL)==IDOK)
{
DestroyWindow(hWnd);
}
}
//////////////////////////////////
// SetListBox
//////////////////////////////////
#pragma argsused
void SetListBox(HWND hWnd)
{
RECT r;
char WildCard[150];
if(DirShowType==FILEMODE)
strcpy(WildCard,FilesWildCard);
else
strcpy(WildCard,"*.*");
ListBox_ResetContent(hControl[ID_FILELIST]);
ListBox_Dir(hControl[ID_FILELIST],
DirShowVal[DirShowType],WildCard);
r.left=14;
r.top=220;
r.right=14+BMPX;
r.bottom=220+BMPY;
InvalidateRect(hWnd,&r,FALSE);
}
//////////////////////////////////
// Handle MouseClick on listbox for DIR or DRIVE change
//////////////////////////////////
#pragma argsused
void HandleMouseClick(HWND hWnd,int id,
HWND hWndCtl,UINT CodeNotify)
{
char buf[MAXSTR];
DlgDirSelectEx(hWnd,buf,MAXSTR,ID_FILELIST);
DlgDirList(hWnd,buf,ID_FILELIST,
ID_PATHS,DirShowVal[DirShowType]);
}
//////////////////////////////////
// Handle WM_COMMAND
//////////////////////////////////
#pragma argsused
void skyline_OnCommand(HWND hWnd,int id,
HWND hWndCtl,UINT CodeNotify)
{
char s[MAXSTR],lpszBuf[MAXSTR];
switch(id)
{
case ID_DRIVES+100:
DirShowType=DRIVEMODE;
SetListBox(hWnd);
break;
case ID_DIRS+100:
DirShowType=DIRMODE;
SetListBox(hWnd);
break;
case ID_FILES+100:
DirShowType=FILEMODE;
SetListBox(hWnd);
break;
case ID_FILELIST:
if(CodeNotify==LBN_DBLCLK)
{
if(DirShowType!=FILEMODE)
HandleMouseClick(hWnd,id,hWndCtl,CodeNotify);
else
{
int index=ListBox_GetCurSel(hControl[ID_FILELIST]);
_getdcwd(0,s,125);
ListBox_GetText(hControl[ID_FILELIST],index,lpszBuf);
strcat(s,"\\");
strcat(s,lpszBuf);
WinExec(s,SW_SHOWNORMAL);
}
}
break;
case ID_CLOSE:
SendMessage(hWnd,WM_CLOSE,0,0);
break;
}
}
//////////////////////////////////
// Handle WM_CTLCOLOR
//////////////////////////////////
#pragma argsused
HBRUSH skyline_OnCtlColor(HWND hWnd,HDC hdc,
HWND hWndChild,int type)
{
switch(type)
{
case CTLCOLOR_STATIC:
case CTLCOLOR_BTN:
SetBkMode(hdc,TRANSPARENT);
return BtnBrush;
}
return NULL;
}
//////////////////////////////////
// Handle WM_GETMAINMAXINFO
//////////////////////////////////
#pragma argsused
void skyline_OnGetMinMaxInfo(HWND hWnd,
MINMAXINFO FAR * lpMinMaxInfo)
{
lpMinMaxInfo->ptMaxSize.x=XSIZE;
lpMinMaxInfo->ptMaxSize.y=YSIZE;
lpMinMaxInfo->ptMaxPosition.x=100;
lpMinMaxInfo->ptMaxPosition.y=100;
lpMinMaxInfo->ptMinTrackSize.x=XSIZE;
lpMinMaxInfo->ptMinTrackSize.y=YSIZE;
lpMinMaxInfo->ptMaxTrackSize.x=XSIZE;
lpMinMaxInfo->ptMaxTrackSize.y=YSIZE;
}
void HandleRightButton(HWND hWnd)
{
switch(DirShowType)
{
case DRIVEMODE:
Button_SetCheck(hControl[1],TRUE);
Button_SetCheck(hControl[0],FALSE);
Button_SetCheck(hControl[2],FALSE);
DirShowType=DIRMODE;
break;
case DIRMODE:
Button_SetCheck(hControl[2],TRUE);
Button_SetCheck(hControl[0],FALSE);
Button_SetCheck(hControl[1],FALSE);
DirShowType=FILEMODE;
break;
case FILEMODE:
Button_SetCheck(hControl[0],TRUE);
Button_SetCheck(hControl[1],FALSE);
Button_SetCheck(hControl[2],FALSE);
DirShowType=DRIVEMODE;
break;
}
SetListBox(hWnd);
}
//////////////////////////////////
// Handle WM_PAINT
//////////////////////////////////
#pragma argsused
void skyline_OnPaint(HWND hWnd)
{
PAINTSTRUCT PaintStruct;
HBITMAP OldBmp;
HDC PaintDC=BeginPaint(hWnd,&PaintStruct);
HDC BltDC=CreateCompatibleDC(PaintDC);
OldBmp=SelectBitmap(BltDC,Bmp[DirShowType]);
BitBlt(PaintDC,14,220,BMPX,BMPY,BltDC,0,0,SRCCOPY);
SelectBitmap(BltDC,OldBmp);
DeleteDC(BltDC);
EndPaint(hWnd,&PaintStruct);
}
//////////////////////////////////
// Handle WM_PARENTNOTIFY
//////////////////////////////////
#pragma argsused
void skyline_OnParentNotify(HWND hWnd,UINT msg,
HWND hWndChild,int idChild)
{
if(msg==WM_RBUTTONDOWN)
HandleRightButton(hWnd);
}
//////////////////////////////////
// Handle WM_RBUTTONDOWN
//////////////////////////////////
#pragma argsused
void skyline_OnRButtonDown(HWND hWnd,BOOL fDoubleClick,
int x,int y,UINT keyFalgs)
{
HandleRightButton(hWnd);
}
// -------------------------------
// The SubClassed WNDPROCS
// -------------------------------
void SetNewWildCard(HWND hWnd)
{
char buf[150];
GetWindowText(hWnd,buf,sizeof(buf));
strcpy(FilesWildCard,buf);
if(DirShowType==FILEMODE)
SetListBox(MainWindow);
}
///////////////////////////////////
// SubClassing for RadioButtons
///////////////////////////////////
LRESULT CALLBACK NewEditProc(HWND hWnd,UINT message,
WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_GETDLGCODE:
return DLGC_WANTALLKEYS;
case WM_CHAR:
if(wParam==VK_TAB)
return 0;
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_RETURN:
SetNewWildCard(hWnd);
SetFocus(hControl[ID_CLOSE]);
break;
case VK_TAB:
int state=GetKeyState(VK_SHIFT);
if(state&0x8000)
SetFocus(hControl[DirShowType]);
else
SetFocus(hControl[ID_CLOSE]);
break;
}
break;
case WM_KILLFOCUS:
SetNewWildCard(hWnd);
break;
case WM_SETFOCUS:
Edit_SetSel(hWnd,0,-1);
break;
}
return CallWindowProc(OldEditProc,hWnd,message,wParam,lParam);
}
*****************8
程序代码:///////////////////////////////////////
// Program Name: FileBox2.h
// Programmer: skyline
///////////////////////////////////////
// Constants
// #include "excpt.h"
#define ID_DRIVES 0
#define ID_DIRS 1
#define ID_FILES 2
#define ID_CLOSE 3
#define ID_FILELIST 4
#define ID_GROUP 5
#define ID_EDIT 6
#define ID_PATHS 7
#define BMPX 365
#define BMPY 100
#define XSIZE 403
#define YSIZE 360
#define MAXSTR 150
// Types
enum TCurMode{DRIVEMODE,DIRMODE,FILEMODE};
// Declarations
#define skyline_DefProc DefWindowProc
BOOL skyline_OnCreate(HWND hWnd,
CREATESTRUCT FAR * lpszCreateStruct);
void skyline_OnDestroy(HWND hWnd);
void skyline_OnClose(HWND hWnd);
void skyline_OnCommand(HWND hWnd,int id,
HWND hWndCtl,UINT CodeNotify);
HBRUSH skyline_OnCtlColor(HWND hWnd,HDC hdc,
HWND hWndChild,int type);
void skyline_OnGetMinMaxInfo(HWND hWnd,
MINMAXINFO FAR * lpMinMaxInfo);
void skyline_OnPaint(HWND hWnd);
void skyline_OnParentNotify(HWND hWnd,UINT msg,
HWND hWndChild,int idChild);
void skyline_OnRButtonDown(HWND hWnd,BOOL fDoubleClick,
int x,int y,UINT keyFalgs);
// Funcs
#ifdef WIN32
LRESULT CALLBACK WndProc(HWND hWnd,UINT Message,
WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK NewEditProc(HWND hWnd,UINT message,
WPARAM wParam,LPARAM lParam);
#else
LRESULT CALLBACK_export WndProc(HWND hWnd,UINT Message,
WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK_export NewEditProc(HWND hWnd,UINT message,
WPARAM wParam,LPARAm lParam);
#endif
void HandleRightButton(HWND hWnd);
BOOL Register(HINSTANCE hInst);
HWND Create(HINSTANCE hInst,int nCmdShow);
void SetListBox(HWND hWnd);
TCurMode DirShowType;
WNDPROC OldEditProc;
******************
程序代码:// FileBox.rc DRIVES BITMAP "DRIVES.BMP" FILES BITMAP "FILES.BMP" DIRS BITMAP "DIRS.BMP"
出现错误的是这个文件
程序代码:/***
*excpt.h - defines exception values, types and routines
*
* Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* This file contains the definitions and prototypes for the compiler-
* dependent intrinsics, support functions and keywords which implement
* the structured exception handling extensions.
*
* [Public]
*
****/
#if _MSC_VER > 1000
#pragma once
#endif
#ifndef _INC_EXCPT
#define _INC_EXCPT
#if !defined(_WIN32) && !defined(_MAC)
#error ERROR: Only Mac or Win32 targets supported!
#endif
#ifdef _MSC_VER
/*
* Currently, all MS C compilers for Win32 platforms default to 8 byte
* alignment.
*/
#pragma pack(push,8)
#endif /* _MSC_VER */
/*
[color=#0000FF]#ifdef __cplusplus
extern "C" {
#endif
[/color]*/
/* Define _CRTIMP */
#ifndef _CRTIMP
#ifdef _DLL
#define _CRTIMP __declspec(dllimport)
#else /* ndef _DLL */
#define _CRTIMP
#endif /* _DLL */
#endif /* _CRTIMP */
/* Define __cdecl for non-Microsoft compilers */
#if ( !defined(_MSC_VER) && !defined(__cdecl) )
#define __cdecl
#endif
/* Define _CRTAPI1 (for compatibility with the NT SDK) */
#ifndef _CRTAPI1
#if _MSC_VER >= 800 && _M_IX86 >= 300
#define _CRTAPI1 __cdecl
#else
#define _CRTAPI1
#endif
#endif
typedef enum _EXCEPTION_DISPOSITION {
ExceptionContinueExecution,
ExceptionContinueSearch,
ExceptionNestedException,
ExceptionCollidedUnwind
} EXCEPTION_DISPOSITION;
/*
* Prototype for SEH support function.
*/
#ifdef _M_IX86
/*
* Declarations to keep MS C 8 (386/486) compiler happy
*/
struct _EXCEPTION_RECORD;
struct _CONTEXT;
EXCEPTION_DISPOSITION __cdecl _except_handler (
struct _EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext
);
#elif defined(_M_MRX000) || defined(_M_ALPHA) || defined(_M_PPC)
/*
* Declarations to keep MIPS, ALPHA, and PPC compiler happy
*/
typedef struct _EXCEPTION_POINTERS *Exception_info_ptr;
struct _EXCEPTION_RECORD;
struct _CONTEXT;
struct _DISPATCHER_CONTEXT;
_CRTIMP EXCEPTION_DISPOSITION __C_specific_handler (
struct _EXCEPTION_RECORD *ExceptionRecord,
void *EstablisherFrame,
struct _CONTEXT *ContextRecord,
struct _DISPATCHER_CONTEXT *DispatcherContext
);
#endif
/*
* Keywords and intrinsics for SEH
*/
#ifdef _MSC_VER
#define GetExceptionCode _exception_code
#define exception_code _exception_code
#define GetExceptionInformation (struct _EXCEPTION_POINTERS *)_exception_info
#define exception_info (struct _EXCEPTION_POINTERS *)_exception_info
#define AbnormalTermination _abnormal_termination
#define abnormal_termination _abnormal_termination
unsigned long __cdecl _exception_code(void);
void * __cdecl _exception_info(void);
int __cdecl _abnormal_termination(void);
#endif
/*
* Legal values for expression in except().
*/
#define EXCEPTION_EXECUTE_HANDLER 1
#define EXCEPTION_CONTINUE_SEARCH 0
#define EXCEPTION_CONTINUE_EXECUTION -1
//#ifdef __cplusplus
//}
//#endif
#ifdef _MSC_VER
#pragma pack(pop)
#endif /* _MSC_VER */
#endif /* _INC_EXCPT */
//DRIVES BITMAP "DRIVES.BMP"
//FILES BITMAP "FILES.BMP"
//DIRS BITMAP "DIRS.BMP"
————————
c:\program files\microsoft visual studio\vc98\include\excpt.h(92) : error C2143: syntax error : missing ';' before '__cdecl'
c:\program files\microsoft visual studio\vc98\include\excpt.h(92) : error C2501: 'EXCEPTION_DISPOSITION' : missing storage-class or type specifiers
Error executing cl.exe.
错误对应的代码是:
程序代码:struct _EXCEPTION_RECORD;
struct _CONTEXT;
EXCEPTION_DISPOSITION __cdecl _except_handler (
struct _EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext
);
excpt.h里的一段? 网上看了下 没有相关的解决方案 有个说是VC装的不对 要重装? 这个……按说自学手册是很早的书 对库要求不是很高吧
我在两个文件里加上 #include "excpt.h"也解决不了问题
把程序移到VS2010下 遇到的问题是 char 类型无法适应VS下面的函数 有的函数的参数必须是LPCSTR / TCHAR 型的
我还得一个一个的把char 换成 TCHAR 然后在“****”字符串前面加L
有个函数 _getdcwd(0,(char *)s,125);的参数必须是char * 型的 擦! 又把它强制转换了。这样折腾下来 编译到是通过了 但是运行时 有个错误
就出在下面这个函数里
DlgDirList(hWnd,L"*.*",ID_FILELIST,
ID_PATHS,DirShowVal[DirShowType]);FileBox.exe 中的 0x7c80cf4a 处有未经处理的异常: 0xC0000005: 写入位置 0x004177ac 时发生访问冲突实在不知怎么搞了 求指导.
[ 本帖最后由 有容就大 于 2012-5-3 09:46 编辑 ]




