#include <windows.h>
#include "stdio.h"
#define DLG_MAIN 1
#define ICO_MAIN 0x1000/* Declare Windows procedure */
BOOL CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */HINSTANCE g_hInstance;int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil){ int ret;
char buff[16];
g_hInstance=hThisInstance;
ret=DialogBox(0,MAKEINTRESOURCE(DLG_MAIN),0,WindowProcedure);
if(ret==-1)
{
sprintf(buff,"错误代码:%d",GetLastError());
MessageBox(0,buff,"错误",MB_OK);
}
}
/* This function is called by the Windows function DispatchMessage() */
//BOOL CALLBACK DialogProc(HWND hwndDlg,UINT UMsg,WPARAM wParam,LPARAM IParam)
BOOL CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HICON hIco;
switch (message) /* handle the messages */
{
case WM_CLOSE: EndDialog(hwnd,0);
return 1;
case WM_INITDIALOG: hIco=LoadIcon(g_hInstance,MAKEINTRESOURCE(ICO_MAIN));
SendMessage(hwnd,WM_SETICON,ICON_BIG,(DWORD)hIco);
break;
case WM_COMMAND: break;
default: /* for messages that we don't deal with */
return 0;
} return 0;
}
//////////////////////////////
资源文件
#define ICO_MAIN 0x1000
#define DLG_MAIN 1ICO_MAIN ICON "XXX_I.ico"DLG_MAIN DIALOG 50, 50, 200,150
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "XXX v0.10"
FONT 9, "宋体"
{
CTEXT "简单对话框\n2005-5-17", -1, 36, 14, 70, 19
DEFPUSHBUTTON "退出(&X)", IDOK, 58, 46, 50, 14
}
#include "stdio.h"
#define DLG_MAIN 1
#define ICO_MAIN 0x1000/* Declare Windows procedure */
BOOL CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/* Make the class name into a global variable */HINSTANCE g_hInstance;int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil){ int ret;
char buff[16];
g_hInstance=hThisInstance;
ret=DialogBox(0,MAKEINTRESOURCE(DLG_MAIN),0,WindowProcedure);
if(ret==-1)
{
sprintf(buff,"错误代码:%d",GetLastError());
MessageBox(0,buff,"错误",MB_OK);
}
}
/* This function is called by the Windows function DispatchMessage() */
//BOOL CALLBACK DialogProc(HWND hwndDlg,UINT UMsg,WPARAM wParam,LPARAM IParam)
BOOL CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HICON hIco;
switch (message) /* handle the messages */
{
case WM_CLOSE: EndDialog(hwnd,0);
return 1;
case WM_INITDIALOG: hIco=LoadIcon(g_hInstance,MAKEINTRESOURCE(ICO_MAIN));
SendMessage(hwnd,WM_SETICON,ICON_BIG,(DWORD)hIco);
break;
case WM_COMMAND: break;
default: /* for messages that we don't deal with */
return 0;
} return 0;
}
//////////////////////////////
资源文件
#define ICO_MAIN 0x1000
#define DLG_MAIN 1ICO_MAIN ICON "XXX_I.ico"DLG_MAIN DIALOG 50, 50, 200,150
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "XXX v0.10"
FONT 9, "宋体"
{
CTEXT "简单对话框\n2005-5-17", -1, 36, 14, 70, 19
DEFPUSHBUTTON "退出(&X)", IDOK, 58, 46, 50, 14
}
所以要有返回值吧?
资源我是照书抄的,应该没错吧
#include <windows.h>
编译运行通过
DialogBox(hThisInstance, MAKEINTRESOURCE(DLG_MAIN),0,WindowProcedure);
(1)新建工程/win32 Application
随便给工程命名,叫test吧
(2)下一步,取空模板,然后完成
(3)新建文件/c++ source file
取名test,把你给出的
//////////////////////////////
资源文件
之上的文件全部拷贝过去
(4)新键文件/resource script,命名test
你随便插入一个对话框,改名为DLG_MAIN,其他不要动
在记事本方式下打开test.rc,把你上面的资源文件对应部分
覆盖掉上面建立的对话框,运行就ok了
1.文件->新建->工程->basic->windows application
2.将你的程序复制贴到工程的main.cpp中,或移去原main.cpp加入你的文件(点工程的右键)
3.将你的资源文件加入项目中(如test.rc,但头上要加#include <windows.h>)
编译工程,运行
理论上,这样可以,但我的dev-cpp 4.9.9.2 gcc version 3.4.2 (mingw-special)的windres( version 2.15.94 20050118)有一BUG,它生成的res有问题,所以仍会出错,我用cygwin的 windres(version 2.15.91 20040725)替代它,可以编译运行,但汉字有问题,看来dev-cpp对windows的支持还不完全
-----------------------------------------------
另外,你可用命令行方式:
VC:
rc temp.rc
cl temp.cpp temp.rc user32.lib
dev-cpp
windres -i temp.rc --input-format=rc -o temp.res -O coff
g++ -o temp temp.cpp temp.res
只要 windres的版本用老些的,就可以运行
----------------------------------------------------------------------
直接用API写windows 的application,只是供学习或更高的研究用,一般开发,建议用MFC
改CTEXT "简单对话框\n2005-5-17", -1, 36, 14, 70, 19
为CTEXT "simple dialog\n2005-5-17", -1, 36, 14, 70, 19
居然可以了
1.rc temp.rc ;用vc的rc将temp转成res
2.cvtres temp.res /out:temp1.o ; 用vc的cvtres 将res转成coff格式的object文件
3.g++ -o temp temp.cpp temp1.o
-------------------------------------------
测试OK