有关Win32的问题

bill_li 2004-12-23 12:51:28
我才学不久,老师给了两个作业题,我不太明白,所以问问大家了。


1。创建2个编译框和一个静态文本框,以及一个“计算”按纽。用户在2个编译框中输入数字,然后单击“计算”,在静态文本框中显示求和结果。
在里面我不太会 “在静态文本框中显示求和结果”。



2。编一个程序,绘制一个五角星,五角星用红线绘制,显示在屏幕的中间,五角星的边取客户区宽度和高度最小值。
我不明白“五角星”怎么编 “五角星的边取客户区宽度和高度最小值。”怎么取。
...全文
114 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bill_li 2005-01-10
  • 打赏
  • 举报
回复
#include <windows.h>
#include <stdio.h>
#include <math.h>

#define MAX_NUM 10000
#define PANE_CONS 100
#define PI 3.1415927

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
static TCHAR szAppName[]=TEXT("RectSample");
static TCHAR szClassName[]=TEXT("RectSampleClass");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szClassName;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires WinNT or higher version!"),
szAppName,MB_ICONERROR);
return 0;
}

hWnd=CreateWindow(
szClassName,
TEXT("Drawing Line Funtion Sample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

//
ShowWindow(hWnd,nCmdShow);
//更新窗口
UpdateWindow(hWnd);

//消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;

static int cxClient,cyClient; //记录用户区宽度和高度
static double dblTheta0=0,nOuterRadius=0,nInnerRadius;
static POINT ptOuter[5],ptInner[5],pt[3];
int i;
static HPEN hpnBlue,hpnOld; //画笔句柄
static HBRUSH hbrRed,hbrOld; //画刷句柄

switch(Msg)
{
case WM_CREATE:
//创建逻辑画笔,实线,3个象素,兰色
hpnBlue=CreatePen(PS_SOLID,3,RGB(0,0,255));
hbrRed=CreateSolidBrush(RGB(255,0,0));
return 0;

case WM_SIZE:
//获取用户区大小
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
nOuterRadius=cxClient<cyClient?cxClient/4.0:cyClient/4.0;
nInnerRadius=nOuterRadius*sin(PI/10)/sin(PI*7/10);
//求点
for(i=0;i<5;i++)
{
ptOuter[i].x=(int)(cxClient/2+nOuterRadius*cos(dblTheta0+PI/2+PI*2*i/5));
ptOuter[i].y=(int)(cyClient/2-nOuterRadius*sin(dblTheta0+PI/2+PI*2*i/5));
ptInner[i].x=(int)(cxClient/2+nInnerRadius*cos(dblTheta0+PI*3/10+PI*2*i/5));
ptInner[i].y=(int)(cyClient/2-nInnerRadius*sin(dblTheta0+PI*3/10+PI*2*i/5));
}

return 0;

case WM_PAINT:
hDC=BeginPaint(hWnd,&ps);

//将hpnRed、hbrBlue选入设备描述表
hpnOld=(HPEN)SelectObject(hDC,hpnBlue);
hbrOld=(HBRUSH)SelectObject(hDC,hbrRed);

for(i=0;i<5;i++)
{
pt[0].x=cxClient/2;
pt[0].y=cyClient/2;
pt[1].x=ptInner[i].x;
pt[1].y=ptInner[i].y;
pt[2].x=ptOuter[i].x;
pt[2].y=ptOuter[i].y;
Polygon(hDC,pt,3);
}

SelectObject(hDC,hbrOld);

for(i=0;i<5;i++)
{
pt[0].x=cxClient/2;
pt[0].y=cyClient/2;
pt[1].x=ptInner[i+1-(i+1)/5*5].x;
pt[1].y=ptInner[i+1-(i+1)/5*5].y;
pt[2].x=ptOuter[i].x;
pt[2].y=ptOuter[i].y;
Polygon(hDC,pt,3);
}
Arc(hDC,cxClient/2-(int)nOuterRadius,cyClient/2-(int)nOuterRadius,
cxClient/2+(int)nOuterRadius,cyClient/2+(int)nOuterRadius,
cxClient/2-(int)nOuterRadius,cyClient/2-(int)nOuterRadius,
cxClient/2-(int)nOuterRadius,cyClient/2-(int)nOuterRadius);

//还原系统默认的画笔、画刷
SelectObject(hDC,hpnOld);

EndPaint(hWnd,&ps);
return 0;

case WM_DESTROY:
//删除不再使用的逻辑画笔
DeleteObject(hpnBlue);
DeleteObject(hbrRed);

PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
bill_li 2005-01-10
  • 打赏
  • 举报
回复
#include <windows.h>
#include <stdio.h>
#include <string.h>

#define ID_EDIT1 1
#define ID_EDIT2 2
#define ID_STDC1 3
#define ID_STDC2 4
#define ID_STDC3 5
#define ID_COMPT 6
#define ID_CLOSE 7

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpcmdLine, int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Compute");
static TCHAR szClassName[] = TEXT("ComputeClass");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hCursor = LoadCursor( NULL,IDC_ARROW );
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName;

if ( !RegisterClass( &wndclass ) )
{
MessageBox( NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR );
return 0;
}

hwnd = CreateWindow( szClassName, TEXT("Simple Computation Program"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL );

ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );

while ( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit1,hwndEdit2;
int cxChar,cyChar;
static HWND hwndStatic1,hwndStatic2,hwndStatic3;
static HWND hwndCmmdBt1,hwndCmmdBt2;
static int cxClient,cyClient;
static int cWinWidth,cWinHeight;
TCHAR szOper1[]=TEXT(" ");
static TCHAR szOper2[]=TEXT("");
TCHAR szReslt[]="";
float fReslt;
HDC hdc;
// PAINTSTRUCT ps;
TEXTMETRIC tm;

switch( message )
{
case WM_CREATE:
hwndEdit1 = CreateWindow( TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_LEFT,
0, 0, 0, 0, hwnd, (HMENU) ID_EDIT1,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
hwndEdit2 = CreateWindow( TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_LEFT,
0, 0, 0, 0, hwnd, (HMENU) ID_EDIT2,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
hwndStatic1 = CreateWindow( TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 0, 0, hwnd, (HMENU) ID_STDC1,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
hwndStatic2 = CreateWindow( TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 0, 0, hwnd, (HMENU) ID_STDC2,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
hwndStatic3 = CreateWindow( TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER,
0, 0, 0, 0, hwnd, (HMENU) ID_STDC3,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
hwndCmmdBt1 = CreateWindow( TEXT("button"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_DEFPUSHBUTTON ,
0, 0, 0, 0, hwnd, (HMENU) ID_COMPT,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
hwndCmmdBt2 = CreateWindow( TEXT("button"), NULL,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0, hwnd, (HMENU) ID_CLOSE,
(HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );
return 0;

case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);

hdc = GetDC( hwnd );

GetTextMetrics( hdc, &tm );
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;


cWinWidth = 10 * cxChar + 2 * GetSystemMetrics( SM_CXBORDER );
cWinHeight = cyChar + 2 * GetSystemMetrics( SM_CYBORDER );
MoveWindow( hwndEdit1, cWinWidth, cWinHeight, cWinWidth, cWinHeight, TRUE );
MoveWindow( hwndEdit2, cWinWidth+2*cWinWidth+4,cWinHeight, cWinWidth, cWinHeight, TRUE );
MoveWindow( hwndStatic1, cWinWidth+cWinWidth+2,cWinHeight, cWinWidth, cWinHeight, TRUE );
MoveWindow( hwndStatic2, cWinWidth+3*cWinWidth+6,cWinHeight, cWinWidth, cWinHeight, TRUE );
MoveWindow( hwndStatic3, cWinWidth+4*cWinWidth+8,cWinHeight, 2*cWinWidth, cWinHeight, TRUE );
MoveWindow( hwndCmmdBt1, cWinWidth+cWinWidth+2,2*cWinHeight+cWinWidth, cWinWidth, cWinHeight+8, TRUE );
MoveWindow( hwndCmmdBt2, cWinWidth+3*cWinWidth+6,2*cWinHeight+cWinWidth, cWinWidth, cWinHeight+8, TRUE );


//初始值
SetWindowText(hwndStatic1,TEXT("+"));
SetWindowText(hwndStatic2,TEXT("="));
SetWindowText(hwndCmmdBt1,TEXT("计算"));
SetWindowText(hwndCmmdBt2,TEXT("关闭"));
ReleaseDC( hwnd, hdc );
return 0;

case WM_SETFOCUS:
SetFocus( hwndEdit1 );
return 0;

case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_COMPT:
GetWindowText(hwndEdit1,szOper1,GetWindowTextLength(hwndEdit1)+1);
GetWindowText(hwndEdit2,szOper2,GetWindowTextLength(hwndEdit2)+1);
fReslt=atof(szOper1)+atof(szOper2);
sprintf(szReslt,"%f",fReslt);
SetWindowText(hwndStatic3,szReslt);
break;
case ID_CLOSE:
SendMessage(hwnd,WM_CLOSE,0,0L);
break;
}
return 0;

case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hwnd, message, wParam, lParam );
}
oyljerry 2004-12-23
  • 打赏
  • 举报
回复
1,用几个控件,接收输入,计算后,显示一下
2,LineTo画线
blhy 2004-12-23
  • 打赏
  • 举报
回复
1、假设结果算出来存在int a 里。假如你用的是SDK 编的话,是这样:
TCHAR char[12];
wsprintf(char ,_T("%d"), a);
SetWindowText(hdc, char); // 这个函数的参数我不太记得清了,你自己查下。其中hdc是你
//的静态文本框的窗口句柄。
2、可以用GetClientRect得到客户区的坐标。
用MoveTo 和LineTo画线。
wangyangcheng 2004-12-23
  • 打赏
  • 举报
回复
处理好Edit的LBUTTONDOWN的消息。
danyueer 2004-12-23
  • 打赏
  • 举报
回复
2 用设备对象绘图,参考函数LineTo
danyueer 2004-12-23
  • 打赏
  • 举报
回复
1 设定Eidt的关联变量,然后UpdateData(TRUE);
HunterForPig 2004-12-23
  • 打赏
  • 举报
回复
怎么跑到此处来啊,兄弟!
nuaawenlin 2004-12-23
  • 打赏
  • 举报
回复
Windows程序设计里面讲得很清楚,160元,要学Windows编程,这两本书必备

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧