使用纯C语言编写windows程序,工作量将会相当大,下面是一个小例子:
/雹橡稿*
* This is a simple windows program, it does nothing but draw an ellipse.
* Windows SDK, Win32 API ,Pure C, (Not C++ or MFC !!)
* Suxpert at gmail dot com, 2008/8/24
* */
#include <windows.h>
LONG WINAPI WndProc( HWND, UINT, WPARAM, LPARAM )
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow ){
/* The Entry for windows program, just like main() in dos */
WNDCLASS wc
HWND hwnd
MSG msg
wc.style = 0 // Class style
wc.lpfnWndProc = (WNDPROC)WndProc // Window procedure address
wc.cbClsExtra = 0 // Class extra bytes
wc.cbWndExtra = 0 // Window extra bytes
wc.hInstance = hInstance // Instance handle
wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ) // Icon handle
wc.hCursor = LoadCursor( NULL, IDC_ARROW ) //源孝 Cursor handle
wc.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 )// Background color
wc.lpszMenuName = NULL // Menu name
如前wc.lpszClassName = "WinSDKtest" // WNDCLASS name
RegisterClass( &wc )
hwnd = CreateWindow (
"WinSDKtest", // WNDCLASS name
"SDK Application", // Window title
WS_OVERLAPPEDWINDOW,// Window style
CW_USEDEFAULT, // Horizontal position
CW_USEDEFAULT, // Vertical position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
HWND_DESKTOP, // Handle of parent window
NULL, // Menu handle
hInstance, // Application's instance handle
NULL// Window-creation data
)
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 )
{
/* Windows will call this function anytime... */
PAINTSTRUCT ps
HDC hdc
switch(message){
case WM_PAINT:
hdc = BeginPaint( hwnd, &ps )
Ellipse( hdc, 0, 0, 800, 600 )
// Here we Draw an ellipse in the window of our program
EndPaint( hwnd, &ps )
break// Someone like to write return here.
case WM_DESTROY:
PostQuitMessage(0)
break
default:
return DefWindowProc( hwnd, message, wParam, lParam )
}
return 0
}
基本过程就是直接调用windows提供的API函数,完成从窗口创建,显示等界面功能到深层的文件 *** 作,注册表等甚至windows内核调试等高级功能。
VC编写dll文件 世雹 1、VC++->New->Project->Win32 Dynamic_Link Library2、选择创建一个空工程首先DLL需要一个头文件,所以新建一个C\C++ Header File这个头文件岩旦中必须包含想要输出的变量和函数,头文件必须定义用于输出的任何符号和数据结构。例如 :这里边又一个预定义#ifdef MYLIBAPI#else#define MYLIBAPI extern "C" __declspec(dllimport)#endif为什么要加这一句呢?因为搜枣帆相同的函数被C++编译后在符号库中的名字与C语言编译后的不同,所以如果使用的是标准C就要告诉编译器,要保留原来的名字,这里用extern “C”来实现。当然DLL还需要一个源文件,在工程中新建一个 C++Source File代码如下,此代码 中的宏定义是必须的。编译以后,会在工程的DEBUG文件夹下生成一个DLL文件。这样DLL文件就创建成功。下面展示一下如何使用这个DLL。代码如下,这里是显示引用DLL。运行以后程序就会输出:1+2=3证明调用动态链接库里的 Add函数成功。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)