我对Visual Studio 2015,C ++,宏有一个令人困惑的问题。我确实在后台有一个更大的项目,但下面的玩具示例说明了这个问题。
我有以下C ++代码(oma8.rc,oma8.h,oma8.cpp)来创建玩具对话框。它很好地编译了gcc编译器:
windres -O coff -i oma8.rc -o oma8.res
g ++ oma8.cpp oma8.res -o oma8
// oma8.cpp
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include "stdlib.h"
#include "oma8.h"
using namespace std;
string param2;
int b1, b3;
string GetInputText( HWND dlg, int resid ) {
HWND hc = GetDlgItem( dlg, resid );
int n = GetWindowTextLength( hc ) + 1;
string s( n, 0 );
GetWindowText( hc, &s[0], n );
return s;
}
int GetCheck( HWND dlg, int resid) {
HWND hc = GetDlgItem(dlg,resid);
int n = IsDlgButtonChecked( dlg, resid);
return n;
}
BOOL CALLBACK DialogProc( HWND hwnd, UINT message,
WPARAM wp, LPARAM ) {
switch ( message ) {
case WM_COMMAND: {
int ctrl = LOWORD( wp );
int event = HIWORD( wp );
if ( ctrl == IDCANCEL && event == BN_CLICKED ) {
DestroyWindow (hwnd);
return TRUE;
}
else if ( ctrl == IDOK && event == BN_CLICKED ) {
param2 = GetInputText( hwnd, IDC_EDIT1 );
b1 = GetCheck( hwnd, RB1 );
b3 = GetCheck( hwnd, RB3 ); // ignore the other buttons for now
DestroyWindow (hwnd);
return TRUE;
}
return FALSE;
}
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
case WM_CLOSE:
DestroyWindow (hwnd);
return TRUE;
}
return FALSE;
}
int main() {
HWND dlg = CreateDialog( GetModuleHandle(0),
MAKEINTRESOURCE( IDD_DIALOG1 ),
0, DialogProc );
MSG msg;
while ( GetMessage( & msg, 0, 0, 0 ) ) {
if ( ! IsDialogMessage( dlg, & msg ) ) {
TranslateMessage( & msg );
DispatchMessage( & msg );
}
}
cout << "corner text: " << param2 << " as a number: " << atof(¶m2[0]) << endl;
cout << "radio1: " << b1 << " radio3: " << b3 << endl;
}
// oma8.rc (stripped 3 comment lines from start)
#include <windows.h>
#include "oma8.h"
//#include "winresrc.h" // apparently not needed
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 20, 20, TABNR(8), THISLINEHEIGHT(15)
STYLE DS_3DLOOK | DS_SETFONT | WS_CAPTION | WS_SYSMENU | WS_VISIBLE | WS_SIZEBOX
//STYLE DS_CENTER | DS_SHELLFONT | DS_FIXEDSYS | DS_MODALFRAME | WS_VSCROLL | WS_POPUP
CAPTION "Caption here"
FONT 10, "Arial"
{
LTEXT "Corner text",IDC_STATIC,LMARG,UPMARG,60,RHEIGHT
EDITTEXT IDC_EDIT1, 60, UPMARG, 60,FIELDH, ES_AUTOHSCROLL
DEFPUSHBUTTON "OK",IDOK,TABNR(5),UPMARG,TABW,FIELDH
PUSHBUTTON "Cancel",IDCANCEL,TABNR(6),UPMARG,TABW,FIELDH
LTEXT "Block1, line 1",IDC_STATIC,TABNR(RBCOLUMN),THISLINEHEIGHT(PARAMLINE),TEXTW,RHEIGHT
AUTORADIOBUTTON "B1, line 2",RB1,TABNR(RBCOLUMN),THISLINEHEIGHT(PARAMLINE+1),TEXTW,RHEIGHT,WS_GROUP
AUTORADIOBUTTON "B1, line 3",RB2,TABNR(RBCOLUMN),THISLINEHEIGHT(PARAMLINE+2),TEXTW,RHEIGHT
LTEXT "Block2 col 1",IDC_STATIC,TABNR(SNGLCOLUMN),THISLINEHEIGHT(CHOICELINE),TABW,RHEIGHT
AUTORADIOBUTTON "B2 col 2",RB3,TABNR(SNGLCOLUMN+1),THISLINEHEIGHT(CHOICELINE),TABW,RHEIGHT, WS_GROUP
AUTORADIOBUTTON "B2 col 3",RB4,TABNR(SNGLCOLUMN+2),THISLINEHEIGHT(CHOICELINE),TABW,RHEIGHT
LTEXT "Block3, line 1",IDC_STATIC,TABNR(RANGECOLUMN),THISLINEHEIGHT(TXLINE),TEXTW,RHEIGHT
AUTORADIOBUTTON "B3, line 2",RB5,TABNR(RANGECOLUMN),THISLINEHEIGHT(RXLINE),TEXTW,RHEIGHT,WS_GROUP
AUTORADIOBUTTON "B3, line 3",RB6,TABNR(RANGECOLUMN),THISLINEHEIGHT(SCLINE),TEXTW,RHEIGHT
}
// oma8.h (stripped 2 comment lines from start)
#define IDD_DIALOG1 100
// basic buttons
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#ifndef IDOK
#define IDOK 40000
#endif
#ifndef IDCANCEL
#define IDCANCEL 40001
#endif
// measures
#define LMARG 6
#define UPMARG 5
#define TEXTW 120 // rb text width
#define TABW 50 // tab width
#define FIELDW 30 // fill-in field w
#define FIELDH 13 // fill-in field h
#define RHEIGHT 14 // row height
// line numbers
#define PARAMLINE 2
#define NOFPARAMS 2
#define OFFSET2 2
#define OFFSET3 5
#define CHOICELINE (PARAMLINE+NOFPARAMS+OFFSET2)
#define TXLINE (CHOICELINE+1)
#define RXLINE (CHOICELINE+2)
#define SCLINE (CHOICELINE+3)
#define RANGETITLELINE 2
#define RANGEFIELDLINE (RANGETITLELINE+1)
#define DETECTORLINE (CHOICELINE+OFFSET3)
#define THISLINEHEIGHT(x) (UPMARG+(x)*RHEIGHT)
// column numbers
#define TABNR(x) (LMARG+TABW*(x))
#define RBCOLUMN 1
#define RANGECOLUMN 5
#define SNGLCOLUMN 2
#define DTCTCOLUMN 7
// parameter buttons
#define IDC_EDIT1 1002
#define RB1 1010
#define RB2 1011
#define RB3 1012
#define RB4 1013
#define RB5 1014
#define RB6 1015
和oma8.exe(控制台中给出的所有命令)打开一个对话框,看起来和我想要的一样,万岁。但是,我有理由说我必须用VS2015编译它。 (稍后会将其插入某个样板代码中,但这与此无关。)
当尝试使用VS2015编译相同的代码时,我得到一堆与.rc文件相关的错误消息,特别是在其宏函数上。前4个是
错误RC2112 BEGIN预期在对话框oma8.rc 11中 找不到错误RC2135文件:0x00040000L oma8.rc 12
找不到错误RC2135文件:这里是oma8.rc 14
找不到错误RC2135文件:10 oma8.rc 15
此外,如果我改变了行
#define THISLINEHEIGHT(x) (UPMARG+(x)*RHEIGHT)
#define TABNR(x) (LMARG+TABW*(x))
到
#define THISLINEHEIGHT(x) (UPMARG + (x) * RHEIGHT)
#define TABNR(x) (LMARG + TABW * (x))
我只收到一条错误消息:
错误RC2104未定义关键字或密钥名称:* oma8.rc 11
我的链接器/编译器选项必须有趣吗?当我从现有代码创建项目时,我选择了Console App并将所有其他选项保留为默认值。 (不记得那些是什么,对不起,只是很多缩略词。)
我花了一周时间用谷歌搜索并尝试不同的修改,但还没有能够解决这个问题。我不知道还有什么可以尝试。我应该在哪里看看?任何想法和进一步的问题都将受到欢迎。
我的系统是Win7,64位。 g ++版本说g ++(tdm-1)5.1.0(C)2015自由软件基金会。