第1个回答 2007-01-13
给你一个dev c++的sample:
工程包含两个文件,main.c和main.h
main.c代码:
#include <windows.h>
#pragma hdrstop
#include "Main.h"
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
#define IDC_MAIN_TEXT 1001
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0; // Null terminator
if(SetWindowText(hEdit, pszFileText))
bSuccess = TRUE; // It worked!
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0)// No need to bother if there's no text.
{
LPSTR pszText;
pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
if(pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwTextLength + 1))
{
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = 0;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
if(bSave)
{
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&ofn))
{
if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "Save file failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
else
{
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "Load of file failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
ES_WANTRETURN,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
break;
case WM_SIZE:
if(wParam != SIZE_MINIMIZED)
MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
HIWORD(lParam), TRUE);
break;
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case CM_FILE_OPEN:
DoFileOpenSave(hwnd, FALSE);
break;
case CM_FILE_SAVEAS:
DoFileOpenSave(hwnd, TRUE);
break;
case CM_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case CM_ABOUT:
MessageBox (NULL, "File Editor for Windows !\n Using the Win32 API" , "About...", 0);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
g_hInst = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = "MAINMENU";
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"A File Program",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
NULL, NULL, g_hInst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
main.h代码:
#define CM_FILE_SAVEAS 9072
#define CM_FILE_EXIT 9071
#define CM_FILE_OPEN 9070
#define CM_ABOUT 9069
是一个文件编辑器。
第2个回答 2007-01-13
模拟投票选举程序,其要求实现:
①对选举的投票,判定是否有候选人,有则进行相应的统计动作,否则弃掉
②输出候选人的姓名和得票数
③挑出获胜人员的名单和票数
一个有可以投几票(究竟投几票自己定),但只能把一票投给一个人,不能投两票,就是说投一票以上给一个人都只能算一票。如果最后的获胜者有两个或两个以上票数相同,则进行新一轮的投票。
#include"stdio.h"
#include"math.h"
main()
{int a,b,c,d,e,f,g,h,i,j,x,y,z;
a=b=c=d=e=f=0;
for(i=0;i<3;i++) //以下是投票的全过程,“i<3”也就是说一共有3个人投票,可以根据投票人数加以修改
{
printf("侯选人名单为:");
printf("\n");
printf("1号a, 2号b, 3号c, 4号d, 5号e, 6号f"); //a,b,c,d,e,f分别代表侯选人的名字
printf("\n");
printf("请输入你要选择的人的号码,一次选一个人,您可以选3次,但不能重复");
printf("\n");
printf("您一共有三票,请投第一票"); //投票开始
scanf("%d",&x);
printf("请投第二票");
scanf("%d",&y);
printf("请投第三票");
scanf("%d",&z);
if(x==1) //以下为:一个人可以投几票(究竟投几票自己定),但只能把一票投给一个人,不能投两票,就是说投一票以上给一个人都只能算一票
a+=1;
else if(x==2)
b+=1;
else if(x==3)
c+=1;
else if(x==4)
d+=1;
else if(x==5)
e+=1;
else if(x==6)
f+=1;
if(y==x)
g+=1;
else
{
if(y==1)
a+=1;
else if(y==2)
b+=1;
else if(y==3)
c+=1;
else if(y==4)
d+=1;
else if(y==5)
e+=1;
else if(y==6)
f+=1;
}
if(z==x)
g+=1;
else if(z==y)
g+=1;
else
{
if(z==1)
a+=1;
else if(z==2)
b+=1;
else if(z==3)
c+=1;
else if(z==4)
d+=1;
else if(z==5)
e+=1;
else if(z==6)
f+=1;
printf("a的票数:%d\nb的票数:%d\nc的票数:%d\nd的票数:%d\ne的票数:%d\nf的票数:%d\n",a,b,c,d,e,f);
printf("\n");
}
}
h=a; //排序 h代表胜出者的票数
if(b>h)
h=b;
if(c>h)
h=c;
if(d>h)
h=d;
if(e>h)
h=e;
if(f>h)
h=f;
printf("\n");
if(h==a) // 找出胜出者
{printf("胜出者为a,所得票数为:%d\n",a);
j=1;}
if(h==b)
{printf("胜出者为b,所得票数为:%d\n",b);
j+=1;}
if(h==c)
{printf("胜出者为c,所得票数为:%d\n",c);
j+=1;}
if(h==d)
{printf("胜出者为d,所得票数为:%d\n",d);
j+=1;}
if(h==e)
{printf("胜出者为e,所得票数为:%d\n",e);
j+=1;}
if(h==f)
{printf("胜出者为f,所得票数为:%d\n",f);
j+=1;}
if(j>1)
printf("因为胜出者不是一个人,所以要从新选举!");
printf("\n");
}本回答被提问者采纳