微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

非常感谢!我的项目快完成了

如何解决非常感谢!我的项目快完成了

我有一个 c 程序,我应该向它添加接口。我可以使用 WIn32 将我的代码插入到我的编码界面吗?

示例:这是我将二进制转换为八进制的代码

 void biTodec(){


 long long n;
 int dec = 0,i = 0,rem;
 printf("Enter a binary number: ");
 scanf("%lld",&n);
 while (n != 0) {
    rem = n % 10;
    n /= 10;
    dec += rem * pow(2,i);
    ++i;
 }
 printf("%lld in binary = %d in decimal",n,dec);
  return 0;
 }

现在,当单击执行转换时,我希望我的按钮在我的 API 中。这是我的 Win32 设计

  #include <stdio.h>
 #include <conio.h>
 #include <windows.h>
 #include <stdlib.h>

 LRESULT CALLBACK WindowProcedure(HWND,UINT,WParaM,LParaM);

 void AddMenus(HWND);
 void AddControls(HWND);

 HMENU hMenu;



  int WINAPI WinMain(HINSTANCE hinst,HINSTANCE hPrevInst,LPSTR 
 args,int 
  ncmdshow){



  WNDCLASSW kim = {0};
  kim.hbrBackground = (HBrush)COLOR_BACKGROUND;
  kim.hCursor = LoadCursor(NULL,IDC_ARROW);
  kim.hInstance = hinst;
  kim.lpszClassName = L"myWindowClass";
  kim.lpfnWndProc = WindowProcedure;

  if(!RegisterClassW(&kim))
    return -1;

  CreateWindowW(L"myWindowClass",L"Number System Converter",WS_OVERLAPPEDWINDOW | WS_VISIBLE,100,500,NULL,NULL);
 MSG msg={0};
 while(GetMessage(&msg,NULL)){
    TranslateMessage(&msg);
    dispatchMessage(&msg);

  }
  return 0;
   }


 LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WParaM 
 wp,LParaM lp){

  switch(msg){

 case WM_COMMAND:


    switch(wp){
    case 1:
        MessageBoxA(hWnd,"This project was made by bla bla for 
    seven (7) days. He had no any prior idea on interface so he spent 
  the 6 out of 7 days just for researching. copyright 2021","About 
  the 
  project",NULL);

        break;
    case 2:
        MessageBeep(MB_OK);
        break;
    case 3:
        PostQuitMessage(0);
        break;
    case 4:
        MessageBeep(MB_OK);
        break;
    case 5:
        MessageBeep(MB_OK);
        break;
    }
  case WM_CREATE:
    AddMenus(hWnd);
    AddControls(hWnd);
    break;

  case WM_DESTROY:
    PostQuitMessage(0);
    break;

  default:
    return DefWindowProcW(hWnd,msg,wp,lp);

  }
  }
  void AddMenus(HWND hWnd){
  hMenu = CreateMenu();
   HMENU hFileMenu = CreateMenu();
   HMENU hSubMenu = CreatePopupMenu();

   AppendMenu(hSubMenu,MF_STRING,4,"File");
   AppendMenu(hSubMenu,5,"Folder");
   AppendMenu(hSubMenu,MF_SEParaTOR,NULL);

   AppendMenu(hFileMenu,MF_POPUP,(UINT_PTR)hSubMenu,"Open");
   AppendMenu(hFileMenu,2,"Save");
   AppendMenu(hFileMenu,NULL);
   AppendMenu(hFileMenu,3,"Exit");

   AppendMenu(hMenu,(UINT_PTR)hFileMenu,"Options");
  AppendMenu(hMenu,1,"Help");


   SetMenu(hWnd,hMenu);
  }

  void AddControls(HWND hWnd){
CreateWindowW(L"Static",L"Choose the number system to be converted",WS_VISIBLE|WS_CHILD,30,300,20,hWnd,NULL );

  CreateWindowW(L"Edit",L"",WS_VISIBLE|WS_BORDER| WS_CHILD,60,80,120,NULL);
CreateWindowW(L"Button",L"Generate",WS_VISIBLE|WS_BORDER|WS_CHILD,200,NULL);
CreateWindowW(L"Edit",NULL);

CreateWindowW(L"Button",L"Binary",WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RAdioBUTTON|WS_TABSTOP,150,L"Decimal",180,L"Octal",210,L"Hexadecimal",240,NULL);

   CreateWindowW(L"Button",NULL);
  CreateWindowW(L"Button",WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RAdioBUTTON | WS_TABSTOP,NULL);

  }

是否可以将我的代码插入到 API 中?如果没有,您能建议我在我的程序中创建用户界面需要学习什么吗?

解决方法

我已经修复了您的代码并插入了您的转换功能。我没有添加更多的转换 - 你可以用我的例子自己做 - 我也没有添加增强功能。

要修复 Windows API 部分,请注意我有:

为控件保存了所有 HWND 以便能够获取/设置值。

使用 BS_AUTORADIOBUTTON 和 WS_GROUP 以便单选按钮照常工作。

为所有控件使用标识符,以便能够检查请求的是哪个操作。

使用适当的强制转换修复了各种警告。

#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <math.h>

#define ID_CONV_SRC_BINARY       0x100
#define ID_CONV_SRC_OCTAL        0x101
#define ID_CONV_SRC_DECIMAL      0x102
#define ID_CONV_SRC_HEXADECIMAL  0x103
#define ID_CONV_DST_BINARY       0x200
#define ID_CONV_DST_OCTAL        0x201
#define ID_CONV_DST_DECIMAL      0x202
#define ID_CONV_DST_HEXADECIMAL  0x203
#define ID_GENERATE              0x300

int srcConvert = 0;
int dstConvert = 0;

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

void AddMenus(HWND);
void AddControls(HWND);

HMENU hMenu;
HWND hNum,hOut;
HWND hWndInEdit;
HWND hWndOutEdit;
HWND hWndSrcBinary;
HWND hWndSrcOctal;
HWND hWndSrcDecimal;
HWND hWndSrcHexadecimal;
HWND hWndDstBinary;
HWND hWndDstOctal;
HWND hWndDstDecimal;
HWND hWndDstHexadecimal;

void biTodec(
    wchar_t * const inBuf,wchar_t * const outBuf,size_t outBufSize)
{
    long long n;
    int dec = 0,i = 0,rem;
    if (swscanf(inBuf,L"%lld",&n) == 1) {
        while (n != 0) {
            rem = n % 10;
            n /= 10;
            dec += rem * (1 << i);//pow(2,i);
            ++i;
        }
    }
    swprintf(outBuf,outBufSize,L"%d",dec);
}

int WINAPI WinMain(
    _In_ HINSTANCE hinst,_In_opt_ HINSTANCE hPrevInstance,_In_ LPSTR lpCmdLine,_In_ int nShowCmd) 
{
    WNDCLASSW kim = { 0 };
    kim.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    kim.hCursor = LoadCursor(NULL,IDC_ARROW);
    kim.hInstance = hinst;
    kim.lpszClassName = L"myWindowClass";
    kim.lpfnWndProc = WindowProcedure;

    if (!RegisterClassW(&kim))
        return -1;

    HWND hWnd = CreateWindowW(L"myWindowClass",L"Number System Converter",WS_OVERLAPPEDWINDOW | WS_VISIBLE,100,500,NULL,NULL);
    MSG msg = { 0 };
    while (GetMessage(&msg,0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

void Generate(HWND hWnd)
{
    wchar_t inBuf[30];
    wchar_t outBuf[30];

    if (Button_GetCheck(hWndSrcBinary))
        srcConvert = ID_CONV_SRC_BINARY;
    else if (Button_GetCheck(hWndSrcDecimal))
        srcConvert = ID_CONV_SRC_DECIMAL;
    else if (Button_GetCheck(hWndSrcOctal))
        srcConvert = ID_CONV_SRC_OCTAL;
    else if (Button_GetCheck(hWndSrcHexadecimal))
        srcConvert = ID_CONV_SRC_HEXADECIMAL;
    else
        srcConvert = 0;

    if (Button_GetCheck(hWndDstBinary))
        dstConvert = ID_CONV_DST_BINARY;
    else if (Button_GetCheck(hWndDstDecimal))
        dstConvert = ID_CONV_DST_DECIMAL;
    else if (Button_GetCheck(hWndDstOctal))
        dstConvert = ID_CONV_DST_OCTAL;
    else if (Button_GetCheck(hWndDstHexadecimal))
        dstConvert = ID_CONV_DST_HEXADECIMAL;
    else
        dstConvert = 0;

    GetWindowText(hWndInEdit,inBuf,sizeof(inBuf) / sizeof(inBuf[0]));
    if ((srcConvert & 0xFF) == (dstConvert & 0xFF))
        wcscpy(outBuf,inBuf);
    else if ((srcConvert == ID_CONV_SRC_BINARY) && (dstConvert == ID_CONV_DST_DECIMAL))
        biTodec(inBuf,outBuf,sizeof(outBuf));
    else {
        MessageBox(hWnd,L"Not implemented yet",L"Warning",MB_OK);
        outBuf[0] = 0;
    }
    SetWindowText(hWndOutEdit,outBuf);
}


LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) 
{
    switch (msg) {
    case WM_COMMAND:
        switch (wp) {
        case ID_CONV_SRC_BINARY:
        case ID_CONV_SRC_DECIMAL:
        case ID_CONV_SRC_HEXADECIMAL:
        case ID_CONV_SRC_OCTAL:
        case ID_CONV_DST_BINARY:
        case ID_CONV_DST_DECIMAL:
        case ID_CONV_DST_HEXADECIMAL:
        case ID_CONV_DST_OCTAL:
        case ID_GENERATE:
            Generate(hWnd);
            break;
        case 1:
            MessageBox(hWnd,L"Copyright 2021",L"About the project",MB_OK);
            break;
        case 2:
            MessageBeep(MB_OK);
            break;
        case 3:
            PostQuitMessage(0);
            break;
        case 4:
            MessageBeep(MB_OK);
            break;
        case 5:
            MessageBeep(MB_OK);
            break;
        }
        break;
    case WM_CREATE:
        AddMenus(hWnd);
        AddControls(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd,msg,wp,lp);
    }
    return 0;
}

void AddMenus(HWND hWnd) {
    hMenu = CreateMenu();
    HMENU hFileMenu = CreateMenu();
    HMENU hSubMenu = CreatePopupMenu();

    AppendMenu(hSubMenu,MF_STRING,4,L"File");
    AppendMenu(hSubMenu,5,L"Folder");
    AppendMenu(hSubMenu,MF_SEPARATOR,NULL);

    AppendMenu(hFileMenu,MF_POPUP,(UINT_PTR)hSubMenu,L"Open");
    AppendMenu(hFileMenu,2,L"Save");
    AppendMenu(hFileMenu,NULL);
    AppendMenu(hFileMenu,3,L"Exit");

    AppendMenu(hMenu,(UINT_PTR)hFileMenu,L"Options");
    AppendMenu(hMenu,1,L"Help");

    SetMenu(hWnd,hMenu);
}

void AddControls(HWND hWnd) {
    CreateWindowW(L"Static",L"Choose the number system to be converted",WS_VISIBLE | WS_CHILD,30,300,20,hWnd,NULL);

    hWndInEdit = CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_BORDER | WS_CHILD,60,80,120,NULL);
    CreateWindowW(L"Button",L"Generate",200,(HMENU)ID_GENERATE,NULL);
    hWndOutEdit = CreateWindowW(L"Edit",NULL);

    hWndSrcBinary = CreateWindowW(L"Button",L"Binary",WS_VISIBLE | WS_BORDER | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_GROUP,150,(HMENU)ID_CONV_SRC_BINARY,NULL);
    hWndSrcDecimal = CreateWindowW(L"Button",L"Decimal",WS_VISIBLE | WS_BORDER | WS_CHILD | BS_AUTORADIOBUTTON,180,(HMENU)ID_CONV_SRC_DECIMAL,NULL);
    hWndSrcOctal = CreateWindowW(L"Button",L"Octal",210,(HMENU)ID_CONV_SRC_OCTAL,NULL);
    hWndSrcHexadecimal = CreateWindowW(L"Button",L"Hexadecimal",240,(HMENU)ID_CONV_SRC_HEXADECIMAL,NULL);

    hWndDstBinary = CreateWindowW(L"Button",(HMENU)ID_CONV_DST_BINARY,NULL);
    hWndDstDecimal = CreateWindowW(L"Button",(HMENU)ID_CONV_DST_DECIMAL,NULL);
    hWndDstOctal = CreateWindowW(L"Button",(HMENU)ID_CONV_DST_OCTAL,NULL);
    hWndDstHexadecimal = CreateWindowW(L"Button",(HMENU)ID_CONV_DST_HEXADECIMAL,NULL);

}

有足够的改进空间。我做了最低限度的回答你的问题。如果您需要更多帮助,请打开更多问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?