【发布时间】:2021-09-03 18:30:31
【问题描述】:
我有一个 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, 100, 500, 500,
NULL,NULL,NULL,NULL);
MSG msg={0};
while(GetMessage(&msg, NULL,NULL, 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,MF_STRING, 5, "Folder");
AppendMenu(hSubMenu, MF_SEPARATOR, NULL, NULL);
AppendMenu(hFileMenu,MF_POPUP,(UINT_PTR)hSubMenu, "Open");
AppendMenu(hFileMenu,MF_STRING, 2, "Save");
AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
AppendMenu(hFileMenu,MF_STRING, 3, "Exit");
AppendMenu(hMenu, MF_POPUP ,(UINT_PTR)hFileMenu, "Options");
AppendMenu(hMenu, MF_POPUP ,1, "Help");
SetMenu(hWnd, hMenu);
}
void AddControls(HWND hWnd){
CreateWindowW(L"Static",L"Choose the number system to be converted",
WS_VISIBLE|WS_CHILD,100, 30, 300, 20, hWnd, NULL, NULL, NULL );
CreateWindowW(L"Edit", L"", WS_VISIBLE|WS_BORDER| WS_CHILD, 60, 80,
120, 30, hWnd, NULL, NULL,NULL);
CreateWindowW(L"Button", L"Generate", WS_VISIBLE|WS_BORDER|WS_CHILD,
200, 80, 80, 20, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Edit", L"", WS_VISIBLE|WS_BORDER| WS_CHILD, 300, 80,
120, 30, hWnd, NULL, NULL,NULL);
CreateWindowW(L"Button", L"Binary",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP, 60, 150,
120, 20, hWnd,NULL , NULL, NULL);
CreateWindowW(L"Button", L"Decimal",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP, 60, 180,
120, 20, hWnd,NULL , NULL, NULL);
CreateWindowW(L"Button", L"Octal",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP, 60, 210,
120, 20, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Button", L"Hexadecimal",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP,60, 240,
120, 20, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Button", L"Binary",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP, 300, 150,
120, 20, hWnd,NULL , NULL, NULL);
CreateWindowW(L"Button", L"Decimal",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP, 300, 180,
120, 20, hWnd,NULL , NULL, NULL);
CreateWindowW(L"Button", L"Octal",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON|WS_TABSTOP, 300, 210,
120, 20, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Button", L"Hexadecimal",
WS_VISIBLE|WS_BORDER|WS_CHILD|BS_RADIOBUTTON | WS_TABSTOP, 300,
240, 120, 20, hWnd,NULL, NULL, NULL);
}
是否可以将我的代码插入 API?如果没有,你能建议我在我的程序中创建用户界面需要学习什么吗?
【问题讨论】:
-
为什么不使用 MSVC 对话框编辑器来代替复杂的代码?
-
@pau 因为,你知道,对话框编辑器已经被破坏了近十年。不依赖经常被破坏的技术是完全合理的。
-
您是否尝试编译您的代码?它包含错误和警告。请先解决这个问题。
-
这与您的previous question 基本相同,最终归结为:如何编写 Windows 应用程序同时避免学习如何编写 Windows 应用程序?这不会有用。
-
也许您应该首先专注于编写一个可以工作的基本 Windows GUI 程序。如果您对此有困难;然后问具体问题。您可以从问题中显示的代码开始。第一步,忘记你的转换例程,专注于消除所有编译错误和警告。
标签: c windows user-interface winapi visual-studio-code