在 Windows 用API可以..?

ranxufeng 2013-04-24 12:26:48
在 Windows 可以用API调用一个函数 从而实现实现那个函数功能吗?
例如:#include <windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR line,int cmd)
{
static TCHAR AppName[]=TEXT("99");
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=AppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),AppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(AppName,TEXT("乘法口决表"),\
WS_OVERLAPPEDWINDOW,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
NULL,\
NULL,\
hinstance,\
NULL);
ShowWindow(hwnd,cmd);
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)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int x=0,y=0;
int i,j;
int len;
TCHAR buf[50];
TEXTMETRIC tm;
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
x=tm.tmAveCharWidth;
y=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
for(i=1;i!=10;++i)
{
for(j=1;j!=i+1;++j)
{
len=wsprintf(buf,TEXT("%dx%d=%-4d"),j,i,i*j);
TextOut(hdc,j*len*x,i*y,buf,len);
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}
可以把
for(i=1;i!=10;++i)
{
for(j=1;j!=i+1;++j)
{
len=wsprintf(buf,TEXT("%dx%d=%-4d"),j,i,i*j);
TextOut(hdc,j*len*x,i*y,buf,len);
}
}
换成一个函数 入加入一个求和 为什么在窗口上不能输入呢?
int sum()
{
int x,y,sum;
scanf("%d%d",&x,&y);
sum = x + y;
printf("%d",sum);
return 0;
}
为什么加入在窗口中 在平台上不能输入呢?
...全文
91 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhcosin 2013-04-24
  • 打赏
  • 举报
回复
scanf 是从终端输入的,不是从窗口输入的, OK ?
ranxufeng 2013-04-24
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929……
表示不懂 刚刚接触到API
ranxufeng 2013-04-24
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
窗口输入用VB6的InputBox函数。
刚刚学 表示不懂 能不能用个代码说明 ??谢谢
赵4老师 2013-04-24
  • 打赏
  • 举报
回复
A simple data entry dialog with functionally similar to a MessageBox.
Purpose: A simple data entry dialog functionally similar to a MessageBox. Any feedback is welcome.
 Collapse | Copy Code
// Sample Utilization:

//Example 1
string s = string.Empty;
if (InputBox.ShowDialog("Type your name:", ref s) == DialogResult.Cancel) return;
 
//Example 2
int x = ((int?)InputBox.Create("Multiply by 2:", "1") ?? 0) * 2;
Here is the InputBox class:
 Collapse | Copy Code
/// <summary>
/// Input dialog box used for simple user data entry.
/// </summary>
public static class InputBox
{
    /// <summary>
    /// Standard modal DialogResult method used for simple user input.
    /// </summary>
    /// <param name="caption">Title for the Input form.</param>
    /// <param name="defaultValue">Default to be displayed in the textbox.</param>
    /// <returns>DialogResult, and updates the value of reference parameter 
    /// defaultValue if the result is DialogResult.OK.</returns>
    public static DialogResult ShowDialog(string caption, ref string defaultValue)
    {
        InputForm inForm = new InputForm(caption, defaultValue);
 
        if (inForm.ShowDialog() == DialogResult.OK)
        {
            defaultValue = inForm.StringValue;
            return DialogResult.OK;
        }
        return DialogResult.Cancel;
    }
 
    /// <summary>
    /// Direct modal InputBox method, used for immediate typecasting.
    /// Shows the dialog as part of its creation.
    /// </summary>
    /// <param name="caption">Title for the Input form.</param>
    /// <param name="defaultValue">Default to be displayed in the textbox.</param>
    /// <returns>InputForm ready to be typecast to the appropriate value type.</returns>
    public static InputForm Create(string caption, string defaultValue)
    {
        InputForm inForm = new InputForm(caption, defaultValue);
        if (inForm.ShowDialog() == DialogResult.Cancel) inForm.StringValue = string.Empty;
        return inForm;
    }
}
Here is the InputForm (used by InputBox):
 Collapse | Copy Code
/// <summary>
/// Display class for InputBox.  Should not be used directly, use InputBox instead.
/// </summary>
public partial class InputForm : Form
{
    #region Constructors

    /// <summary>
    /// Default constructor.
    /// </summary>
    public InputForm()
    {
        InitializeComponent();
    }
 
    /// <summary>
    /// Parameterized constructor.
    /// </summary>
    /// <param name="caption">Title for the Input form.</param>
    /// <param name="defaultValue">Default to be displayed in the textbox.</param>
    public InputForm(string caption, string defaultValue) : this()
    {
        this.Text = caption;
        txtValue.Text = defaultValue;
    }
 
    #endregion Constructors
 
    #region Public Properties
 
    /// <summary>
    /// Accessor for the textbox value.
    /// </summary>
    public string StringValue
    {
        get { return txtValue.Text; }
        set { txtValue.Text = value; }
    }
 
    #endregion Public Properties
 
    #region Typecasting Operators
 
    /// <summary>
    /// Typecasting operator.
    /// </summary>
    /// <param name="inForm">Completed InputForm with user entry.</param>
    /// <returns>Nullable value: double.</returns>
    public static explicit operator double?(InputForm inForm)
    {
        if (inForm.StringValue == string.Empty) return null;
        try { return double.Parse(inForm.StringValue); }
        catch { return null; }
    }
 
    /// <summary>
    /// Typecasting operator.
    /// </summary>
    /// <param name="inForm">Completed InputForm with user entry.</param>
    /// <returns>Nullable value: int.</returns>
    public static explicit operator int?(InputForm inForm)
    {
        if (inForm.StringValue == string.Empty) return null;
        try { return Int32.Parse(inForm.StringValue); }
        catch { return null; }
    }
 
    /// <summary>
    /// Typecasting operator.
    /// </summary>
    /// <param name="inForm">Completed InputForm with user entry.</param>
    /// <returns>Nullable value: long.</returns>
    public static explicit operator long?(InputForm inForm)
    {
        if (inForm.StringValue == string.Empty) return null;
        try { return Int64.Parse(inForm.StringValue); }
        catch { return null; }
    }
 
    /// <summary>
    /// Typecasting operator.
    /// </summary>
    /// <param name="inForm">Completed InputForm with user entry.</param>
    /// <returns>String value.</returns>
    public static explicit operator string(InputForm inForm)
    {
        return inForm.StringValue;
    }
 
    #endregion Typecasting Operators
}


赵4老师 2013-04-24
  • 打赏
  • 举报
回复
窗口输入用VB6的InputBox函数。
zhcosin 2013-04-24
  • 打赏
  • 举报
回复
引用 2 楼 ranxufeng 的回复:
引用 1 楼 zhcosin 的回复:scanf 是从终端输入的,不是从窗口输入的, OK ? 不过 在窗口可以输入吗?
你这点基础都没有哇,你在窗口上弄个文本框,在程序里读取文本框的字符串转成数字不就行了吗。
ranxufeng 2013-04-24
  • 打赏
  • 举报
回复
引用 1 楼 zhcosin 的回复:
scanf 是从终端输入的,不是从窗口输入的, OK ?
不过 在窗口可以输入吗?

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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