㈠ 一個簡單的win32應用程序
#include<windows.h>
#include<stdio.h>
#define IDC_EDIT1 1
#define IDC_EDIT2 2
#define IDC_STATIC 3
#define IDC_BTN 4
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
HINSTANCE hinst;
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
TCHAR* classname="test";
hinst=hInstance;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)::GetStockObject(DKGRAY_BRUSH);
wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=::LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=classname;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
::RegisterClass(&wndclass);
HWND hwnd=::CreateWindow(classname,TEXT("ADD"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
250,120,NULL,NULL,hInstance,0);
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
MSG msg;
while(::GetMessage(&msg,NULL,0,0)){
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static double sum;
int c=01011;
static char a[20],b[20],s[40];
switch(uMsg)
{
case WM_CREATE:
{
::CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|ES_LEFT|ES_NUMBER,10,10,60,20,hwnd,(HMENU)IDC_EDIT1,hinst,0);
::CreateWindow("STATIC","+",WS_CHILD|WS_VISIBLE|SS_CENTER,70,10,30,20,hwnd,(HMENU)IDC_STATIC,hinst,0);
::CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|ES_LEFT|ES_NUMBER,100,10,60,20,hwnd,(HMENU)IDC_EDIT2,hinst,0);
::CreateWindow("BUTTON","=",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,170,10,60,20,hwnd,(HMENU)IDC_BTN,hinst,0);
}break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BTN:
{
::GetDlgItemText(hwnd,IDC_EDIT1,a,20);
::GetDlgItemText(hwnd,IDC_EDIT2,b,20);
sum=atof(a)+atof(b);
sprintf(s,"%s+%s=%lf",a,b,sum);
HDC hdc=::GetDC(hwnd);
::TextOut(hdc,10,40,s,strlen(s));
::ReleaseDC(hwnd,hdc);
}break;
}
}break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc=::BeginPaint(hwnd,&ps);
::TextOut(hdc,10,40,s,strlen(s));
::EndPaint(hwnd,&ps);
}break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
㈡ 一個簡單的win32窗口程序
HACCELhAccelTable;
hAccelTable=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_WIN32));
while(GetMessage(&msg,NULL,0,0))
{
if(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
還要建立加速鍵表資源和相應項,指向菜單,或自定義的ID,
在wm_command或wm_syscommand中處理,和菜單類似
你可以建立默認的win32工程試試,或者是helloworld的win32工程,vs的幾個版本應該差的不太多,裡面應該含有例子
㈢ 最簡單的Win32應用程序問題
switch句子有問題,搞的亂七八糟,代碼些清晰點 // testarray.cpp : 定義控制台應用程序的入口點。 // #include "stdafx.h" #include<windows.h> #...